commit 2781882110e050a829117c42edf73f6af8bff72c
parent 0e9f8555196c767e77768b3f331111ee60706baf
Author: hhvn <dev@hhvn.uk>
Date: Tue, 28 Dec 2021 16:28:18 +0000
s/ui.c s/commands.c s/hirc.h: generic fetching of buffers
Diffstat:
3 files changed, 20 insertions(+), 14 deletions(-)
diff --git a/src/commands.c b/src/commands.c
@@ -750,7 +750,8 @@ command_select(struct Server *server, char *str) {
buf = atoi(str);
if (!buf)
ui_error("invalid buffer index: '%s'", str);
- ui_buflist_select(buf);
+ if (ui_buflist_get(buf, &sp, &chp) != -1)
+ ui_select(sp, chp);
} else {
command_toofew("select");
}
diff --git a/src/hirc.h b/src/hirc.h
@@ -129,7 +129,7 @@ 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, int *ret_privs);
-void ui_buflist_select(int num);
+int ui_buflist_get(int num, struct Server **server, struct Channel **chan);
int ui_get_pair(short fg, short bg);
int ui_wprintc(struct Window *window, int lines, char *format, ...);
int ui_strlenc(struct Window *window, char *s, int *lines);
diff --git a/src/ui.c b/src/ui.c
@@ -709,26 +709,28 @@ ui_buflist_count(int *ret_servers, int *ret_channels, int *ret_privs) {
return sc + cc + pc + 1;
}
-void
-ui_buflist_select(int num) {
+int
+ui_buflist_get(int num, struct Server **server, struct Channel **chan) {
struct Server *sp;
struct Channel *chp;
int i;
if (num <= 0) {
ui_error("buffer index greater than 0 expected", NULL);
- return;
+ return -1;
}
if (num == 1) {
- ui_select(NULL, NULL);
- return;
+ *server = NULL;
+ *chan = NULL;
+ return 0;
}
for (i = 2, sp = servers; sp; sp = sp->next) {
if (i == num) {
- ui_select(sp, NULL);
- return;
+ *server = sp;
+ *chan = NULL;
+ return 0;
}
i++; /* increment before moving
to channel section, not
@@ -736,19 +738,22 @@ ui_buflist_select(int num) {
for (chp = sp->channels; chp; chp = chp->next, i++) {
if (i == num) {
- ui_select(sp, chp);
- return;
+ *server = sp;
+ *chan = chp;
+ return 0;
}
}
for (chp = sp->privs; chp; chp = chp->next, i++) {
if (i == num) {
- ui_select(sp, chp);
- return;
+ *server = sp;
+ *chan = chp;
+ return 0;
}
}
}
- ui_error("couldn't select buffer with index %d", num);
+ ui_error("couldn't find buffer with index %d", num);
+ return -1;
}
static char *