#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SelectionMode {
Normal,
Rectangular,
Line,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Selection {
pub start: (usize, usize),
pub end: (usize, usize),
pub mode: SelectionMode,
}
impl Selection {
pub fn new(start: (usize, usize), end: (usize, usize), mode: SelectionMode) -> Self {
Self { start, end, mode }
}
pub fn normalized(&self) -> ((usize, usize), (usize, usize)) {
let (start_col, start_row) = self.start;
let (end_col, end_row) = self.end;
if start_row < end_row || (start_row == end_row && start_col <= end_col) {
(self.start, self.end)
} else {
(self.end, self.start)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_selection_normalization() {
let sel = Selection::new((0, 0), (10, 0), SelectionMode::Normal);
assert_eq!(sel.normalized(), ((0, 0), (10, 0)));
let sel = Selection::new((10, 0), (0, 0), SelectionMode::Normal);
assert_eq!(sel.normalized(), ((0, 0), (10, 0)));
let sel = Selection::new((10, 0), (5, 1), SelectionMode::Normal);
assert_eq!(sel.normalized(), ((10, 0), (5, 1)));
let sel = Selection::new((5, 1), (10, 0), SelectionMode::Normal);
assert_eq!(sel.normalized(), ((10, 0), (5, 1)));
}
}