Skip to content

Commit

Permalink
"Press any key" after CTRL+R
Browse files Browse the repository at this point in the history
  • Loading branch information
danielinux committed Oct 1, 2024
1 parent 689d48c commit 0a746da
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/cjit.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,12 @@ static int cjit_compile_buffer(void *tcs, char *code, int argc, char **argv)
_err(err_msg);
free(err_msg);
}
enableGetCharMode(STDIN_FILENO);
_err("\n\n\n\nPress any key to continue....\n");
getchar();
disableGetCharMode(STDIN_FILENO);

enableRawMode(STDIN_FILENO);
sleep(3);
editorRefreshScreen();
return res;
}
Expand Down
32 changes: 32 additions & 0 deletions src/kilo.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,38 @@ int enableRawMode(int fd) {
return -1;
}

/* For getchar() behavior */
int enableGetCharMode(int fd) {
struct termios nocanon;

if (!isatty(STDIN_FILENO))
goto fatal;
disableRawMode(STDIN_FILENO);
if (tcgetattr(fd,&orig_termios) == -1)
goto fatal;

nocanon = orig_termios; /* modify the original mode */
/* input modes: no break, no CR to NL, no parity check, no strip char,
* no start/stop output control. */
nocanon.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
/* control chars - set return condition: min number of bytes and timer. */
nocanon.c_cc[VMIN] = 1; /* Wait for one byte */
nocanon.c_cc[VTIME] = 0; /* No timeout */

/* put terminal in raw mode after flushing */
if (tcsetattr(fd,TCSAFLUSH,&nocanon) < 0) goto fatal;
return 0;

fatal:
errno = ENOTTY;
return -1;
}

/* Disable GetChar mode */
void disableGetCharMode(int fd) {
tcsetattr(fd,TCSAFLUSH,&orig_termios);
}

/* Read a key from the terminal put in raw mode, trying to handle
* escape sequences. */
int editorReadKey(int fd) {
Expand Down

0 comments on commit 0a746da

Please sign in to comment.