From 497c31d9fc0babf02d2bfdfa2f2633ae80cb24f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 29 May 2022 11:11:52 +0200 Subject: [PATCH] commands: scroll up: simplify viewport clamping logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When scrolling the viewport up, we need to ensure we don’t go past the scrollback wrap around. This was previously done using “custom” logic that tried to calculate many rows away from the scrollback start the current viewport is. But this is *exactly* what grid_row_abs_to_sb() does, except it doesn’t account for uninitialized scrollback. So, copy the logic from grid_row_abs_to_sb(), but ensure scrollback start points to valid scrollback data. The maximum number of rows we’re allowed to scroll is now the same as the current viewport’s sb-relative coordinate. Maybe closes #1074 --- commands.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/commands.c b/commands.c index 9d00067d..85e200e2 100644 --- a/commands.c +++ b/commands.c @@ -32,20 +32,12 @@ cmd_scrollback_up(struct terminal *term, int rows) scrollback_start &= grid_rows - 1; } - /* Number of rows to scroll, without going past the scrollback start */ - int max_rows = 0; - if (view + screen_rows >= grid_rows) { - /* View crosses scrollback wrap-around */ - xassert(scrollback_start <= view); - max_rows = view - scrollback_start; - } else { - if (scrollback_start <= view) - max_rows = view - scrollback_start; - else - max_rows = view + (grid_rows - scrollback_start); - } + /* The view row number in scrollback relative coordinates. This is + * the maximum number of rows we’re allowed to scroll */ + int view_sb_rel = view - scrollback_start + grid_rows; + view_sb_rel &= grid_rows - 1; - rows = min(rows, max_rows); + rows = min(rows, view_sb_rel); if (rows == 0) return;