Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Gurrium committed Dec 17, 2022
1 parent 7ee418d commit f82b284
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions kilo.c
Expand Up @@ -9,7 +9,7 @@

/*** difnes ***/

#define CTRL_KEY(k) ((k) & 0x1f)
#define CTRL_KEY(k) ((k)&0x1f)

/*** data ***/

Expand Down Expand Up @@ -41,20 +41,33 @@ void enableRawMode() {
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) die("tcsetattr");
}

char editorReadKey() {
int nread;
char c;
while ((nread = read(STDIN_FILENO, &c, 1)) != 1) {
if (nread == -1 && errno != EAGAIN) die("read");
}
return c;
}

/*** input ***/
void editorProcessKeyPress() {
char c = editorReadKey();

switch (c) {
case CTRL_KEY('q'):
exit(0);
break;
}
}

/*** init ***/

int main() {
enableRawMode();

while (1) {
char c = '\0';
if (read(STDIN_FILENO, &c, 1) == -1 && errno != EAGAIN) die("read");
if (iscntrl(c)) {
printf("%d\r\n", c);
} else {
printf("%d ('%c')\r\n", c, c);
}
if (c == CTRL_KEY('q')) break;
editorProcessKeyPress();
}

return 0;
Expand Down

0 comments on commit f82b284

Please sign in to comment.