retains cursor position, adds a move cursor function

This commit is contained in:
SuperNovaa41 2025-01-15 12:02:42 -05:00
parent c6bdbe1676
commit 809da00448
4 changed files with 36 additions and 3 deletions

View File

@ -1,8 +1,12 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include "draw.h" #include "draw.h"
#include "term.h" #include "term.h"
#define MAX_LEN 256
/** BEGIN extern section **/ /** BEGIN extern section **/
extern editor_t editor; extern editor_t editor;
@ -19,6 +23,7 @@ void refresh_screen(void)
*/ */
int cur_x, cur_y; int cur_x, cur_y;
char* move_cursor;
cur_x = editor.cx; cur_x = editor.cx;
cur_y = editor.cy; cur_y = editor.cy;
@ -32,6 +37,10 @@ void refresh_screen(void)
draw_editor_rows(); 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); screen_buffer_append(&screen_buffer, SHOW_CURSOR_STR, SHOW_CURSOR_STR_LEN);
write(STDOUT_FILENO, screen_buffer.text, screen_buffer.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++) for (i = 0; i < editor.rows - 1; i++)
screen_buffer_append(&screen_buffer, "~\r\n", 3); 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

View File

@ -20,4 +20,16 @@ void refresh_screen(void);
void draw_editor_rows(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 #endif

View File

@ -1,3 +1,4 @@
#include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include "draw.h" #include "draw.h"
@ -9,6 +10,7 @@ screen_buffer_t screen_buffer;
int main(void) int main(void)
{ {
char c; char c;
char* in;
setup_terminal(); setup_terminal();
@ -21,7 +23,5 @@ int main(void)
refresh_screen(); refresh_screen();
} }
kill_application();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -107,6 +107,9 @@ void setup_terminal(void)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
editor.cx = 0;
editor.cy = 0;
editor.cols = win.ws_col; editor.cols = win.ws_col;
editor.rows = win.ws_row; editor.rows = win.ws_row;
} }