Make scrollback length configurable from the CLI

This commit is contained in:
Johannes Findeisen 2026-11-29 23:05:19 +01:00
commit 15a6c6dee9
2 changed files with 23 additions and 9 deletions

View file

@ -26,6 +26,8 @@ line.
Use `-c RRGGBB` or `--color RRGGBB` to replace the default cyan accent on
the channel tree and divider. For example, `./irc9 --color FF8800` uses
orange on terminals with true-color support.
Use `--scrollback LINES` to set how many lines each window retains. The default
is 500 lines. For example, `./irc9 --scrollback 2000` keeps 2000 lines per window.
With no config path, irc9 looks next to the executable, in this order:
`irc9rc`, `config`, then `~/.irc9rc`. Paths are relative to the binary, not to

30
irc9.c
View file

@ -60,7 +60,6 @@
#define MAXHIST 64
#define MAXSCROLL 500
#define MAXWRAPROWS 128
#define MAXSCROLLROWS (MAXSCROLL * MAXWRAPROWS)
#define TREEW 24
#define RETRY 5
@ -177,6 +176,7 @@ static volatile sig_atomic_t quit_flag;
static int dirty = 1;
static int input_dirty;
static time_t status_minute;
static int scrollback_limit = MAXSCROLL;
static int cols = 80, lines = 24;
static int tree_offset;
static int treew = TREEW, mainh = 1, chat_top = 1;
@ -326,7 +326,7 @@ static Window *window_new(Server *s, const char *name, Wintype type)
snprintf(w->name, sizeof w->name, "%s", name);
w->type = type;
w->srv = s;
w->lines = xalloc_array(MAXSCROLL, sizeof *w->lines);
w->lines = xalloc_array(scrollback_limit, sizeof *w->lines);
window_insert(s, w);
return w;
}
@ -444,9 +444,9 @@ static void set_channel_topic(Window *w, const char *topic)
static void win_append_raw(Window *w, const char *text, int keep_formatting, size_t body_offset)
{
if (w->nlines == MAXSCROLL) {
if (w->nlines == scrollback_limit) {
free(w->lines[0].text);
memmove(w->lines, w->lines + 1, (MAXSCROLL - 1) * sizeof *w->lines);
memmove(w->lines, w->lines + 1, (scrollback_limit - 1) * sizeof *w->lines);
w->nlines--;
}
char stamp[16], *line;
@ -458,7 +458,7 @@ static void win_append_raw(Window *w, const char *text, int keep_formatting, siz
if (body_offset > strlen(clean)) body_offset = strlen(clean);
w->lines[w->nlines++] = (ScrollLine){line, strlen(stamp) + body_offset};
free(clean);
if (w->scroll > 0) w->scroll++;
if (w->scroll > 0 && w->scroll < scrollback_limit * MAXWRAPROWS) w->scroll++;
if (w != curwin) w->unread++;
dirty = 1;
}
@ -2304,8 +2304,9 @@ static void handle_key(int c)
int height = mainh - layout_topic(0);
int step = height > 1 ? height - 1 : 1;
if (curwin->scroll < 0) curwin->scroll = 0;
if (curwin->scroll > MAXSCROLLROWS - step)
curwin->scroll = MAXSCROLLROWS;
int limit = scrollback_limit * MAXWRAPROWS;
if (curwin->scroll > limit - step)
curwin->scroll = limit;
else
curwin->scroll += step;
dirty = 1;
@ -2711,7 +2712,7 @@ static void draw_main(void)
if (curwin->scroll > maxscroll) curwin->scroll = maxscroll;
}
for (int r = 0; r < h; r++) {
int idx = curwin->scroll + (h - 1 - r);
long long idx = (long long)curwin->scroll + h - 1 - r;
if (idx >= 0 && idx < nr && rows[idx])
draw_irc_text(chat_top + header + r, left, rows[idx], ww);
}
@ -2951,7 +2952,7 @@ static const char *find_config(void)
static void print_usage(FILE *out, const char *program)
{
fprintf(out, "usage: %s [-h|--help] [-v|--version] [-s|--silent] "
"[-C|--colored] [-c|--color RRGGBB] [config]\n", program);
"[-C|--colored] [-c|--color RRGGBB] [--scrollback LINES] [config]\n", program);
}
int main(int argc, char **argv)
@ -2980,6 +2981,17 @@ int main(int argc, char **argv)
return 1;
}
accent_color = strtol(argv[i], NULL, 16);
} else if (options && strcmp(arg, "--scrollback") == 0) {
const char *value = ++i < argc ? argv[i] : "";
errno = 0;
long limit = strtol(value, NULL, 10);
if (!*value || value[strspn(value, "0123456789")] || errno == ERANGE ||
limit < 1 || limit > INT_MAX / MAXWRAPROWS) {
fprintf(stderr, "irc9: --scrollback requires a line count from 1 to %d\n",
INT_MAX / MAXWRAPROWS);
return 1;
}
scrollback_limit = (int)limit;
} else if (!path && (!options || arg[0] != '-' || arg[1] == 0)) {
path = arg;
} else {