Skip to main content

beamterm_core/
position.rs

1/// A position in the terminal grid, specified by column and row.
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub struct CursorPosition {
4    /// Column index (zero-based).
5    pub col: u16,
6    /// Row index (zero-based).
7    pub row: u16,
8}
9
10impl CursorPosition {
11    /// Creates a new cursor position at the given column and row.
12    #[must_use]
13    pub fn new(col: u16, row: u16) -> Self {
14        Self { col, row }
15    }
16
17    pub(crate) fn move_left(self, distance: u16) -> Option<CursorPosition> {
18        self.col
19            .checked_sub(distance)
20            .map(|col| CursorPosition::new(col, self.row))
21    }
22
23    pub(crate) fn move_right(self, distance: u16, row_length: u16) -> Option<CursorPosition> {
24        self.col
25            .checked_add(distance)
26            .map(|col| CursorPosition::new(col, self.row))
27            .filter(|&pos| pos.col < row_length)
28    }
29}