1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use std::fmt::Debug;

/// Create a new, immutable position (column, line);
///
/// # Examples
/// ```
/// use tty_interface::{Position, pos};
///
/// let position = pos!(1, 2);
/// assert_eq!(1, position.x());
/// assert_eq!(2, position.y());
/// ```
#[macro_export]
macro_rules! pos {
    ($x: expr, $y: expr) => {
        Position::new($x, $y)
    };
}

/// A coordinate position in the terminal. May be absolute or relative to some buffer's origin.
#[derive(Eq, PartialEq, Copy, Clone)]
pub struct Position {
    x: u16,
    y: u16,
}

impl Position {
    /// Create a new, immutable position.
    ///
    /// # Examples
    /// ```
    /// use tty_interface::Position;
    ///
    /// let origin = Position::new(2, 4);
    /// assert_eq!(2, origin.x());
    /// assert_eq!(4, origin.y());
    /// ```
    pub fn new(x: u16, y: u16) -> Position {
        Position { x, y }
    }

    /// This position's column value.
    pub fn x(&self) -> u16 {
        self.x
    }

    /// This position's line value.
    pub fn y(&self) -> u16 {
        self.y
    }
}

impl PartialOrd for Position {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Position {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        match self.y().cmp(&other.y()) {
            std::cmp::Ordering::Equal => self.x().cmp(&other.x()),
            ordering => ordering,
        }
    }
}

impl Debug for Position {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Position({}, {})", self.x(), self.y())
    }
}

#[cfg(test)]
mod tests {
    use std::cmp::Ordering;

    use crate::{pos, Position};

    #[test]
    fn position_initialization() {
        let cases = [(0, 0), (0, 3), (8, 2), (4, 6)];

        for (x, y) in cases {
            let position = pos!(x, y);

            assert_eq!(x, position.x());
            assert_eq!(y, position.y());
        }
    }

    #[test]
    fn position_comparison() {
        let assert_case = |first: (u16, u16), second: (u16, u16), expected: Ordering| {
            let first_position = pos!(first.0, first.1);
            let second_position = pos!(second.0, second.1);

            assert_eq!(
                expected,
                first_position.cmp(&second_position),
                "comparing {:?} and {:?}",
                first_position,
                second_position
            );

            assert_eq!(
                Some(expected),
                first_position.partial_cmp(&second_position),
                "comparing {:?} and {:?}",
                first_position,
                second_position,
            );
        };

        let positions = [(0, 0), (0, 1), (1, 0), (1, 1)];

        let cases = [
            (positions[0], positions[0], Ordering::Equal),
            (positions[0], positions[1], Ordering::Less),
            (positions[0], positions[2], Ordering::Less),
            (positions[0], positions[3], Ordering::Less),
            (positions[1], positions[0], Ordering::Greater),
            (positions[1], positions[1], Ordering::Equal),
            (positions[1], positions[2], Ordering::Greater),
            (positions[1], positions[3], Ordering::Less),
            (positions[2], positions[0], Ordering::Greater),
            (positions[2], positions[1], Ordering::Less),
            (positions[2], positions[2], Ordering::Equal),
            (positions[2], positions[3], Ordering::Less),
            (positions[3], positions[0], Ordering::Greater),
            (positions[3], positions[1], Ordering::Greater),
            (positions[3], positions[2], Ordering::Greater),
            (positions[3], positions[3], Ordering::Equal),
        ];

        for case in cases {
            assert_case(case.0, case.1, case.2);
        }
    }
}