diff --git a/README.md b/README.md index 643ea91..e6b11fd 100644 --- a/README.md +++ b/README.md @@ -190,6 +190,22 @@ At the save prompt, type a path and press Enter, or press Esc or Ctrl-G to cancel. Existing files are protected from overwrites. +## Runtime options + +Type a command starting with `:` in the input line to use a command-line +option inside irc9. Settings apply to the current session. To send a message +that starts with a literal colon, type two colons. + +| Command | Action | +| --- | --- | +| `:colored` / `:nocolored` | Enable or disable nickname coloring for new lines and tree entries | +| `:silent` / `:nosilent` | Hide or show future channel activity notices | +| `:color RRGGBB` / `:nocolor` | Set the interface accent or restore the default cyan | +| `:scrollback LINES` / `:noscrollback` | Resize scrollback in every window or restore the 500-line default. Reducing the limit discards the oldest lines | +| `:history` / `:nohistory` | Enable or disable saving and browsing slash-command history | +| `:version` | Show the program version in the current window | +| `:help` | List the runtime options in the current window | + ## Commands There is no command for changing settings — edit the config file and restart diff --git a/irc9.c b/irc9.c index 19c488b..a7612be 100644 --- a/irc9.c +++ b/irc9.c @@ -1988,6 +1988,9 @@ static void history_add(const char *s) static void history_load(void) { + static int loaded; + if (loaded) return; + const char *home = getenv("HOME"); if (!home || !*home || snprintf(history_path, sizeof history_path, "%s/.irc9_history", home) @@ -1997,6 +2000,7 @@ static void history_load(void) return; } + loaded = 1; FILE *file = fopen(history_path, "r"); if (!file) { if (errno != ENOENT) @@ -2422,6 +2426,80 @@ static void do_command(Window *window, char *line) } } +static int valid_accent_color(const char *value) +{ + return value && strspn(value, "0123456789abcdefABCDEF") == 6 && value[6] == 0; +} + +static int parse_scrollback_limit(const char *value) +{ + if (!value || !*value || value[strspn(value, "0123456789")]) return 0; + errno = 0; + long limit = strtol(value, NULL, 10); + if (errno == ERANGE || limit < 1 || limit > INT_MAX / MAXWRAPROWS) return 0; + return (int)limit; +} + +static void resize_scrollback(int limit) +{ + if (limit == scrollback_limit) return; + for (Server *s = servers; s; s = s->next) { + for (Window *w = s->windows; w; w = w->next) { + int discard = w->nlines > limit ? w->nlines - limit : 0; + ScrollLine *lines = xalloc_array(limit, sizeof *lines); + for (int i = 0; i < discard; i++) free(w->lines[i].text); + w->nlines -= discard; + memcpy(lines, w->lines + discard, (size_t)w->nlines * sizeof *lines); + free(w->lines); + w->lines = lines; + if (w->scroll > limit * MAXWRAPROWS) w->scroll = limit * MAXWRAPROWS; + } + } + scrollback_limit = limit; +} + +static void do_option_command(Window *w, char *command) +{ + char *name = next_word(&command); + char *value = next_word(&command); + if (!w || !name) return; + if (next_word(&command) || + (value && strcmp(name, "color") != 0 && strcmp(name, "scrollback") != 0)) { + appendf(w, "* unexpected arguments for :%s", name); + return; + } + + if (strcmp(name, "help") == 0 || strcmp(name, "version") == 0) { + appendf(w, "irc9 " IRC9_VERSION); + if (strcmp(name, "help") == 0) { + appendf(w, ":colored / :nocolored, :silent / :nosilent"); + appendf(w, ":color RRGGBB / :nocolor, :history / :nohistory"); + appendf(w, ":scrollback LINES / :noscrollback, :version, :help"); + appendf(w, "Use /help for IRC commands and :: to send a leading colon."); + } + } else if (strcmp(name, "colored") == 0 || strcmp(name, "nocolored") == 0) { + colored_nicks = name[0] != 'n'; + } else if (strcmp(name, "silent") == 0 || strcmp(name, "nosilent") == 0) { + silent = name[0] != 'n'; + } else if (strcmp(name, "history") == 0 || strcmp(name, "nohistory") == 0) { + history_enabled = name[0] != 'n'; + if (history_enabled) history_load(); + } else if (strcmp(name, "color") == 0) { + if (valid_accent_color(value)) accent_color = strtol(value, NULL, 16); + else appendf(w, "* :color requires a six-digit hexadecimal color (RRGGBB)"); + } else if (strcmp(name, "nocolor") == 0) { + accent_color = -1; + } else if (strcmp(name, "scrollback") == 0) { + int limit = parse_scrollback_limit(value); + if (limit) resize_scrollback(limit); + else appendf(w, "* :scrollback requires a line count from 1 to %d", INT_MAX / MAXWRAPROWS); + } else if (strcmp(name, "noscrollback") == 0) { + resize_scrollback(MAXSCROLL); + } else { + appendf(w, "* unknown setting: :%s (see :help)", name); + } +} + static void submit_input(void) { char line[MAXINPUT]; @@ -2431,13 +2509,16 @@ static void submit_input(void) if (*line == '/') { if (history_enabled) history_record(line); do_command(curwin, line); + } else if (line[0] == ':' && line[1] != ':') { + do_option_command(curwin, line + 1); } else if (*line) { + const char *message = line[0] == ':' ? line + 1 : line; Window *t = input_target(curwin); if (!t) { if (curwin) appendf(curwin, "* no target here; use /join or /msg"); } else { - sendf(t->srv, "PRIVMSG %s :%s", t->name, line); - append_nickf(t, "<", t->srv->nick, "> ", "%s", line); + sendf(t->srv, "PRIVMSG %s :%s", t->name, message); + append_nickf(t, "<", t->srv->nick, "> ", "%s", message); } } @@ -3305,8 +3386,7 @@ int main(int argc, char **argv) } else if (options && (strcmp(arg, "-C") == 0 || strcmp(arg, "--colored") == 0)) { colored_nicks = 1; } else if (options && (strcmp(arg, "-c") == 0 || strcmp(arg, "--color") == 0)) { - if (++i == argc || strlen(argv[i]) != 6 || - strspn(argv[i], "0123456789abcdefABCDEF") != 6) { + if (++i == argc || !valid_accent_color(argv[i])) { fprintf(stderr, "irc9: %s requires a six-digit hexadecimal color (RRGGBB)\n", arg); return 1; } @@ -3314,16 +3394,13 @@ int main(int argc, char **argv) } 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; - long limit = strtol(value, NULL, 10); - if (!*value || value[strspn(value, "0123456789")] || errno == ERANGE || - limit < 1 || limit > INT_MAX / MAXWRAPROWS) { + int limit = parse_scrollback_limit(++i < argc ? argv[i] : NULL); + if (!limit) { fprintf(stderr, "irc9: --scrollback requires a line count from 1 to %d\n", INT_MAX / MAXWRAPROWS); return 1; } - scrollback_limit = (int)limit; + scrollback_limit = limit; } else if (!path && (!options || arg[0] != '-' || arg[1] == 0)) { path = arg; } else {