Skip to main content

gm_lib/utils/
cursor.rs

1use crossterm::event::{KeyCode, KeyEventKind};
2
3use crate::tui::Event;
4
5#[derive(Clone, Debug, Default)]
6pub struct Cursor {
7    pub current: usize,
8}
9
10impl Cursor {
11    pub fn reset(&mut self) {
12        self.current = 0;
13    }
14
15    pub fn handle(&mut self, event: &Event, cursor_max: usize) {
16        if let Event::Input(key_event) = event {
17            if key_event.kind == KeyEventKind::Press {
18                match key_event.code {
19                    KeyCode::Up => {
20                        self.move_up(cursor_max);
21                    }
22                    KeyCode::Down => {
23                        self.move_down(cursor_max);
24                    }
25                    _ => {}
26                }
27            }
28        }
29    }
30
31    fn move_up(&mut self, max: usize) {
32        if max != 0 {
33            self.current = (self.current + max - 1) % max;
34        }
35    }
36
37    fn move_down(&mut self, max: usize) {
38        if max != 0 {
39            self.current = (self.current + 1) % max;
40        }
41    }
42}