par_term/
selection.rs

1/// Selection mode
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub enum SelectionMode {
4    /// Normal character-based selection
5    Normal,
6    /// Rectangular/block selection
7    Rectangular,
8    /// Full line selection (triple-click)
9    Line,
10}
11
12/// Selection state for text selection
13#[derive(Debug, Clone, Copy, PartialEq)]
14pub struct Selection {
15    /// Start position (col, row) in terminal coordinates
16    pub start: (usize, usize),
17    /// End position (col, row) in terminal coordinates
18    pub end: (usize, usize),
19    /// Selection mode
20    pub mode: SelectionMode,
21}
22
23impl Selection {
24    /// Create a new selection
25    pub fn new(start: (usize, usize), end: (usize, usize), mode: SelectionMode) -> Self {
26        Self { start, end, mode }
27    }
28
29    /// Get normalized selection (ensures start is before end)
30    pub fn normalized(&self) -> ((usize, usize), (usize, usize)) {
31        let (start_col, start_row) = self.start;
32        let (end_col, end_row) = self.end;
33
34        if start_row < end_row || (start_row == end_row && start_col <= end_col) {
35            (self.start, self.end)
36        } else {
37            (self.end, self.start)
38        }
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn test_selection_normalization() {
48        // Forward selection
49        let sel = Selection::new((0, 0), (10, 0), SelectionMode::Normal);
50        assert_eq!(sel.normalized(), ((0, 0), (10, 0)));
51
52        // Backward selection (same line)
53        let sel = Selection::new((10, 0), (0, 0), SelectionMode::Normal);
54        assert_eq!(sel.normalized(), ((0, 0), (10, 0)));
55
56        // Forward selection (multi-line)
57        let sel = Selection::new((10, 0), (5, 1), SelectionMode::Normal);
58        assert_eq!(sel.normalized(), ((10, 0), (5, 1)));
59
60        // Backward selection (multi-line)
61        let sel = Selection::new((5, 1), (10, 0), SelectionMode::Normal);
62        assert_eq!(sel.normalized(), ((10, 0), (5, 1)));
63    }
64}