nasmfm

sfm rewrite
git clone https://git.afify.dev/nasmfm.git
Log | Files | Refs | LICENSE

commit 0840e8d192a2cc5644e453a14c826807ef5ec28c
parent 6c865f04f3cce5ed4502fc32b8e2a044e6d33ec2
Author: afify <hassan@afify.dev>
Date:   Wed,  9 Mar 2022 16:59:45 +0300

[feat] add cursor pos

Diffstat:
Mnasmfm.c | 33++++++++++++++++++---------------
1 file changed, 18 insertions(+), 15 deletions(-)

diff --git a/nasmfm.c b/nasmfm.c @@ -12,10 +12,10 @@ /* macros */ #define CTRL_KEY(k) ((k)&0x1f) -#define CLEAR_SCREEN abAppend(&ab, "\x1b[2J", 4); -#define CURSOR_TOP_LEFT abAppend(&ab, "\x1b[H", 3); -#define CURSOR_HIDE abAppend(&ab, "\x1b[?25l", 6); -#define CURSOR_SHOW abAppend(&ab, "\x1b[?25h", 6); +#define CLEAR_SCREEN abAppend(&ab, "\x1b[2J", 4); +#define CURSOR_TOP_LEFT abAppend(&ab, "\x1b[H", 3); +#define CURSOR_HIDE abAppend(&ab, "\x1b[?25l", 6); +#define CURSOR_SHOW abAppend(&ab, "\x1b[?25h", 6); #define ABUF_INIT \ { \ NULL, 0 \ @@ -25,6 +25,8 @@ /* function declarations */ typedef struct { + int cx; + int cy; int rows; int cols; struct termios orig_termios; @@ -58,8 +60,8 @@ abFree(struct abuf *ab) void die(const char *s) { - CLEAR_SCREEN - CURSOR_TOP_LEFT + //CLEAR_SCREEN + //CURSOR_TOP_LEFT perror(s); exit(1); } @@ -123,10 +125,13 @@ void editorRefreshScreen() { struct abuf ab = ABUF_INIT; - //abAppend(&ab, "\x1b[2J", 4); + abAppend(&ab, "\x1b[?25l", 6); abAppend(&ab, "\x1b[H", 3); editorDrawRows(&ab); - abAppend(&ab, "\x1b[H", 3); + char buf[32]; + snprintf(buf, sizeof(buf), "\x1b[%d;%dH", term.cy + 1, term.cx + 1); + abAppend(&ab, buf, strlen(buf)); + abAppend(&ab, "\x1b[?25h", 6); write(STDOUT_FILENO, ab.b, ab.len); abFree(&ab); } @@ -148,16 +153,12 @@ editorProcessKeypress() { char c = editorReadKey(); switch (c) { - case CTRL_KEY('q'): - CLEAR_SCREEN - CURSOR_TOP_LEFT - exit(0); - break; case 'q': - CLEAR_SCREEN - CURSOR_TOP_LEFT + write(STDOUT_FILENO, "\x1b[2J", 4); + write(STDOUT_FILENO, "\x1b[H", 3); exit(0); break; + break; default: printf("%d ('%c')\r\n", c, c); } @@ -179,6 +180,8 @@ getWindowSize(int *rows, int *cols) void initEditor() { + term.cx = 0; + term.cy = 0; if (getWindowSize(&term.rows, &term.cols) == -1) die("getWindowSize"); }