ratatui/layout/
position.rs

1#![warn(missing_docs)]
2use std::fmt;
3
4use crate::layout::Rect;
5
6/// Position in the terminal
7///
8/// The position is relative to the top left corner of the terminal window, with the top left corner
9/// being (0, 0). The x axis is horizontal increasing to the right, and the y axis is vertical
10/// increasing downwards.
11///
12/// # Examples
13///
14/// ```
15/// use ratatui::layout::{Position, Rect};
16///
17/// // the following are all equivalent
18/// let position = Position { x: 1, y: 2 };
19/// let position = Position::new(1, 2);
20/// let position = Position::from((1, 2));
21/// let position = Position::from(Rect::new(1, 2, 3, 4));
22///
23/// // position can be converted back into the components when needed
24/// let (x, y) = position.into();
25/// ```
26#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
27#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
28pub struct Position {
29    /// The x coordinate of the position
30    ///
31    /// The x coordinate is relative to the left edge of the terminal window, with the left edge
32    /// being 0.
33    pub x: u16,
34
35    /// The y coordinate of the position
36    ///
37    /// The y coordinate is relative to the top edge of the terminal window, with the top edge
38    /// being 0.
39    pub y: u16,
40}
41
42impl Position {
43    /// Position at the origin, the top left edge at 0,0
44    pub const ORIGIN: Self = Self { x: 0, y: 0 };
45
46    /// Create a new position
47    pub const fn new(x: u16, y: u16) -> Self {
48        Self { x, y }
49    }
50}
51
52impl From<(u16, u16)> for Position {
53    fn from((x, y): (u16, u16)) -> Self {
54        Self { x, y }
55    }
56}
57
58impl From<Position> for (u16, u16) {
59    fn from(position: Position) -> Self {
60        (position.x, position.y)
61    }
62}
63
64impl From<Rect> for Position {
65    fn from(rect: Rect) -> Self {
66        rect.as_position()
67    }
68}
69
70impl fmt::Display for Position {
71    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
72        write!(f, "({}, {})", self.x, self.y)
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn new() {
82        let position = Position::new(1, 2);
83        assert_eq!(position.x, 1);
84        assert_eq!(position.y, 2);
85    }
86
87    #[test]
88    fn from_tuple() {
89        let position = Position::from((1, 2));
90        assert_eq!(position.x, 1);
91        assert_eq!(position.y, 2);
92    }
93
94    #[test]
95    fn into_tuple() {
96        let position = Position::new(1, 2);
97        let (x, y) = position.into();
98        assert_eq!(x, 1);
99        assert_eq!(y, 2);
100    }
101
102    #[test]
103    fn from_rect() {
104        let rect = Rect::new(1, 2, 3, 4);
105        let position = Position::from(rect);
106        assert_eq!(position.x, 1);
107        assert_eq!(position.y, 2);
108    }
109
110    #[test]
111    fn to_string() {
112        let position = Position::new(1, 2);
113        assert_eq!(position.to_string(), "(1, 2)");
114    }
115}