Lots of changes

implements keybinds
restructures code, fixes up makefile
etc
This commit is contained in:
SuperNovaa41 2025-01-15 15:47:51 -05:00
parent 809da00448
commit 6f985888a0
9 changed files with 163 additions and 27 deletions

View File

@ -1,16 +1,19 @@
TARGET=editor
CC=gcc
OBJ = main.o draw.o term.o
OBJ = main.o term.o draw.o controls.o
$(TARGET): $(OBJ)
mkdir -p ../build
$(CC) -o $(TARGET) $(OBJ) -g
mv $(TARGET) ../build/
main.o: term.h draw.h
term.o: term.h draw.h
draw.o: term.h draw.h
main.o: headers/term.h headers/draw.h headers/controls.h
term.o: headers/term.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
clean:

96
src/controls.c Normal file
View 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);
}
}

View File

@ -2,8 +2,8 @@
#include <string.h>
#include <unistd.h>
#include "draw.h"
#include "term.h"
#include "headers/draw.h"
#include "headers/term.h"
#define MAX_LEN 256
@ -17,13 +17,11 @@ extern screen_buffer_t screen_buffer;
void refresh_screen(void)
{
/**
* TODO: need to move the cursor back at the end
* also need to draw the cusor coords at the bottom
* TODO:
* and need to implement controls
*/
int cur_x, cur_y;
char* move_cursor;
cur_x = editor.cx;
cur_y = editor.cy;
@ -36,10 +34,9 @@ void refresh_screen(void)
screen_buffer_append(&screen_buffer, CLEAR_SCREEN_STR, CLEAR_SCREEN_STR_LEN);
draw_editor_rows();
draw_cursor_pos();
move_cursor_pos(&move_cursor, cur_x, cur_y);
screen_buffer_append(&screen_buffer, move_cursor, strlen(move_cursor));
free(move_cursor);
move_cursor_pos(cur_x, cur_y);
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);
}
void move_cursor_pos(char** out, int x, int y)
void draw_cursor_pos(void)
{
*out = malloc(sizeof(char) * MAX_LEN);
snprintf(*out, MAX_LEN, "\e[%d;%dH", x, y);
char* pos = malloc(sizeof(char) * MAX_LEN);
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

8
src/headers/__defines.h Normal file
View 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
View 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

View File

@ -19,17 +19,14 @@
void refresh_screen(void);
void draw_editor_rows(void);
void draw_cursor_pos(void);
/**
* # 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 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

View File

@ -4,8 +4,14 @@
#ifndef TERM_H
#define TERM_H
typedef enum {
COMMAND_MODE,
INSERT_MODE,
} control_type_t;
typedef struct {
struct termios term_settings;
control_type_t control_type;
size_t cols, rows;
int cx, cy; // cursor pos
} editor_t;

View File

@ -1,8 +1,8 @@
#include <stdio.h>
#include <unistd.h>
#include "draw.h"
#include "term.h"
#include "headers/controls.h"
#include "headers/draw.h"
#include "headers/term.h"
editor_t editor;
screen_buffer_t screen_buffer;
@ -20,6 +20,8 @@ int main(void)
read(STDIN_FILENO, &c, 1);
if (c == '\004')
break;
else
parse_input_char(c);
refresh_screen();
}

View File

@ -6,7 +6,7 @@
#include <unistd.h>
#include "term.h"
#include "headers/term.h"
/** BEGIN extern section **/
@ -107,8 +107,10 @@ void setup_terminal(void)
exit(EXIT_FAILURE);
}
editor.cx = 0;
editor.cy = 0;
editor.control_type = COMMAND_MODE;
editor.cx = 1;
editor.cy = 1;
editor.cols = win.ws_col;
editor.rows = win.ws_row;