From 55ae1fb76af56195573da4fa3c02c4f92d438be8 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 10 Feb 2018 18:37:47 -0800 Subject: [PATCH] move selection types to their own file --- term/src/lib.rs | 90 ++----------------------------------------- term/src/selection.rs | 89 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 87 deletions(-) create mode 100644 term/src/selection.rs diff --git a/term/src/lib.rs b/term/src/lib.rs index f5af218b0..fa3f05c90 100644 --- a/term/src/lib.rs +++ b/term/src/lib.rs @@ -26,6 +26,9 @@ use std::time::{Duration, Instant}; #[macro_use] mod debug; +pub mod selection; +use selection::{SelectionCoordinate, SelectionRange}; + pub mod hyperlink; use hyperlink::Hyperlink; @@ -78,93 +81,6 @@ pub struct CursorPosition { pub y: VisibleRowIndex, } -/// The x,y coordinates of either the start or end of a selection region -#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)] -pub struct SelectionCoordinate { - pub x: usize, - pub y: ScrollbackOrVisibleRowIndex, -} - -/// Represents the selected text range. -/// The end coordinates are inclusive. -#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)] -pub struct SelectionRange { - pub start: SelectionCoordinate, - pub end: SelectionCoordinate, -} - -impl SelectionRange { - /// Create a new range that starts at the specified location - pub fn start(start: SelectionCoordinate) -> Self { - let end = start.clone(); - Self { start, end } - } - - /// Returns an extended selection that it ends at the specified location - pub fn extend(&self, end: SelectionCoordinate) -> Self { - Self { - start: self.start.clone(), - end, - } - } - - /// Return a normalized selection such that the starting y coord - /// is <= the ending y coord. - pub fn normalize(&self) -> Self { - if self.start.y <= self.end.y { - self.clone() - } else { - Self { - start: self.end.clone(), - end: self.start.clone(), - } - } - } - - /// Yields a range representing the row indices. - /// Make sure that you invoke this on a normalized range! - pub fn rows(&self) -> Range { - debug_assert!( - self.start.y <= self.end.y, - "you forgot to normalize a SelectionRange" - ); - self.start.y..self.end.y + 1 - } - - /// Yields a range representing the selected columns for the specified row. - /// Not that the range may include usize::max_value() for some rows; this - /// indicates that the selection extends to the end of that row. - /// Since this struct has no knowledge of line length, it cannot be - /// more precise than that. - /// Must be called on a normalized range! - pub fn cols_for_row(&self, row: ScrollbackOrVisibleRowIndex) -> Range { - let (x1, x2) = if self.start.x <= self.end.x { - (self.start.x, self.end.x.saturating_add(1)) - } else { - (self.end.x, self.start.x.saturating_add(1)) - }; - debug_assert!( - self.start.y <= self.end.y, - "you forgot to normalize a SelectionRange" - ); - if row < self.start.y || row > self.end.y { - 0..0 - } else if self.start.y == self.end.y { - // A single line selection - x1..x2 - } else if row == self.end.y { - // last line of multi-line - 0..x2 - } else if row == self.start.y { - // first line of multi-line - x1..usize::max_value() - } else { - // some "middle" line of multi-line - 0..usize::max_value() - } - } -} - pub mod color; mod csi; use self::csi::*; diff --git a/term/src/selection.rs b/term/src/selection.rs new file mode 100644 index 000000000..331be6537 --- /dev/null +++ b/term/src/selection.rs @@ -0,0 +1,89 @@ +use super::ScrollbackOrVisibleRowIndex; +use std::ops::Range; + +/// The x,y coordinates of either the start or end of a selection region +#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)] +pub struct SelectionCoordinate { + pub x: usize, + pub y: ScrollbackOrVisibleRowIndex, +} + +/// Represents the selected text range. +/// The end coordinates are inclusive. +#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)] +pub struct SelectionRange { + pub start: SelectionCoordinate, + pub end: SelectionCoordinate, +} + +impl SelectionRange { + /// Create a new range that starts at the specified location + pub fn start(start: SelectionCoordinate) -> Self { + let end = start.clone(); + Self { start, end } + } + + /// Returns an extended selection that it ends at the specified location + pub fn extend(&self, end: SelectionCoordinate) -> Self { + Self { + start: self.start.clone(), + end, + } + } + + /// Return a normalized selection such that the starting y coord + /// is <= the ending y coord. + pub fn normalize(&self) -> Self { + if self.start.y <= self.end.y { + self.clone() + } else { + Self { + start: self.end.clone(), + end: self.start.clone(), + } + } + } + + /// Yields a range representing the row indices. + /// Make sure that you invoke this on a normalized range! + pub fn rows(&self) -> Range { + debug_assert!( + self.start.y <= self.end.y, + "you forgot to normalize a SelectionRange" + ); + self.start.y..self.end.y + 1 + } + + /// Yields a range representing the selected columns for the specified row. + /// Not that the range may include usize::max_value() for some rows; this + /// indicates that the selection extends to the end of that row. + /// Since this struct has no knowledge of line length, it cannot be + /// more precise than that. + /// Must be called on a normalized range! + pub fn cols_for_row(&self, row: ScrollbackOrVisibleRowIndex) -> Range { + let (x1, x2) = if self.start.x <= self.end.x { + (self.start.x, self.end.x.saturating_add(1)) + } else { + (self.end.x, self.start.x.saturating_add(1)) + }; + debug_assert!( + self.start.y <= self.end.y, + "you forgot to normalize a SelectionRange" + ); + if row < self.start.y || row > self.end.y { + 0..0 + } else if self.start.y == self.end.y { + // A single line selection + x1..x2 + } else if row == self.end.y { + // last line of multi-line + 0..x2 + } else if row == self.start.y { + // first line of multi-line + x1..usize::max_value() + } else { + // some "middle" line of multi-line + 0..usize::max_value() + } + } +}