From 34d688c08ad0025c878d24f224f73083e14e1512 Mon Sep 17 00:00:00 2001 From: Johannes Findeisen Date: Fri, 27 Nov 2026 22:53:59 +0100 Subject: [PATCH] Save window scrollback with Ctrl-S --- README.md | 4 ++ irc9.c | 120 ++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 97 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 2529726..60e0845 100644 --- a/README.md +++ b/README.md @@ -136,9 +136,13 @@ MODE, TOPIC, and INVITE events are shown in the relevant window. | `ctrl-n` / `ctrl-p` | next / previous window (tree order, wraps around) | | `PageUp` / `PageDown` | scroll the current window | | `ctrl-l` | clear the screen (history stays scrollable) | +| `ctrl-s` | save the current window’s scrollback to a new plain-text file | | Up / Down | command history | | Left / Right, Home / End | edit the input line | +At the save prompt, type a path and press Enter, or press Esc or Ctrl-G to +cancel. Existing files are protected from overwrites. + ## Commands diff --git a/irc9.c b/irc9.c index 1104d5a..cbbc6ae 100644 --- a/irc9.c +++ b/irc9.c @@ -172,6 +172,9 @@ static int silent; static char input[MAXINPUT]; static int input_len, input_pos; +static int saving; +static char save_path[MAXINPUT]; +static int save_len, save_pos; static char history[MAXHIST][MAXINPUT]; static int hist_n, hist_cur = -1; static int comp_active, comp_index; @@ -334,7 +337,10 @@ static void window_close(Server *s, Window *w) Member *mn = m->next; free(m); m = mn; } if (s->reply_win == w) s->reply_win = NULL; - if (curwin == w) focus_window(s->windows); + if (curwin == w) { + saving = 0; + focus_window(s->windows); + } free(w); dirty = 1; } @@ -1911,6 +1917,7 @@ static void do_command(char *line) appendf(t, " /voice /devoice /ban /unban /raw /close /wc /clear"); appendf(t, " /reconnect /quit /help"); appendf(t, "keys: ctrl-n / ctrl-p switch windows, pgup/pgdn scroll, ctrl-l clear screen"); + appendf(t, " ctrl-s save scrollback"); } else { if (curwin) appendf(curwin, "* unknown command: /%s", cmd); } @@ -1940,8 +1947,73 @@ static void submit_input(void) dirty = 1; } +static void save_scrollback(Window *w, const char *path) +{ + FILE *file = fopen(path, "wx"); + if (!file) { + appendf(w, "* cannot save %s: %s", path, strerror(errno)); + return; + } + + int error = 0; + for (int i = 0; i < w->nlines && !error; i++) { + char *line = strip_ircfmt(w->lines[i]); + for (char *p = line; *p; p++) + if (((unsigned char)*p < 32 && *p != '\t') || *p == 127) *p = '?'; + if (fprintf(file, "%s\n", line) < 0) error = errno; + free(line); + } + if (fclose(file) == EOF && !error) error = errno; + if (error) appendf(w, "* cannot finish saving %s: %s", path, strerror(error)); + else appendf(w, "* saved scrollback to %s", path); +} + +static void edit_line(int c, char text[MAXINPUT], int *len, int *pos) +{ + if (c == 127 || c == 8) { + if (*pos > 0) { + memmove(text + *pos - 1, text + *pos, + (size_t)(*len - *pos) + 1); + --*pos; + --*len; + } + dirty = 1; + return; + } + if (c == KEY_LEFT) { if (*pos > 0) (*pos)--; dirty = 1; return; } + if (c == KEY_RIGHT) { if (*pos < *len) (*pos)++; dirty = 1; return; } + if (c == KEY_HOME) { *pos = 0; dirty = 1; return; } + if (c == KEY_END) { *pos = *len; dirty = 1; return; } + if (((c >= 32 && c < 127) || (c >= 0x80 && c < 0x100)) && + *len < MAXINPUT - 1) { + memmove(text + *pos + 1, text + *pos, + (size_t)(*len - *pos) + 1); + text[(*pos)++] = (char)c; + ++*len; + dirty = 1; + } +} + static void handle_key(int c) { + if (saving) { + if (c == 0x07 || c == 27 || c == '\n' || c == '\r') { + saving = 0; + dirty = 1; + if ((c == '\n' || c == '\r') && save_len) + save_scrollback(curwin, save_path); + } else { + edit_line(c, save_path, &save_len, &save_pos); + } + return; + } + if (c == 0x13 && curwin) { /* ctrl-s */ + saving = 1; + save_path[0] = 0; + save_len = save_pos = 0; + dirty = 1; + return; + } if (c != '\t') comp_active = 0; if (c == '\t') { complete_input(); return; } if (c == 0x0e) { switch_window(1); return; } /* ctrl-n */ @@ -1954,19 +2026,6 @@ static void handle_key(int c) if (c == '\n' || c == '\r') { submit_input(); return; } - if (c == 127 || c == 8) { - if (input_pos > 0) { - memmove(input + input_pos - 1, input + input_pos, - (size_t)(input_len - input_pos) + 1); - input_pos--; input_len--; - } - dirty = 1; - return; - } - if (c == KEY_LEFT) { if (input_pos > 0) input_pos--; dirty = 1; return; } - if (c == KEY_RIGHT) { if (input_pos < input_len) input_pos++; dirty = 1; return; } - if (c == KEY_HOME) { input_pos = 0; dirty = 1; return; } - if (c == KEY_END) { input_pos = input_len; dirty = 1; return; } if (c == KEY_UP) { history_browse(-1); dirty = 1; return; } if (c == KEY_DOWN) { history_browse(1); dirty = 1; return; } if (c == KEY_PPAGE) { @@ -1989,14 +2048,7 @@ static void handle_key(int c) } return; } - if (((c >= 32 && c < 127) || (c >= 0x80 && c < 0x100)) && - input_len < MAXINPUT - 1) { - memmove(input + input_pos + 1, input + input_pos, - (size_t)(input_len - input_pos) + 1); - input[input_pos++] = (char)c; - input_len++; - dirty = 1; - } + edit_line(c, input, &input_len, &input_pos); } /* ------------------------------------------------------------------ */ @@ -2353,7 +2405,10 @@ static void draw_input(void) fputs(terminal_color ? "\033[1m" : "\033[7m", stdout); terminal_move(lines - 2, 0); terminal_repeat(' ', cols); - if (curwin) { + if (saving && cols > 2) { + terminal_move(lines - 2, 1); + terminal_text("Save scrollback to file (Enter to save, Esc to cancel)", cols - 2); + } else if (curwin) { char now[16], status[MAXLINE], umode[40]; fmttime(now, sizeof now, "%H:%M"); if (curwin->srv->umode[0]) @@ -2373,14 +2428,23 @@ static void draw_input(void) terminal_move(lines - 1, 0); if (cols > 1) fputs("> ", stdout); - terminal_text(input, cols - 3); + const char *text = saving ? save_path : input; + int pos = saving ? save_pos : input_pos; + int start = 0; int x = 2; - for (int i = 0; i < input_pos; ) { + for (int i = 0; i < pos; ) { int width; - size_t len = terminal_character(input + i, (size_t)(input_pos - i), &width); + size_t len = terminal_character(text + i, (size_t)(pos - i), &width); x += width; i += len ? (int)len : 1; } + while (saving && x >= cols && start < pos) { + int width; + size_t len = terminal_character(text + start, (size_t)(pos - start), &width); + start += len ? (int)len : 1; + x -= width; + } + terminal_text(text + start, cols - 3); if (x >= cols) x = cols - 1; terminal_move(lines - 1, x); } @@ -2438,8 +2502,10 @@ static void terminal_read_keys(void) struct timespec now = {0}; clock_gettime(CLOCK_MONOTONIC, &now); if (state && (now.tv_sec - last_escape.tv_sec) * 1000LL + - (now.tv_nsec - last_escape.tv_nsec) / 1000000 >= 1000) + (now.tv_nsec - last_escape.tv_nsec) / 1000000 >= 1000) { + if (state == 1 && saving) handle_key(27); state = 0; + } unsigned char bytes[256]; ssize_t count = read(STDIN_FILENO, bytes, sizeof bytes);