diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-08-02 00:36:10 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-08-02 00:36:10 +0200 |
| commit | fe0938b1de0c46fc2afcaa3dcd6a0f4ec870d21a (patch) | |
| tree | 8db7509894842395cfb309f00c41b7f4d173888c /src/widgets/luaeditor/cursor.rs | |
| parent | 1e1eb95926f556e666bc20355327abd24d264858 (diff) | |
add state which is shared with the lua environment
Diffstat (limited to 'src/widgets/luaeditor/cursor.rs')
| -rw-r--r-- | src/widgets/luaeditor/cursor.rs | 102 |
1 files changed, 0 insertions, 102 deletions
diff --git a/src/widgets/luaeditor/cursor.rs b/src/widgets/luaeditor/cursor.rs deleted file mode 100644 index e051231..0000000 --- a/src/widgets/luaeditor/cursor.rs +++ /dev/null @@ -1,102 +0,0 @@ - -pub enum CursorMove { - Up(usize), - Down(usize), - Left(usize), - Right(usize), - Top, - Bottom, - Begin, - End, - Jump((usize, usize)), -} - -#[derive(Default, Debug, Clone)] -pub struct Cursor { - x: isize, - y: isize, - x_max: isize, - y_max: isize, -} - -impl Cursor { - pub fn new() -> Self { - Self { - x: 0, - y: 0, - x_max: 0, - y_max: 0, - } - } - - pub fn with_position(mut self, x: isize, y: isize) -> Self { - self.x = x; - self.y = y; - self - } - - pub fn with_max(mut self, x: isize, y: isize) -> Self { - self.x_max = x; - self.y_max = y; - self - } - - pub fn x(&self) -> usize { - self.x as usize - } - - pub fn y(&self) -> usize { - self.y as usize - } - - pub fn set_x_max(&mut self, max: usize) { - self.x_max = max as isize; - } - - pub fn set_y_max(&mut self, max: usize) { - self.y_max = max as isize; - } - - pub fn move_checked(&mut self, m: CursorMove) { - self.move_unchecked(m); - self.correct_cursor_position(); - } - - pub fn move_unchecked(&mut self, m: CursorMove) { - match m { - CursorMove::Up(n) => self.y -= n as isize, - CursorMove::Down(n) => self.y += n as isize, - CursorMove::Left(n) => self.x -= n as isize, - CursorMove::Right(n) => self.x += n as isize, - CursorMove::Top => self.y = 0, - CursorMove::Bottom => self.y = self.y_max, - CursorMove::Begin => self.x = 0, - CursorMove::End => self.x = self.x_max, - CursorMove::Jump((x, y)) => { - self.x = x as isize; - self.y = y as isize; - } - }; - } - - pub fn correct_cursor_position(&mut self) { - self.y = self.y.max(0).min(self.y_max); - self.x = self.x.max(0).min(self.x_max); - } - - pub fn is_at_start(&self) -> bool { - self.x == 0 - } - - pub fn is_at_end(&self) -> bool { - self.x == self.x_max - } - - pub fn is_at_top(&self) -> bool { - self.y == 0 - } - - pub fn is_at_bottom(&self) -> bool { - self.y == self.y_max - } -} |