Skip to main content

beamterm_renderer/
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    pub col: u16,
5    pub row: u16,
6}
7
8impl CursorPosition {
9    pub fn new(col: u16, row: u16) -> Self {
10        Self { col, row }
11    }
12
13    pub(crate) fn move_left(self, distance: u16) -> Option<CursorPosition> {
14        self.col
15            .checked_sub(distance)
16            .map(|col| CursorPosition::new(col, self.row))
17    }
18
19    pub(crate) fn move_right(self, distance: u16, row_length: u16) -> Option<CursorPosition> {
20        self.col
21            .checked_add(distance)
22            .map(|col| CursorPosition::new(col, self.row))
23            .filter(|&pos| pos.col < row_length)
24    }
25}