From 13fa1bf108cb35f5a0b9b7228a24b438cd9182d6 Mon Sep 17 00:00:00 2001 From: "jabber.developer2" Date: Sat, 20 Jun 2026 11:40:57 +0300 Subject: [PATCH] fix(ui): reserve pad height per message to prevent multi-line clip and scroll desync A tall multi-line message printed near the bottom of the ncurses pad was clipped because _win_print_internal grew the pad from the current cursor only (_win_ensure_pad_capacity(getcury)), not the height of the message about to be printed. The clipped message's captured height (e.g. 1 row for a 49-row message) was wrong while win_redraw later rendered it in full; the resulting buffer->lines / y_start_pos mismatch desynced the page-up scroll anchor, producing a ~one-page jump when scrolling past such messages from history. Estimate the rendered height (hard newlines + soft-wrap over the usable width) and reserve that many rows before printing, so the message is never clipped and its captured height matches the redraw. All print paths go through _win_print_internal, so incoming/outgoing/history are covered. Regression from the upstream sync (72f4f186d), which replaced the fixed-size pad with the dynamic _win_ensure_pad_capacity model. --- src/ui/window.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/ui/window.c b/src/ui/window.c index f0ec5bbb..0f6e2780 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -67,6 +67,20 @@ _win_ensure_pad_capacity(ProfWin* window, WINDOW* win, int lines_needed) } } } + +// upper-bound estimate of rows a message body occupies (hard newlines + soft-wrap), to reserve pad space before printing so a tall message isn't clipped (a clipped height desyncs the scroll offset) +static int +_win_estimated_lines(WINDOW* win, const char* const message, int indent, int pad_indent) +{ + int usable = MAX(1, getmaxx(win) - (indent + pad_indent)); + int newlines = 0; + for (const char* p = message; *p; p++) { + if (*p == '\n') { + newlines++; + } + } + return newlines + utf8_display_len(message) / usable + 2; +} static const char* LOADING_MESSAGE = "Loading older messages…"; static const char* CONS_WIN_TITLE = "CProof. Type /help for help information."; static const char* XML_WIN_TITLE = "XML Console"; @@ -2001,7 +2015,7 @@ _win_print_internal(ProfWin* window, const char* show_char, int pad_indent, GDat } } - _win_ensure_pad_capacity(window, window->layout->win, getcury(window->layout->win)); + _win_ensure_pad_capacity(window, window->layout->win, getcury(window->layout->win) + _win_estimated_lines(window->layout->win, message + offset, indent, pad_indent)); if (prefs_get_boolean(PREF_WRAP)) { _win_print_wrapped(window->layout->win, message + offset, indent, pad_indent);