diff options
| author | Mofan552 <71218481+mofan552@users.noreply.github.com> | 2026-07-17 18:06:57 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-07-17 11:06:57 +0200 |
| commit | 6b967baacc86cb24b3fd55e445c833b28f733c0c (patch) | |
| tree | ecab55939483551ba4d88fc2e7f191887028226e | |
| parent | docs(changelog): allow disabling ctrl-mouse-scroll (diff) | |
| download | sl0ppij-6b967baacc86cb24b3fd55e445c833b28f733c0c.tar.gz sl0ppij-6b967baacc86cb24b3fd55e445c833b28f733c0c.tar.xz sl0ppij-6b967baacc86cb24b3fd55e445c833b28f733c0c.zip | |
fix(grid): scroll the region when a line wraps at its bottom margin (#5357)
* fix(grid): scroll the region when a line wraps at its bottom margin
Wrapping at the bottom margin of a DECSTBM scroll region used to move
the cursor below the region instead of scrolling it, after which no
subsequent output could scroll through the region (or reach scrollback
for top-anchored regions) anymore.
line_wrap() now mirrors add_canonical_line(): the region scrolls up one
line, the cursor stays on its bottom row, and the new row is a wrapped
(non-canonical) row. Top-anchored partial regions preserve the
scrolled-off line in scrollback (#4941), other regions discard it, and
the alternate screen never transfers.
* docs(changelog): line wrap scroll region fix
* fix(grid): keep hyperlink tracking aligned when wrap scrolls an inner region
When a line wraps at the bottom of a scroll region that does not start
at the top of the screen, the region's rows shift up but the hyperlink
tracker's recorded positions did not, so a URL wrapping there lost its
clickable anchors. Offset the tracked positions for the rows inside the
region, the way the full-screen and top-anchored paths already do via
offset_cursor_lines.
* refactor shared logic and adjust changelog
---------
Co-authored-by: Aram Drevekenin <aram@poor.dev>
| -rw-r--r-- | CHANGELOG.md | 1 | ||||
| -rw-r--r-- | zellij-server/src/panes/grid.rs | 103 | ||||
| -rw-r--r-- | zellij-server/src/panes/hyperlink_tracker.rs | 21 | ||||
| -rw-r--r-- | zellij-server/src/panes/unit/grid_tests.rs | 160 |
4 files changed, 238 insertions, 47 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 950a0899..bc054c12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) * feat: allow opening panes/tabs from the CLI without changing focus (https://github.com/zellij-org/zellij/pull/5346) * feat: allow binding `SetDarkTheme`, `SetLightTheme` and `ToggleTheme` as direct keybinding actions (https://github.com/zellij-org/zellij/pull/5302) * feat: allow disabling ctrl-mouse-scroll to resize panes (https://github.com/zellij-org/zellij/pull/5283) +* fix: scrolling region line wrap behavior at bottom edge (https://github.com/zellij-org/zellij/pull/5357) ## [0.44.3] - 2026-05-13 * fix(windows): bump windows-sys to 0.59 to align manifest with code, fixing source builds via `cargo install`/`cargo binstall` (https://github.com/zellij-org/zellij/pull/5139) diff --git a/zellij-server/src/panes/grid.rs b/zellij-server/src/panes/grid.rs index 0a659849..5be48d34 100644 --- a/zellij-server/src/panes/grid.rs +++ b/zellij-server/src/panes/grid.rs @@ -1785,6 +1785,50 @@ impl Grid { } self.output_buffer.update_all_lines(); } + fn scroll_region_up_at_bottom(&mut self, new_row: Row, offset_hyperlinks: bool) { + let (scroll_region_top, scroll_region_bottom) = self.scroll_region; + if scroll_region_top >= self.viewport.len() { + return; + } + if scroll_region_bottom == self.height.saturating_sub(1) && scroll_region_top == 0 { + if self.alternate_screen_state.is_none() { + self.transfer_rows_to_lines_above(1); + if offset_hyperlinks { + self.hyperlink_tracker.offset_cursor_lines(1); + } + } else if !self.viewport.is_empty() { + self.viewport.pop_front(); + } + self.viewport.push_back(new_row); + self.selection.move_up(1); + } else { + if scroll_region_top == 0 + && self.alternate_screen_state.is_none() + && !self.viewport.is_empty() + { + self.transfer_rows_to_lines_above(1); + if offset_hyperlinks { + self.hyperlink_tracker.offset_cursor_lines(1); + } + self.selection.move_up(1); + } else if scroll_region_top < self.viewport.len() { + self.viewport.remove(scroll_region_top); + if offset_hyperlinks { + self.hyperlink_tracker.offset_cursor_lines_in_range( + scroll_region_top as isize, + scroll_region_bottom as isize, + 1, + ); + } + } + if self.viewport.len() >= scroll_region_bottom { + self.viewport.insert(scroll_region_bottom, new_row); + } else { + self.viewport.push_back(new_row); + } + } + self.output_buffer.update_all_lines(); + } pub fn add_canonical_line(&mut self) { let (scroll_region_top, scroll_region_bottom) = self.scroll_region; self.hyperlink_tracker.update( @@ -1795,47 +1839,12 @@ impl Grid { &mut self.link_handler.borrow_mut(), ); if self.cursor.y == scroll_region_bottom { - // end of scroll region - // when we have a scroll region set and we're at its bottom - // we need to delete its first line, thus shifting all lines in it upwards - // then we add an empty line at its end which will be filled by the application - // controlling the scroll region (presumably filled by whatever comes next in the - // scroll buffer, but that's not something we control) if scroll_region_top >= self.viewport.len() { - // the state is corrupted return; } let scroll_bg = self.cursor.pending_styles.background; - if scroll_region_bottom == self.height.saturating_sub(1) && scroll_region_top == 0 { - if self.alternate_screen_state.is_none() { - self.transfer_rows_to_lines_above(1); - } else if !self.viewport.is_empty() { - self.viewport.pop_front(); - } - - self.viewport - .push_back(Row::new().canonical().with_bg_color(scroll_bg)); - self.selection.move_up(1); - } else { - if scroll_region_top == 0 - && self.alternate_screen_state.is_none() - && !self.viewport.is_empty() - { - // Partial scroll region starting at top: preserve - // scrolled-off lines in scrollback - self.transfer_rows_to_lines_above(1); - self.selection.move_up(1); - } else if scroll_region_top < self.viewport.len() { - self.viewport.remove(scroll_region_top); - } - let new_row = Row::new().canonical().with_bg_color(scroll_bg); - if self.viewport.len() >= scroll_region_bottom { - self.viewport.insert(scroll_region_bottom, new_row); - } else { - self.viewport.push_back(new_row); - } - } - self.output_buffer.update_all_lines(); // TODO: only update scroll region lines + let new_row = Row::new().canonical().with_bg_color(scroll_bg); + self.scroll_region_up_at_bottom(new_row, false); return; } if self.viewport.len() <= self.cursor.y + 1 { @@ -1993,17 +2002,17 @@ impl Grid { } fn line_wrap(&mut self) { self.cursor.x = 0; - if self.cursor.y == self.height.saturating_sub(1) { - if self.alternate_screen_state.is_none() { - self.transfer_rows_to_lines_above(1); - self.hyperlink_tracker.offset_cursor_lines(1); - } else if !self.viewport.is_empty() { - self.viewport.pop_front(); + let (scroll_region_top, scroll_region_bottom) = self.scroll_region; + if self.cursor.y == scroll_region_bottom { + if scroll_region_top >= self.viewport.len() { + return; } - let wrapped_row = Row::new(); - self.viewport.push_back(wrapped_row); - self.selection.move_up(1); - self.output_buffer.update_all_lines(); + self.scroll_region_up_at_bottom(Row::new(), true); + } else if self.cursor.y == self.height.saturating_sub(1) { + // the cursor is on the last line of the screen but below the scroll region's + // bottom margin: there is nowhere to scroll to, so we wrap onto the same line + // (mirroring what add_canonical_line does in this situation) + self.output_buffer.update_line(self.cursor.y); } else { self.cursor.y += 1; if self.viewport.len() <= self.cursor.y { diff --git a/zellij-server/src/panes/hyperlink_tracker.rs b/zellij-server/src/panes/hyperlink_tracker.rs index 56bcfcdb..a8bd8666 100644 --- a/zellij-server/src/panes/hyperlink_tracker.rs +++ b/zellij-server/src/panes/hyperlink_tracker.rs @@ -86,6 +86,27 @@ impl HyperlinkTracker { self.last_cursor = Some(current_pos); } + pub fn offset_cursor_lines_in_range(&mut self, top: isize, bottom: isize, offset: isize) { + // Offset only positions inside the given row range (a scroll region), + // used when a scroll region that does not start at the top of the + // screen scrolls: rows outside it do not move + for pos in &mut self.cursor_positions { + if pos.y >= top && pos.y <= bottom { + pos.y -= offset; + } + } + if let Some(start_pos) = &mut self.start_position { + if start_pos.y >= top && start_pos.y <= bottom { + start_pos.y -= offset; + } + } + if let Some(last_cursor) = &mut self.last_cursor { + if last_cursor.y >= top && last_cursor.y <= bottom { + last_cursor.y -= offset; + } + } + } + pub fn offset_cursor_lines(&mut self, offset: isize) { // Offset all stored cursor positions for pos in &mut self.cursor_positions { diff --git a/zellij-server/src/panes/unit/grid_tests.rs b/zellij-server/src/panes/unit/grid_tests.rs index 099a3ca8..ac697956 100644 --- a/zellij-server/src/panes/unit/grid_tests.rs +++ b/zellij-server/src/panes/unit/grid_tests.rs @@ -5612,6 +5612,166 @@ fn partial_scroll_region_all_three_mechanisms_transfer_in_order() { } #[test] +fn wrap_at_bottom_of_partial_scroll_region_transfers_to_scrollback() { + // A long line that soft-wraps while the cursor is on the scroll region's + // bottom row must scroll the region, exactly like a newline there would + let mut content: Vec<u8> = Vec::new(); + content.extend_from_slice(PARTIAL_SR); + content.extend_from_slice(FILL_8_LINES); + content.extend_from_slice(b"\r\n"); + content.extend_from_slice(&[b'I'; 90]); // 90 cols on a 40-col grid: wraps twice + + let grid = create_grid_with_size_and_raw(10, 40, &content); + + // the newline scrolls AAA off, the two wraps scroll BBB and CCC off + assert_eq!(scrollback_texts(&grid), vec!["AAA", "BBB", "CCC"]); + let vp = viewport_texts(&grid); + assert_eq!(vp[0], "DDD"); + assert_eq!(vp[4], "HHH"); + assert_eq!(vp[5], "I".repeat(40)); + assert_eq!(vp[6], "I".repeat(40)); + assert_eq!(vp[7], "I".repeat(10)); + assert_eq!(vp.len(), 8, "rows below the region must be untouched"); + // the continuation rows are wrapped rows, so resize re-wraps them correctly + assert!(grid.viewport[5].is_canonical); + assert!(!grid.viewport[6].is_canonical); + assert!(!grid.viewport[7].is_canonical); +} + +#[test] +fn wrap_at_bottom_of_partial_scroll_region_keeps_cursor_inside_region() { + // Before the fix, the cursor would escape below the region's bottom margin + // on wrap, and no subsequent line could ever scroll into scrollback again + let mut content: Vec<u8> = Vec::new(); + content.extend_from_slice(PARTIAL_SR); + content.extend_from_slice(FILL_8_LINES); + content.extend_from_slice(b"\r\n"); + content.extend_from_slice(&[b'I'; 90]); + + let grid = create_grid_with_size_and_raw(10, 40, &content); + + // region is rows 0-7; after two wraps the cursor must still be on row 7 + assert_eq!( + grid.cursor_coordinates().map(|(x, y, _)| (x, y)), + Some((10, 7)) + ); +} + +#[test] +fn wrap_at_bottom_of_nonzero_top_scroll_region_does_not_transfer() { + let mut content: Vec<u8> = Vec::new(); + content.extend_from_slice(b"AAA\r\nBBB\r\nCCC\r\nDDD\r\nEEE\r\nFFF"); + // Scroll region rows 3-6 (1-based), so top is row 2, not 0 + content.extend_from_slice(b"\x1b[3;6r"); + content.extend_from_slice(b"\x1b[6;1H"); + content.extend_from_slice(&[b'X'; 50]); // wraps once at the region's bottom + + let grid = create_grid_with_size_and_raw(10, 40, &content); + + // regions not anchored at the top do not preserve scrolled-off lines + assert_eq!(grid.lines_above.len(), 0); + let vp = viewport_texts(&grid); + assert_eq!(vp[0], "AAA", "rows above the region must be untouched"); + assert_eq!(vp[1], "BBB", "rows above the region must be untouched"); + assert_eq!(vp[2], "DDD", "the region's top row (CCC) is discarded"); + assert_eq!(vp[4], "X".repeat(40)); + assert_eq!(vp[5], "X".repeat(10)); +} + +#[test] +fn wrap_at_bottom_of_partial_scroll_region_does_not_transfer_on_alternate_screen() { + let mut content: Vec<u8> = Vec::new(); + content.extend_from_slice(b"\x1b[?1049h"); + content.extend_from_slice(PARTIAL_SR); + content.extend_from_slice(FILL_8_LINES); + content.extend_from_slice(b"\r\n"); + content.extend_from_slice(&[b'I'; 90]); + + let grid = create_grid_with_size_and_raw(10, 40, &content); + + assert_eq!(grid.lines_above.len(), 0); + let vp = viewport_texts(&grid); + assert_eq!(vp[5], "I".repeat(40)); + assert_eq!(vp[6], "I".repeat(40)); + assert_eq!(vp[7], "I".repeat(10)); +} + +#[test] +fn wrap_at_bottom_of_explicit_full_screen_scroll_region_keeps_wrap_flag() { + // CSI 1;10r on a 10-row grid covers the whole screen; wrap behavior must + // be identical to having no region set at all (scroll + wrapped row) + let mut content: Vec<u8> = Vec::new(); + content.extend_from_slice(b"\x1b[1;10r"); + for i in 1..=10u8 { + if i > 1 { + content.extend_from_slice(b"\r\n"); + } + content.extend_from_slice(b"EARLY-"); + content.push(b'0' + i / 10); + content.push(b'0' + i % 10); + } + content.extend_from_slice(b"\r\n"); + content.extend_from_slice(&[b'I'; 90]); // wraps twice on a 40-col grid + + let grid = create_grid_with_size_and_raw(10, 40, &content); + + // the newline and the two wraps each scrolled one line into scrollback + assert_eq!( + scrollback_texts(&grid), + vec!["EARLY-01", "EARLY-02", "EARLY-03"] + ); + let vp = viewport_texts(&grid); + assert_eq!(vp[6], "EARLY-10"); + assert_eq!(vp[7], "I".repeat(40)); + assert_eq!(vp[8], "I".repeat(40)); + assert_eq!(vp[9], "I".repeat(10)); + assert!(grid.viewport[7].is_canonical); + assert!( + !grid.viewport[8].is_canonical, + "continuation must stay a wrapped row" + ); + assert!( + !grid.viewport[9].is_canonical, + "continuation must stay a wrapped row" + ); +} + +#[test] +fn wrap_at_bottom_of_nonzero_top_scroll_region_keeps_hyperlink_tracking() { + // A plain-text URL that wraps at the bottom of a region not anchored at + // the top: the auto-linkifier's tracked positions must follow the rows + // as the region scrolls, or the URL loses its clickable anchors + let mut content: Vec<u8> = Vec::new(); + content.extend_from_slice(b"AAA\r\nBBB\r\nCCC\r\nDDD\r\nEEE\r\nFFF"); + content.extend_from_slice(b"\x1b[3;6r\x1b[6;1H"); + content.extend_from_slice(b"https://example.com/"); + content.extend_from_slice(&[b'a'; 30]); // 50 chars: wraps at the region bottom + content.extend_from_slice(b" "); + + let grid = create_grid_with_size_and_raw(10, 40, &content); + + let start_anchors_per_row: Vec<usize> = grid + .viewport + .iter() + .map(|row| { + row.columns + .iter() + .filter(|c| { + matches!( + c.styles.link_anchor, + Some(crate::panes::terminal_character::LinkAnchor::Start(_)) + ) + }) + .count() + }) + .collect(); + + // rows 0-3: AAA, BBB, DDD, EEE (CCC scrolled off and discarded) - no anchors; + // row 4 holds the URL's first 40 columns, row 5 the wrapped remainder + assert_eq!(start_anchors_per_row, vec![0, 0, 0, 0, 40, 10]); +} + +#[test] fn scroll_region_newline_sets_bg_color_on_new_row() { // Set bg color, set scroll region 1-5, fill 5 lines, then newline to scroll let content = b"\x1b[48;2;26;26;26m\x1b[1;5r\ |
