From f20770693bce0f4b5e73a4236d8c4df6f976d250 Mon Sep 17 00:00:00 2001 From: SuperNovaa41 Date: Wed, 15 Jan 2025 17:47:21 -0500 Subject: [PATCH] implements line scrolling --- src/controls.c | 24 ++++++++++++++++++++---- src/draw.c | 12 +++++++++--- src/include/draw.h | 3 +++ src/include/term.h | 3 +++ src/term.c | 4 ++++ 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/controls.c b/src/controls.c index 0e66acb..07feb7f 100644 --- a/src/controls.c +++ b/src/controls.c @@ -73,37 +73,53 @@ void move_cursor(movement_t dir) if (editor.cx - 1 <= 0) break; move_cursor_pos(--editor.cx, editor.cy); + editor.rx--; break; case RIGHT: if (editor.cx + 1 > editor.rows[editor.cy - 1].len) break; move_cursor_pos(++editor.cx, editor.cy); + editor.rx++; break; case UP: - if (editor.cy -1 <= 0) + if (editor.cy - 1 <= 0) { + if (editor.row_offset == 0) + break; + editor.row_offset--; + editor.ry--; break; + } move_cursor_pos(editor.cx, --editor.cy); + editor.ry--; // want to fall down to the end of the line if we're moving to a shorter one DO_FALLDOWN(); break; case DOWN: - if (editor.cy + 1 >= editor.num_rows) + if (editor.cy + 1 >= editor.screen_rows - 1) { + if (editor.ry + 1 >= editor.num_rows) + break; + editor.row_offset++; + editor.ry++; break; + } + move_cursor_pos(editor.cx, ++editor.cy); + editor.ry++; DO_FALLDOWN(); break; case HOME: move_cursor_pos(1, 1); - editor.cx = 1; - editor.cy = 1; + editor.cx = 1; editor.cy = 1; + editor.rx = 1; editor.ry = 1; break; case BEGIN_LINE: move_cursor_pos(1, editor.cy); editor.cx = 1; + editor.rx = 1; break; default: fprintf(stderr, "move_cursor had an invalid input, what the hell.\n"); diff --git a/src/draw.c b/src/draw.c index f6b98c5..8dae315 100644 --- a/src/draw.c +++ b/src/draw.c @@ -48,7 +48,10 @@ void refresh_screen(void) void draw_file_buffer(void) { size_t i; - for (i = 0; i < editor.num_rows; i++) { + + // reserve the last line for info + for (i = editor.row_offset; i < editor.num_rows && i < (editor.screen_rows + editor.row_offset - 1); i++) { + screen_buffer_append(CLEAR_LINE_STR, CLEAR_LINE_STR_LEN); // clear line first to prevent flickering issues screen_buffer_append(editor.rows[i].line, editor.rows[i].len); } } @@ -56,14 +59,17 @@ void draw_file_buffer(void) void draw_editor_rows(void) { size_t i; - for (i = editor.num_rows; i < editor.screen_rows - 1; i++) + for (i = editor.num_rows; i < editor.screen_rows + editor.row_offset - 1; i++) { + screen_buffer_append(CLEAR_LINE_STR, CLEAR_LINE_STR_LEN); screen_buffer_append("~\r\n", 3); + } } void draw_cursor_pos(void) { char* pos = malloc(sizeof(char) * MAX_LEN); - snprintf(pos, MAX_LEN, "%d,%d", editor.cx, editor.cy); + snprintf(pos, MAX_LEN, "%d,%d", editor.rx, editor.ry); + screen_buffer_append(CLEAR_LINE_STR, CLEAR_LINE_STR_LEN); screen_buffer_append(pos, strlen(pos)); free(pos); } diff --git a/src/include/draw.h b/src/include/draw.h index 632d096..0d1d3b5 100644 --- a/src/include/draw.h +++ b/src/include/draw.h @@ -6,6 +6,9 @@ #define CLEAR_SCREEN_STR "\e[2J" #define CLEAR_SCREEN_STR_LEN 4 +#define CLEAR_LINE_STR "\e[2K" +#define CLEAR_LINE_STR_LEN 4 + #define MOVE_CURSOR_HOME_STR "\e[H" #define MOVE_CURSOR_HOME_STR_LEN 3 diff --git a/src/include/term.h b/src/include/term.h index 1e500a9..d16740b 100644 --- a/src/include/term.h +++ b/src/include/term.h @@ -18,9 +18,12 @@ typedef struct { struct termios term_settings; control_type_t control_type; + size_t row_offset; row_t* rows; size_t num_rows; + int rx, ry; + size_t screen_cols, screen_rows; int cx, cy; // cursor pos } editor_t; diff --git a/src/term.c b/src/term.c index 8794a18..7fdf349 100644 --- a/src/term.c +++ b/src/term.c @@ -111,6 +111,10 @@ void setup_terminal(void) editor.num_rows = 0; editor.rows = NULL; + editor.row_offset = 0; + + editor.rx = 1; + editor.ry = 1; editor.cx = 1; editor.cy = 1;