example/
example.rs

1use simple_terminal_app::{
2    app,
3    commands::{clear, color, cursor},
4    event::{self, Key},
5    scene::Scene,
6    Point,
7};
8
9struct TestScene {
10    color: u8,
11}
12
13impl Scene for TestScene {
14    fn init(&mut self, state: &mut app::State) {
15        state
16            .command()
17            .push("Hello!")
18            .push(cursor::SteadyBlock)
19            .execute();
20
21        state.flush();
22    }
23
24    fn process_input(&mut self, state: &mut app::State, key_event: event::Key) {
25        match key_event {
26            Key::Esc => state.stop(),
27            Key::Char('\n') => {
28                let pos = state.cursor_position;
29
30                state
31                    .command()
32                    .push(cursor::Goto(Point::new(0, pos.y + 1)))
33                    .execute();
34            }
35
36            Key::Char('c') => state
37                .command()
38                .push(clear::All)
39                .push(cursor::Goto(Point::new(0, 0)))
40                .execute(),
41            Key::Char('p') => {
42                let pos = state.cursor_position;
43
44                state.command().push(pos).execute();
45            }
46
47            Key::Char('H') => state.command().push(cursor::Show).execute(),
48            Key::Char('h') => state.command().push(cursor::Hide).execute(),
49            Key::Char('m') => state.command().push("message ").execute(),
50            Key::Char('q') => state.change_scene(Box::new(QuitScene)),
51
52            Key::Char('s') => {
53                let size = state.size();
54
55                state.command().push(size).execute();
56            }
57
58            _ => {}
59        }
60
61        state.flush();
62    }
63
64    fn update(&mut self, state: &mut app::State) {
65        let size = state.size();
66
67        let pos = state.cursor_position;
68
69        let write_pos = Point::new(size.x - pos.to_string().len() as u16, size.y);
70
71        state
72            .command()
73            .push(cursor::Save)
74            .push(cursor::Goto(write_pos))
75            .push(clear::CurrentLine)
76            .push(color::Fg(color::AnsiValue(self.color)))
77            .push(pos)
78            .push(color::Fg(color::Reset))
79            .push(cursor::Restore)
80            .execute();
81
82        state.flush();
83
84        self.color = self.color.overflowing_add(1).0;
85    }
86}
87
88struct QuitScene;
89
90impl Scene for QuitScene {
91    fn init(&mut self, state: &mut app::State) {
92        state
93            .command()
94            .push(clear::All)
95            .push(cursor::Goto(Point::new(0, 0)))
96            .push("Press any key to exit. ")
97            .execute();
98
99        state.flush();
100    }
101
102    fn process_input(&mut self, state: &mut app::State, _key: Key) {
103        state.command().push("See ya!").execute();
104
105        state.flush();
106
107        std::thread::sleep(std::time::Duration::from_secs(1));
108
109        state.stop();
110    }
111
112    fn update(&mut self, _state: &mut app::State) {}
113}
114
115fn main() {
116    app::start(Box::new(TestScene { color: 0 }), None);
117}