Skip to content

Commit 46c0098

Browse files
committed
added restore screen at exit
1 parent c8bd6a4 commit 46c0098

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/loki_terminal.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ static editor_ctx_t *signal_context = NULL;
3232
void terminal_disable_raw_mode(editor_ctx_t *ctx, int fd) {
3333
/* Don't even check the return value as it's too late. */
3434
if (ctx && ctx->rawmode) {
35+
/* Exit alternate screen buffer (restores original terminal content)
36+
* Only if stdout is a terminal (not a pipe or file) */
37+
if (isatty(STDOUT_FILENO)) {
38+
(void)write(STDOUT_FILENO, "\x1b[?1049l", 8);
39+
}
3540
tcsetattr(fd, TCSAFLUSH, &orig_termios);
3641
ctx->rawmode = 0;
3742
}
@@ -65,6 +70,12 @@ int terminal_enable_raw_mode(editor_ctx_t *ctx, int fd) {
6570
if (tcsetattr(fd, TCSAFLUSH, &raw) < 0) goto fatal;
6671
ctx->rawmode = 1;
6772

73+
/* Enter alternate screen buffer (saves current screen, gives clean slate)
74+
* Only if stdout is a terminal (not a pipe or file) */
75+
if (isatty(STDOUT_FILENO)) {
76+
(void)write(STDOUT_FILENO, "\x1b[?1049h", 8);
77+
}
78+
6879
/* Register this context for signal handling */
6980
signal_context = ctx;
7081

0 commit comments

Comments
 (0)