Lots of changes
implements keybinds restructures code, fixes up makefile etc
This commit is contained in:
parent
809da00448
commit
6f985888a0
11
src/Makefile
11
src/Makefile
@ -1,16 +1,19 @@
|
|||||||
TARGET=editor
|
TARGET=editor
|
||||||
CC=gcc
|
CC=gcc
|
||||||
|
|
||||||
OBJ = main.o draw.o term.o
|
OBJ = main.o term.o draw.o controls.o
|
||||||
|
|
||||||
$(TARGET): $(OBJ)
|
$(TARGET): $(OBJ)
|
||||||
mkdir -p ../build
|
mkdir -p ../build
|
||||||
$(CC) -o $(TARGET) $(OBJ) -g
|
$(CC) -o $(TARGET) $(OBJ) -g
|
||||||
mv $(TARGET) ../build/
|
mv $(TARGET) ../build/
|
||||||
|
|
||||||
main.o: term.h draw.h
|
main.o: headers/term.h headers/draw.h headers/controls.h
|
||||||
term.o: term.h draw.h
|
term.o: headers/term.h
|
||||||
draw.o: term.h draw.h
|
draw.o: headers/term.h headers/draw.h
|
||||||
|
controls.o: headers/controls.h headers/__defines.h headers/draw.h headers/term.h
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
clean:
|
clean:
|
||||||
|
96
src/controls.c
Normal file
96
src/controls.c
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "headers/__defines.h"
|
||||||
|
#include "headers/controls.h"
|
||||||
|
#include "headers/draw.h"
|
||||||
|
#include "headers/term.h"
|
||||||
|
|
||||||
|
extern editor_t editor;
|
||||||
|
extern screen_buffer_t screen_buffer;
|
||||||
|
|
||||||
|
int parse_input_char(char input_char)
|
||||||
|
{
|
||||||
|
if (INSERT_MODE == editor.control_type) {
|
||||||
|
if (ESC == input_char) {
|
||||||
|
editor.control_type = COMMAND_MODE;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// put char
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch(input_char) {
|
||||||
|
/** INSERT MODE KEYS START **/
|
||||||
|
case 'a':
|
||||||
|
move_cursor(RIGHT);
|
||||||
|
case 'i':
|
||||||
|
editor.control_type = INSERT_MODE;
|
||||||
|
break;
|
||||||
|
/** INSERT MODE KEYS END **/
|
||||||
|
|
||||||
|
/** MOVEMENT KEYS START **/
|
||||||
|
|
||||||
|
case 'h':
|
||||||
|
move_cursor(LEFT);
|
||||||
|
break;
|
||||||
|
case 'j':
|
||||||
|
move_cursor(DOWN);
|
||||||
|
break;
|
||||||
|
case 'k':
|
||||||
|
move_cursor(UP);
|
||||||
|
break;
|
||||||
|
case 'l':
|
||||||
|
move_cursor(RIGHT);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'g': // TODO: move this to 'gg'
|
||||||
|
move_cursor(HOME);
|
||||||
|
break;
|
||||||
|
case 'G':
|
||||||
|
// want to move to the end of the buffer
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '0':
|
||||||
|
move_cursor(BEGIN_LINE);
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
/** MOVEMENT KEYS END **/
|
||||||
|
default:
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void move_cursor(movement_t dir)
|
||||||
|
{
|
||||||
|
switch(dir) {
|
||||||
|
case LEFT:
|
||||||
|
move_cursor_pos(--editor.cx, editor.cy);
|
||||||
|
break;
|
||||||
|
case RIGHT:
|
||||||
|
move_cursor_pos(++editor.cx, editor.cy);
|
||||||
|
break;
|
||||||
|
case UP:
|
||||||
|
move_cursor_pos(editor.cx, --editor.cy);
|
||||||
|
break;
|
||||||
|
case DOWN:
|
||||||
|
move_cursor_pos(editor.cx, ++editor.cy);
|
||||||
|
break;
|
||||||
|
case HOME:
|
||||||
|
move_cursor_pos(1, 1);
|
||||||
|
editor.cx = 1;
|
||||||
|
editor.cy = 1;
|
||||||
|
break;
|
||||||
|
case BEGIN_LINE:
|
||||||
|
move_cursor_pos(1, editor.cy);
|
||||||
|
editor.cx = 1;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
fprintf(stderr, "move_cursor had an invalid input, what the hell.\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
31
src/draw.c
31
src/draw.c
@ -2,8 +2,8 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "draw.h"
|
#include "headers/draw.h"
|
||||||
#include "term.h"
|
#include "headers/term.h"
|
||||||
|
|
||||||
#define MAX_LEN 256
|
#define MAX_LEN 256
|
||||||
|
|
||||||
@ -17,13 +17,11 @@ extern screen_buffer_t screen_buffer;
|
|||||||
void refresh_screen(void)
|
void refresh_screen(void)
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* TODO: need to move the cursor back at the end
|
* TODO:
|
||||||
* also need to draw the cusor coords at the bottom
|
|
||||||
* and need to implement controls
|
* and need to implement controls
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int cur_x, cur_y;
|
int cur_x, cur_y;
|
||||||
char* move_cursor;
|
|
||||||
|
|
||||||
cur_x = editor.cx;
|
cur_x = editor.cx;
|
||||||
cur_y = editor.cy;
|
cur_y = editor.cy;
|
||||||
@ -36,10 +34,9 @@ void refresh_screen(void)
|
|||||||
screen_buffer_append(&screen_buffer, CLEAR_SCREEN_STR, CLEAR_SCREEN_STR_LEN);
|
screen_buffer_append(&screen_buffer, CLEAR_SCREEN_STR, CLEAR_SCREEN_STR_LEN);
|
||||||
|
|
||||||
draw_editor_rows();
|
draw_editor_rows();
|
||||||
|
draw_cursor_pos();
|
||||||
|
|
||||||
move_cursor_pos(&move_cursor, cur_x, cur_y);
|
move_cursor_pos(cur_x, cur_y);
|
||||||
screen_buffer_append(&screen_buffer, move_cursor, strlen(move_cursor));
|
|
||||||
free(move_cursor);
|
|
||||||
|
|
||||||
screen_buffer_append(&screen_buffer, SHOW_CURSOR_STR, SHOW_CURSOR_STR_LEN);
|
screen_buffer_append(&screen_buffer, SHOW_CURSOR_STR, SHOW_CURSOR_STR_LEN);
|
||||||
|
|
||||||
@ -53,11 +50,21 @@ void draw_editor_rows(void)
|
|||||||
screen_buffer_append(&screen_buffer, "~\r\n", 3);
|
screen_buffer_append(&screen_buffer, "~\r\n", 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void draw_cursor_pos(void)
|
||||||
void move_cursor_pos(char** out, int x, int y)
|
|
||||||
{
|
{
|
||||||
*out = malloc(sizeof(char) * MAX_LEN);
|
char* pos = malloc(sizeof(char) * MAX_LEN);
|
||||||
snprintf(*out, MAX_LEN, "\e[%d;%dH", x, y);
|
snprintf(pos, MAX_LEN, "%d,%d", editor.cx, editor.cy);
|
||||||
|
screen_buffer_append(&screen_buffer, pos, strlen(pos));
|
||||||
|
free(pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
void move_cursor_pos(int x, int y)
|
||||||
|
{
|
||||||
|
// TODO: add check for boundaries
|
||||||
|
char* out = malloc(sizeof(char) * MAX_LEN);
|
||||||
|
snprintf(out, MAX_LEN, "\e[%d;%dH", y, x);
|
||||||
|
screen_buffer_append(&screen_buffer, out, strlen(out));
|
||||||
|
free(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
#undef MAX_LEN
|
#undef MAX_LEN
|
||||||
|
8
src/headers/__defines.h
Normal file
8
src/headers/__defines.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#ifndef __DEFINES_H
|
||||||
|
#define __DEFINES_H
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
ESC = 0x1B,
|
||||||
|
} key_code_t;
|
||||||
|
|
||||||
|
#endif
|
15
src/headers/controls.h
Normal file
15
src/headers/controls.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#ifndef CONTROLS_H
|
||||||
|
#define CONTROLS_H
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
UP, DOWN,
|
||||||
|
LEFT, RIGHT,
|
||||||
|
HOME,
|
||||||
|
BEGIN_LINE,
|
||||||
|
} movement_t;
|
||||||
|
|
||||||
|
int parse_input_char(char in);
|
||||||
|
|
||||||
|
void move_cursor(movement_t dir);
|
||||||
|
|
||||||
|
#endif
|
@ -19,17 +19,14 @@
|
|||||||
void refresh_screen(void);
|
void refresh_screen(void);
|
||||||
|
|
||||||
void draw_editor_rows(void);
|
void draw_editor_rows(void);
|
||||||
|
void draw_cursor_pos(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* # move_cursor_pos
|
* # move_cursor_pos
|
||||||
*
|
*
|
||||||
* - char** out - pointer to char* which will be malloc'd and have the formatted string placed into
|
|
||||||
* - int x - x pos
|
* - int x - x pos
|
||||||
* - int y - y pos
|
* - int y - y pos
|
||||||
*
|
|
||||||
* out must be free'd
|
|
||||||
*/
|
*/
|
||||||
void move_cursor_pos(char** out, int x, int y);
|
void move_cursor_pos(int x, int y);
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -4,8 +4,14 @@
|
|||||||
#ifndef TERM_H
|
#ifndef TERM_H
|
||||||
#define TERM_H
|
#define TERM_H
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
COMMAND_MODE,
|
||||||
|
INSERT_MODE,
|
||||||
|
} control_type_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
struct termios term_settings;
|
struct termios term_settings;
|
||||||
|
control_type_t control_type;
|
||||||
size_t cols, rows;
|
size_t cols, rows;
|
||||||
int cx, cy; // cursor pos
|
int cx, cy; // cursor pos
|
||||||
} editor_t;
|
} editor_t;
|
@ -1,8 +1,8 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "draw.h"
|
#include "headers/controls.h"
|
||||||
#include "term.h"
|
#include "headers/draw.h"
|
||||||
|
#include "headers/term.h"
|
||||||
|
|
||||||
editor_t editor;
|
editor_t editor;
|
||||||
screen_buffer_t screen_buffer;
|
screen_buffer_t screen_buffer;
|
||||||
@ -20,6 +20,8 @@ int main(void)
|
|||||||
read(STDIN_FILENO, &c, 1);
|
read(STDIN_FILENO, &c, 1);
|
||||||
if (c == '\004')
|
if (c == '\004')
|
||||||
break;
|
break;
|
||||||
|
else
|
||||||
|
parse_input_char(c);
|
||||||
refresh_screen();
|
refresh_screen();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
|
||||||
#include "term.h"
|
#include "headers/term.h"
|
||||||
|
|
||||||
/** BEGIN extern section **/
|
/** BEGIN extern section **/
|
||||||
|
|
||||||
@ -107,8 +107,10 @@ void setup_terminal(void)
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
editor.cx = 0;
|
editor.control_type = COMMAND_MODE;
|
||||||
editor.cy = 0;
|
|
||||||
|
editor.cx = 1;
|
||||||
|
editor.cy = 1;
|
||||||
|
|
||||||
editor.cols = win.ws_col;
|
editor.cols = win.ws_col;
|
||||||
editor.rows = win.ws_row;
|
editor.rows = win.ws_row;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user