Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Gurrium committed Dec 26, 2022
1 parent 9a43b43 commit db1f78b
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion kilo.c
Expand Up @@ -18,6 +18,7 @@
/*** data ***/

struct editorConfig {
int cx, cy;
int screenrows;
int screencols;
struct termios orig_termios;
Expand Down Expand Up @@ -120,6 +121,23 @@ void abFree(struct abuf *ab) {

/*** input ***/

void editorMoveCursor(char key) {
switch (key) {
case 'a':
E.cx--;
break;
case 'd':
E.cx++;
break;
case 'w':
E.cy--;
break;
case 's':
E.cy++;
break;
}
}

void editorProcessKeyPress() {
char c = editorReadKey();

Expand All @@ -130,6 +148,12 @@ void editorProcessKeyPress() {

exit(0);
break;
case 'w':
case 's':
case 'a':
case 'd':
editorMoveCursor(c);
break;
}
}

Expand Down Expand Up @@ -169,7 +193,10 @@ void editorRefreshScreen() {

editorDrawRows(&ab);

abAppend(&ab, "\x1b[H", 3);
char buf[32];
snprintf(buf, sizeof(buf), "\x1b[%d;%dH", E.cy + 1, E.cx + 1);
abAppend(&ab, buf, strlen(buf));

abAppend(&ab, "\x1b[?25h", 6);

write(STDIN_FILENO, ab.b, ab.len);
Expand All @@ -179,6 +206,9 @@ void editorRefreshScreen() {
/*** init ***/

void initEditor() {
E.cx = 0;
E.cy = 0;

if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize");
}

Expand Down

0 comments on commit db1f78b

Please sign in to comment.