diff --git a/src/draw.c b/src/draw.c index 71050b0..6327485 100644 --- a/src/draw.c +++ b/src/draw.c @@ -1,8 +1,12 @@ +#include +#include #include #include "draw.h" #include "term.h" +#define MAX_LEN 256 + /** BEGIN extern section **/ extern editor_t editor; @@ -17,8 +21,9 @@ void refresh_screen(void) * also need to draw the cusor coords at the bottom * and need to implement controls */ - + int cur_x, cur_y; + char* move_cursor; cur_x = editor.cx; cur_y = editor.cy; @@ -32,6 +37,10 @@ void refresh_screen(void) draw_editor_rows(); + move_cursor_pos(&move_cursor, 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); write(STDOUT_FILENO, screen_buffer.text, screen_buffer.len); @@ -43,3 +52,12 @@ void draw_editor_rows(void) for (i = 0; i < editor.rows - 1; i++) screen_buffer_append(&screen_buffer, "~\r\n", 3); } + + +void move_cursor_pos(char** out, int x, int y) +{ + *out = malloc(sizeof(char) * MAX_LEN); + snprintf(*out, MAX_LEN, "\e[%d;%dH", x, y); +} + +#undef MAX_LEN diff --git a/src/draw.h b/src/draw.h index 9ae4577..1b2553c 100644 --- a/src/draw.h +++ b/src/draw.h @@ -20,4 +20,16 @@ void refresh_screen(void); void draw_editor_rows(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); + + #endif diff --git a/src/main.c b/src/main.c index a2dadd9..8b5ff3b 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,4 @@ +#include #include #include "draw.h" @@ -9,6 +10,7 @@ screen_buffer_t screen_buffer; int main(void) { char c; + char* in; setup_terminal(); @@ -21,7 +23,5 @@ int main(void) refresh_screen(); } - kill_application(); - return EXIT_SUCCESS; } diff --git a/src/term.c b/src/term.c index ae41ad9..ff5121a 100644 --- a/src/term.c +++ b/src/term.c @@ -107,6 +107,9 @@ void setup_terminal(void) exit(EXIT_FAILURE); } + editor.cx = 0; + editor.cy = 0; + editor.cols = win.ws_col; editor.rows = win.ws_row; }