implements line scrolling

This commit is contained in:
SuperNovaa41 2025-01-15 17:47:21 -05:00
parent ab9b64492d
commit f20770693b
5 changed files with 39 additions and 7 deletions

View File

@ -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");

View File

@ -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);
}

View File

@ -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

View File

@ -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;

View File

@ -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;