implements line scrolling
This commit is contained in:
parent
ab9b64492d
commit
f20770693b
@ -73,37 +73,53 @@ void move_cursor(movement_t dir)
|
|||||||
if (editor.cx - 1 <= 0)
|
if (editor.cx - 1 <= 0)
|
||||||
break;
|
break;
|
||||||
move_cursor_pos(--editor.cx, editor.cy);
|
move_cursor_pos(--editor.cx, editor.cy);
|
||||||
|
editor.rx--;
|
||||||
break;
|
break;
|
||||||
case RIGHT:
|
case RIGHT:
|
||||||
if (editor.cx + 1 > editor.rows[editor.cy - 1].len)
|
if (editor.cx + 1 > editor.rows[editor.cy - 1].len)
|
||||||
break;
|
break;
|
||||||
move_cursor_pos(++editor.cx, editor.cy);
|
move_cursor_pos(++editor.cx, editor.cy);
|
||||||
|
editor.rx++;
|
||||||
break;
|
break;
|
||||||
case UP:
|
case UP:
|
||||||
if (editor.cy -1 <= 0)
|
if (editor.cy - 1 <= 0) {
|
||||||
|
if (editor.row_offset == 0)
|
||||||
|
break;
|
||||||
|
editor.row_offset--;
|
||||||
|
editor.ry--;
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
move_cursor_pos(editor.cx, --editor.cy);
|
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
|
// want to fall down to the end of the line if we're moving to a shorter one
|
||||||
DO_FALLDOWN();
|
DO_FALLDOWN();
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case DOWN:
|
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;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
move_cursor_pos(editor.cx, ++editor.cy);
|
move_cursor_pos(editor.cx, ++editor.cy);
|
||||||
|
editor.ry++;
|
||||||
|
|
||||||
DO_FALLDOWN();
|
DO_FALLDOWN();
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case HOME:
|
case HOME:
|
||||||
move_cursor_pos(1, 1);
|
move_cursor_pos(1, 1);
|
||||||
editor.cx = 1;
|
editor.cx = 1; editor.cy = 1;
|
||||||
editor.cy = 1;
|
editor.rx = 1; editor.ry = 1;
|
||||||
break;
|
break;
|
||||||
case BEGIN_LINE:
|
case BEGIN_LINE:
|
||||||
move_cursor_pos(1, editor.cy);
|
move_cursor_pos(1, editor.cy);
|
||||||
editor.cx = 1;
|
editor.cx = 1;
|
||||||
|
editor.rx = 1;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "move_cursor had an invalid input, what the hell.\n");
|
fprintf(stderr, "move_cursor had an invalid input, what the hell.\n");
|
||||||
|
12
src/draw.c
12
src/draw.c
@ -48,7 +48,10 @@ void refresh_screen(void)
|
|||||||
void draw_file_buffer(void)
|
void draw_file_buffer(void)
|
||||||
{
|
{
|
||||||
size_t i;
|
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);
|
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)
|
void draw_editor_rows(void)
|
||||||
{
|
{
|
||||||
size_t i;
|
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);
|
screen_buffer_append("~\r\n", 3);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw_cursor_pos(void)
|
void draw_cursor_pos(void)
|
||||||
{
|
{
|
||||||
char* pos = malloc(sizeof(char) * MAX_LEN);
|
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));
|
screen_buffer_append(pos, strlen(pos));
|
||||||
free(pos);
|
free(pos);
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,9 @@
|
|||||||
#define CLEAR_SCREEN_STR "\e[2J"
|
#define CLEAR_SCREEN_STR "\e[2J"
|
||||||
#define CLEAR_SCREEN_STR_LEN 4
|
#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 "\e[H"
|
||||||
#define MOVE_CURSOR_HOME_STR_LEN 3
|
#define MOVE_CURSOR_HOME_STR_LEN 3
|
||||||
|
|
||||||
|
@ -18,9 +18,12 @@ typedef struct {
|
|||||||
struct termios term_settings;
|
struct termios term_settings;
|
||||||
control_type_t control_type;
|
control_type_t control_type;
|
||||||
|
|
||||||
|
size_t row_offset;
|
||||||
row_t* rows;
|
row_t* rows;
|
||||||
size_t num_rows;
|
size_t num_rows;
|
||||||
|
|
||||||
|
int rx, ry;
|
||||||
|
|
||||||
size_t screen_cols, screen_rows;
|
size_t screen_cols, screen_rows;
|
||||||
int cx, cy; // cursor pos
|
int cx, cy; // cursor pos
|
||||||
} editor_t;
|
} editor_t;
|
||||||
|
@ -111,6 +111,10 @@ void setup_terminal(void)
|
|||||||
|
|
||||||
editor.num_rows = 0;
|
editor.num_rows = 0;
|
||||||
editor.rows = NULL;
|
editor.rows = NULL;
|
||||||
|
editor.row_offset = 0;
|
||||||
|
|
||||||
|
editor.rx = 1;
|
||||||
|
editor.ry = 1;
|
||||||
|
|
||||||
editor.cx = 1;
|
editor.cx = 1;
|
||||||
editor.cy = 1;
|
editor.cy = 1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user