Add opt-in persistent slash command history

This commit is contained in:
Johannes Findeisen 2026-11-30 21:57:06 +01:00
commit 172157825b
2 changed files with 66 additions and 10 deletions

View file

@ -28,6 +28,8 @@ 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.
Use `--history` to save submitted slash commands to `~/.irc9_history` and
recall the 64 most recent commands with Up and Down across sessions.
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
@ -167,7 +169,7 @@ MODE, TOPIC, and INVITE events are shown in the relevant window.
| `ctrl-l` | clear the screen (history stays scrollable) |
| `ctrl-t` | toggle copy view, which hides the tree, chat timestamps, and sender names |
| `ctrl-s` | save the current window’s scrollback to a new plain-text file |
| Up / Down | command history |
| Up / Down | browse slash command history with `--history` |
| Left / Right, Home / End | edit the input line |
| `Del` | delete the character at the cursor |

72
irc9.c
View file

@ -212,6 +212,8 @@ 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 history_enabled;
static char history_path[PATH_MAX];
static int comp_active, comp_index;
static char comp_prefix[MAXINPUT], comp_last[NICKLEN];
#define MAXRECENT 16
@ -1809,7 +1811,7 @@ static void read_server(Server *s)
static void history_add(const char *s)
{
if (!*s) return;
if (*s != '/') return;
if (hist_n == MAXHIST) {
for (int i = 1; i < MAXHIST; i++)
memcpy(history[i - 1], history[i], sizeof history[i]);
@ -1819,15 +1821,63 @@ static void history_add(const char *s)
hist_cur = -1;
}
static void history_browse(int dir)
static void history_load(void)
{
if (dir > 0 && hist_cur < 0) {
/* Down on a freshly typed line saves it, then clears the input. */
history_add(input);
input[0] = 0;
input_len = input_pos = 0;
const char *home = getenv("HOME");
if (!home || !*home ||
snprintf(history_path, sizeof history_path, "%s/.irc9_history", home)
>= (int)sizeof history_path) {
history_path[0] = 0;
fprintf(stderr, "irc9: cannot locate ~/.irc9_history\n");
return;
}
FILE *file = fopen(history_path, "r");
if (!file) {
if (errno != ENOENT)
fprintf(stderr, "irc9: cannot read %s: %s\n", history_path, strerror(errno));
return;
}
char *line = NULL;
size_t capacity = 0;
ssize_t length;
while ((length = getline(&line, &capacity, file)) >= 0) {
if (length && line[length - 1] == '\n') line[--length] = 0;
if (length < MAXINPUT && !memchr(line, 0, (size_t)length)) history_add(line);
}
if (!feof(file))
fprintf(stderr, "irc9: cannot read %s: %s\n", history_path, strerror(errno));
free(line);
fclose(file);
}
static void history_record(const char *line)
{
history_add(line);
if (!*history_path) return;
int fd = open(history_path, O_WRONLY | O_CREAT | O_APPEND, 0600);
int error = 0;
if (fd < 0) {
error = errno;
} else {
char entry[MAXINPUT + 1];
int length = snprintf(entry, sizeof entry, "%s\n", line);
ssize_t written;
do {
written = write(fd, entry, (size_t)length);
} while (written < 0 && errno == EINTR);
if (written != length) error = written < 0 ? errno : EIO;
if (close(fd) < 0 && !error) error = errno;
}
if (error && curwin)
appendf(curwin, "* cannot write %s: %s", history_path, strerror(error));
}
static void history_browse(int dir)
{
if (!history_enabled || (dir > 0 && hist_cur < 0)) return;
if (hist_n == 0) return;
if (hist_cur < 0) {
/* dir < 0: start at the most recent entry. */
@ -2210,9 +2260,10 @@ static void submit_input(void)
{
char line[MAXINPUT];
snprintf(line, sizeof line, "%s", input);
history_add(line);
hist_cur = -1;
if (*line == '/') {
if (history_enabled) history_record(line);
do_command(curwin, line);
} else if (*line) {
Window *t = input_target(curwin);
@ -3008,7 +3059,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] [--scrollback LINES] [config]\n", program);
"[-C|--colored] [-c|--color RRGGBB] [--scrollback LINES] [--history] [config]\n", program);
}
int main(int argc, char **argv)
@ -3037,6 +3088,8 @@ int main(int argc, char **argv)
return 1;
}
accent_color = strtol(argv[i], NULL, 16);
} else if (options && strcmp(arg, "--history") == 0) {
history_enabled = 1;
} else if (options && strcmp(arg, "--scrollback") == 0) {
const char *value = ++i < argc ? argv[i] : "";
errno = 0;
@ -3063,6 +3116,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
load_config(path);
if (history_enabled) history_load();
#ifdef USE_OPENSSL
tls_ctx = SSL_CTX_new(TLS_client_method());