Clamp scroll_to_prompt offsets to available scrollback

Keep the viewport within the history buffer and remember the matched prompt when it is visible so last-visited output follows the jump.
This commit is contained in:
Zhizhen He 2026-11-11 21:18:20 +08:00
parent bb237bf137
commit 96ddbed7f6
2 changed files with 30 additions and 5 deletions

View file

@ -3789,9 +3789,10 @@ screen_history_scroll_to_prompt(Screen *self, int num_of_prompts_to_jump, int sc
} else {
int delta = num_of_prompts_to_jump < 0 ? -1 : 1;
num_of_prompts_to_jump = num_of_prompts_to_jump < 0 ? -num_of_prompts_to_jump : num_of_prompts_to_jump;
int y = -self->scrolled_by;
const int oldest_y = -(int)self->historybuf->count;
int y = -(int)self->scrolled_by;
#define ensure_y_ok \
if (y >= (int)self->lines || -y > (int)self->historybuf->count) return false;
if (y >= (int)self->lines || y < oldest_y) return false;
ensure_y_ok;
y += scroll_offset;
while (num_of_prompts_to_jump) {
@ -3799,10 +3800,12 @@ screen_history_scroll_to_prompt(Screen *self, int num_of_prompts_to_jump, int sc
ensure_y_ok;
if (range_line_(self, y)->attrs.prompt_kind == PROMPT_START) { num_of_prompts_to_jump--; }
}
y -= scroll_offset;
#undef ensure_y_ok
self->scrolled_by = y >= 0 ? 0 : -y;
screen_set_last_visited_prompt(self, 0);
const int viewport_top = MAX(oldest_y, MIN(0, y - scroll_offset));
self->scrolled_by = -viewport_top;
const int prompt_y = y - viewport_top;
// The context above the prompt can include another command's output.
screen_set_last_visited_prompt(self, prompt_y >= 0 && prompt_y < (int)self->lines ? prompt_y : 0);
}
if (old != self->scrolled_by) {
reset_pixel_scroll(self, 0);

View file

@ -2353,6 +2353,28 @@ class TestScreen(BaseTest):
s.draw('a foo')
ae(s, 'foo', 2)
def test_scroll_to_prompt_with_short_scrollback(self):
for lines_before_prompt in (0, 1):
with self.subTest(lines_before_prompt=lines_before_prompt):
s = self.create_screen(cols=12, lines=4, scrollback=10)
if lines_before_prompt:
parse_bytes(s, b'\x1b]133;C\x07older\r\n')
parse_bytes(s, b'\x1b]133;A\x07$ a\r\n')
parse_bytes(s, b'\x1b]133;C\x07a0\r\n\r\n\r\n')
parse_bytes(s, b'\x1b]133;A\x07$ live')
self.ae(s.historybuf.count, lines_before_prompt + 1)
self.assertTrue(s.scroll_to_prompt(-1, 3))
self.ae(s.scrolled_by, s.historybuf.count)
s.scroll_to_absolute(0)
output = []
s.cmd_output(2, output.append)
self.ae(''.join(output).rstrip(), 'a0')
self.assertTrue(s.scroll_to_prompt(0))
self.ae(s.scrolled_by, s.historybuf.count)
self.ae(str(s.visual_line(lines_before_prompt)), '$ a')
def test_prompt_marking(self):
# ]]]]]]]]]]]]]]]]}}}}}}}}}}}}}}}}))))))))))))))))))))))