mirror of
https://github.com/wez/wezterm.git
synced 2024-11-11 03:27:05 +03:00
move selection types to their own file
This commit is contained in:
parent
68949808ba
commit
55ae1fb76a
@ -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<ScrollbackOrVisibleRowIndex> {
|
||||
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<usize> {
|
||||
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::*;
|
||||
|
89
term/src/selection.rs
Normal file
89
term/src/selection.rs
Normal file
@ -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<ScrollbackOrVisibleRowIndex> {
|
||||
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<usize> {
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user