1
0
Fork 0
forked from murx/irc9

Wrap channel topics across multiple lines

This commit is contained in:
Johannes Findeisen 2026-11-27 23:25:42 +01:00
commit 7f14513bcd
2 changed files with 38 additions and 20 deletions

55
irc9.c
View file

@ -48,7 +48,8 @@
#define MAXINPUT 512
#define MAXHIST 64
#define MAXSCROLL 500
#define MAXSCROLLROWS (MAXSCROLL * 128) /* push_wrapped emits at most 128 rows per line */
#define MAXWRAPROWS 128
#define MAXSCROLLROWS (MAXSCROLL * MAXWRAPROWS)
#define TREEW 24
#define RETRY 5
@ -2053,10 +2054,7 @@ static void edit_line(int c, char text[MAXINPUT], int *len, int *pos)
}
}
static int topic_rows(void)
{
return curwin && curwin->type == W_CHANNEL && mainh > 0 && cols - treew >= 3;
}
static int layout_topic(int draw);
static void handle_key(int c)
{
@ -2083,7 +2081,7 @@ static void handle_key(int c)
if (c == 0x0e) { switch_window(1); return; } /* ctrl-n */
if (c == 0x10) { switch_window(-1); return; } /* ctrl-p */
if (c == 0x0c) { /* ctrl-l */
if (curwin) { curwin->scroll = -(mainh - topic_rows()); dirty = 1; }
if (curwin) { curwin->scroll = -(mainh - layout_topic(0)); dirty = 1; }
return;
}
@ -2094,7 +2092,7 @@ static void handle_key(int c)
if (c == KEY_DOWN) { history_browse(1); dirty = 1; return; }
if (c == KEY_PPAGE) {
if (curwin) {
int height = mainh - topic_rows();
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)
@ -2107,7 +2105,7 @@ static void handle_key(int c)
}
if (c == KEY_NPAGE) {
if (curwin) {
int height = mainh - topic_rows();
int height = mainh - layout_topic(0);
curwin->scroll -= height > 1 ? height - 1 : 1;
if (curwin->scroll < 0) curwin->scroll = 0;
dirty = 1;
@ -2235,7 +2233,7 @@ static void push_wrapped(char **rows, int *nr, int maxrows, const char *s, int w
if (*nr >= maxrows) return;
char *copy = dupstr(s);
char *segs[128];
char *segs[MAXWRAPROWS];
/* Tokens plus the single spaces we reinsert never exceed the source line. */
char *cur = xmalloc(size_add(strlen(s), 2));
size_t clen = 0;
@ -2250,7 +2248,7 @@ static void push_wrapped(char **rows, int *nr, int maxrows, const char *s, int w
while (tlvis > width) {
int used;
size_t b = ircfmt_bytes_for_vis(tok, width, &used);
if (nseg < 128) segs[nseg++] = dupnstr(tok, b);
if (nseg < MAXWRAPROWS) segs[nseg++] = dupnstr(tok, b);
tok += b; tlvis -= used; tlbytes -= b;
}
memcpy(cur, tok, tlbytes); cur[tlbytes] = 0; clen = tlbytes; curvis = tlvis;
@ -2261,18 +2259,18 @@ static void push_wrapped(char **rows, int *nr, int maxrows, const char *s, int w
cur[clen] = 0;
curvis += 1 + tlvis;
} else {
if (nseg < 128) segs[nseg++] = dupstr(cur);
if (nseg < MAXWRAPROWS) segs[nseg++] = dupstr(cur);
while (tlvis > width) {
int used;
size_t b = ircfmt_bytes_for_vis(tok, width, &used);
if (nseg < 128) segs[nseg++] = dupnstr(tok, b);
if (nseg < MAXWRAPROWS) segs[nseg++] = dupnstr(tok, b);
tok += b; tlvis -= used; tlbytes -= b;
}
memcpy(cur, tok, tlbytes); cur[tlbytes] = 0; clen = tlbytes; curvis = tlvis;
}
}
if (clen || nseg == 0) {
if (nseg < 128) segs[nseg++] = dupstr(cur);
if (nseg < MAXWRAPROWS) segs[nseg++] = dupstr(cur);
}
for (int i = nseg - 1; i >= 0; i--) {
if (*nr < maxrows) rows[(*nr)++] = segs[i];
@ -2430,18 +2428,37 @@ static void draw_title(void)
fputs("\033[0m", stdout);
}
/* Use the same topic height for drawing and paging the chat pane. */
static int layout_topic(int draw)
{
int width = cols - treew - 2;
if (!curwin || curwin->type != W_CHANNEL || mainh < 1 || width < 1)
return 0;
char topic[MAXLINE + sizeof "Topic: "];
snprintf(topic, sizeof topic, "Topic: %s",
curwin->topic[0] ? curwin->topic : "(no topic)");
char *rows[MAXWRAPROWS];
int count = 0;
push_wrapped(rows, &count, MAXWRAPROWS, topic, width);
int limit = mainh > 1 ? mainh - 1 : 1;
int height = count < limit ? count : limit;
for (int row = 0; row < count; row++) {
char *text = rows[count - 1 - row];
if (draw && row < height) draw_irc_text(row + 1, treew + 1, text, width);
free(text);
}
return height;
}
static void draw_main(void)
{
int width = cols - treew;
if (curwin && mainh > 0 && width >= 3) {
int ww = width - 2;
int header = topic_rows();
if (header) {
const char *topic = curwin->topic[0] ? curwin->topic : "(no topic)";
draw_irc_text(1, treew + 1, "Topic:", ww);
if (ww > 7) draw_irc_text(1, treew + 8, topic, ww - 7);
}
int header = layout_topic(1);
int h = mainh - header;
if (h == 0) return;
long long need_ll = (long long)h + curwin->scroll;