diff --git a/term/src/lib.rs b/term/src/lib.rs index 572dc0256..6fc9ce6c9 100644 --- a/term/src/lib.rs +++ b/term/src/lib.rs @@ -79,6 +79,15 @@ pub fn in_range(value: T, range: &Range) -> bool { value >= range.start && value < range.end } +/// Returns true if r1 intersects r2 +pub fn intersects_range(r1: Range, r2: Range) -> bool { + use std::cmp::{max, min}; + let start = max(r1.start, r2.start); + let end = min(r1.end, r2.end); + + end > start +} + /// Position allows referring to an absolute visible row number /// or a position relative to some existing row number (typically /// where the cursor is located). Both of the cases are represented diff --git a/term/src/terminalstate.rs b/term/src/terminalstate.rs index 46e317c28..7d65df401 100644 --- a/term/src/terminalstate.rs +++ b/term/src/terminalstate.rs @@ -213,7 +213,7 @@ impl TerminalState { match sel { Some(sel) => { let sel_cols = sel.cols_for_row(row); - if cols.start >= sel_cols.start && cols.end <= sel_cols.end { + if intersects_range(cols, sel_cols) { // Intersects, so clear the selection self.clear_selection(); true @@ -239,7 +239,7 @@ impl TerminalState { match sel { Some(sel) => { let sel_rows = sel.rows(); - if rows.start >= sel_rows.start && rows.end <= sel_rows.end { + if intersects_range(rows, sel_rows) { // Intersects, so clear the selection self.clear_selection(); true