hirc

IRC client
Log | Files | Refs

commit 3a9642f10e41f2756a85e429ecae619c4033f077
parent a887889285bd98950946ce5d2bf7e3e422be5593
Author: hhvn <dev@hhvn.uk>
Date:   Sat, 27 Nov 2021 22:20:16 +0000

config.c hirc.h ui.c: mapping commands to formats

Diffstat:
Mconfig.c | 5+++++
Mhirc.h | 2++
Mui.c | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
3 files changed, 70 insertions(+), 4 deletions(-)

diff --git a/config.c b/config.c @@ -149,6 +149,11 @@ struct Config config[] = { .strhandle = NULL, .description = { "Format of topic at top of main window", NULL}}, + {"format.privmsg", 1, Val_string, + .str = "${nick} ${2}", + .strhandle = NULL, + .description = { + "Format of messages", NULL}}, {NULL}, }; diff --git a/hirc.h b/hirc.h @@ -99,6 +99,8 @@ void ui_redraw(void); void ui_draw_input(void); void ui_draw_nicklist(void); void ui_draw_buflist(void); +int ui_hist_print(struct Window *window, int lines, struct History *hist); +int ui_hist_len(struct Window *window, struct History *hist, int *lines); void ui_draw_main(void); int ui_buflist_count(int *ret_servers, int *ret_channels); void ui_buflist_select(int num); diff --git a/ui.c b/ui.c @@ -41,13 +41,22 @@ struct Window windows[Win_last] = { [Win_nicklist] = {.handler = ui_draw_nicklist}, [Win_buflist] = {.handler = ui_draw_buflist}, }; -struct Selected selected; + +struct { + char *cmd; + char *format; +} formatmap[] = { + {"PRIVMSG", "format.privmsg"}, + {NULL, NULL}, +}; struct { char string[INPUT_MAX]; unsigned counter; } input; +struct Selected selected; + void ui_error_(char *file, int line, char *format, ...) { char msg[1024]; @@ -688,6 +697,54 @@ ui_wclear(struct Window *window) { wmove(window->window, 0, 0); } +int +ui_hist_print(struct Window *window, int lines, struct History *hist) { + char *cmd; + int i; + + if (!hist) + return -1; + + if (!hist->params || !*(hist->params+1)) + goto raw; + + if (**(hist->params) == ':') + cmd = *(hist->params+1); + else + cmd = *(hist->params); + + for (i=0; formatmap[i].cmd; i++) + if (formatmap[i].format && strcmp_n(formatmap[i].cmd, cmd) == 0) + return ui_wprintc(window, lines, "%s\n", ui_format(config_gets(formatmap[i].format), hist)); + +raw: + return ui_wprintc(window, lines, "%s\n", hist->raw); +} + +int +ui_hist_len(struct Window *window, struct History *hist, int *lines) { + char *cmd; + int i; + + if (!hist) + return -1; + + if (!hist->params || !*(hist->params+2)) + goto raw; + + if (**(hist->params) == ':') + cmd = *(hist->params+2); + else + cmd = *(hist->params); + + for (i=0; formatmap[i].cmd; i++) + if (formatmap[i].format && strcmp_n(formatmap[i].cmd, cmd) == 0) + return ui_strlenc(window, ui_format(config_gets(formatmap[i].format), hist), lines); + +raw: + return ui_strlenc(window, hist->raw, lines); +} + void ui_draw_main(void) { struct History *p; @@ -699,15 +756,17 @@ ui_draw_main(void) { for (p = selected.history->history; p && y > 0; p = p->next) { if (!(p->options & HIST_SHOW)) continue; - ui_strlenc(&windows[Win_main], p->raw, &lines); + if (ui_hist_len(&windows[Win_main], p, &lines) == -1) + continue; y = y - lines; if (y < lines) { y *= -1; wmove(windows[Win_main].window, 0, 0); - ui_wprintc(&windows[Win_main], y, "%s\n", p->raw); + ui_hist_print(&windows[Win_main], y, p); + break; } wmove(windows[Win_main].window, y, 0); - ui_wprintc(&windows[Win_main], 0, "%s\n", p->raw); + ui_hist_print(&windows[Win_main], 0, p); } if (selected.channel && selected.channel->topic) {