From 4b302e8f57d04bc2a83bd65b50af012b4b030a44 Mon Sep 17 00:00:00 2001 From: Funami580 Date: Sat, 14 May 2022 23:03:59 +0200 Subject: [PATCH] rectangular selection: fix selection fixes: * beginning the selection with the top right or bottom left corner * beginning the selection with the bottom right corner and only select a single line --- wezterm-gui/src/selection.rs | 49 ++++++++++++++----------- wezterm-gui/src/termwindow/selection.rs | 36 ++++++++++++------ 2 files changed, 52 insertions(+), 33 deletions(-) diff --git a/wezterm-gui/src/selection.rs b/wezterm-gui/src/selection.rs index 81c7a907d..71fda8673 100644 --- a/wezterm-gui/src/selection.rs +++ b/wezterm-gui/src/selection.rs @@ -219,30 +219,37 @@ impl SelectionRange { /// Must be called on a normalized range! pub fn cols_for_row(&self, row: StableRowIndex, rectangular: bool) -> Range { let norm = self.normalize(); - let range = if row < norm.start.y || row > norm.end.y { - 0..0 - } else if norm.start.y == norm.end.y { - // A single line selection - if norm.start.x <= norm.end.x { - norm.start.x..norm.end.x.saturating_add(1) - } else { - norm.end.x..norm.start.x.saturating_add(1) - } - } else if row == norm.end.y { - // last line of multi-line - 0..norm.end.x.saturating_add(1) - } else if row == norm.start.y { - // first line of multi-line - norm.start.x..usize::max_value() - } else { - // some "middle" line of multi-line - 0..usize::max_value() - }; if rectangular { - range.start.max(norm.start.x)..range.end.min(norm.end.x.saturating_add(1)) + if row < norm.start.y || row > norm.end.y { + 0..0 + } else { + if norm.start.x <= norm.end.x { + norm.start.x..norm.end.x.saturating_add(1) + } else { + norm.end.x..norm.start.x.saturating_add(1) + } + } } else { - range + if row < norm.start.y || row > norm.end.y { + 0..0 + } else if norm.start.y == norm.end.y { + // A single line selection + if norm.start.x <= norm.end.x { + norm.start.x..norm.end.x.saturating_add(1) + } else { + norm.end.x..norm.start.x.saturating_add(1) + } + } else if row == norm.end.y { + // last line of multi-line + 0..norm.end.x.saturating_add(1) + } else if row == norm.start.y { + // first line of multi-line + norm.start.x..usize::max_value() + } else { + // some "middle" line of multi-line + 0..usize::max_value() + } } } } diff --git a/wezterm-gui/src/termwindow/selection.rs b/wezterm-gui/src/termwindow/selection.rs index 7f126d4e0..01d8fb848 100644 --- a/wezterm-gui/src/termwindow/selection.rs +++ b/wezterm-gui/src/termwindow/selection.rs @@ -90,19 +90,31 @@ impl super::TermWindow { // Compute the start and end horizontall cell of the selection. // The selection extent depends on the mouse cursor position in relation // to the origin. - let (start_x, end_x) = if (x >= origin.x && y == origin.y) || y > origin.y { - // If the selection is extending forwards from the origin, it includes the - // origin and doesn't include the cell under the cursor. Note that the - // reported cell here is offset by -50% from the real cell you see on the - // screen, so this causes a visual cell on the screen to be selected when - // the mouse moves over 50% of its width, which effectively means the next - // cell is being reported here, hence it's excluded - (origin.x, x.saturating_sub(1)) + let (start_x, end_x) = if mode == SelectionMode::Block { + if x >= origin.x { + // If the selection is extending forwards from the origin, + // it includes the origin + (origin.x, x.saturating_sub(1)) + } else { + // If the selection is extending backwards from the origin, + // it doesn't include the origin + (origin.x.saturating_sub(1), x) + } } else { - // If the selection is extending backwards from the origin, it doesn't - // include the origin and includes the cell under the cursor, which has - // the same effect as described above when going backwards - (origin.x.saturating_sub(1), x) + if (x >= origin.x && y == origin.y) || y > origin.y { + // If the selection is extending forwards from the origin, it includes the + // origin and doesn't include the cell under the cursor. Note that the + // reported cell here is offset by -50% from the real cell you see on the + // screen, so this causes a visual cell on the screen to be selected when + // the mouse moves over 50% of its width, which effectively means the next + // cell is being reported here, hence it's excluded + (origin.x, x.saturating_sub(1)) + } else { + // If the selection is extending backwards from the origin, it doesn't + // include the origin and includes the cell under the cursor, which has + // the same effect as described above when going backwards + (origin.x.saturating_sub(1), x) + } }; self.selection(pane.pane_id()).range = if origin.x != x || origin.y != y {