1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use std::{
    io::{self, stdout, Stdout},
    rc::Rc,
    time::Duration,
};

use anyhow::Result;
use crossterm::{
    event::{self, Event, KeyCode, KeyEventKind},
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
    ExecutableCommand,
};
use ratatui::{
    prelude::*,
    widgets::{block::Title, *},
};

fn main() -> Result<()> {
    App::run()
}

struct App {
    term: Term,
    should_quit: bool,
    state: AppState,
}

#[derive(Debug, Default, Clone, Copy)]
struct AppState {
    progress1: u16,
    progress2: u16,
    progress3: f64,
    progress4: u16,
}

impl App {
    fn run() -> Result<()> {
        // run at ~10 fps minus the time it takes to draw
        let timeout = Duration::from_secs_f32(1.0 / 10.0);
        let mut app = Self::start()?;
        while !app.should_quit {
            app.update();
            app.draw()?;
            app.handle_events(timeout)?;
        }
        app.stop()?;
        Ok(())
    }

    fn start() -> Result<Self> {
        Ok(App {
            term: Term::start()?,
            should_quit: false,
            state: AppState {
                progress1: 0,
                progress2: 0,
                progress3: 0.0,
                progress4: 0,
            },
        })
    }

    fn stop(&mut self) -> Result<()> {
        Term::stop()?;
        Ok(())
    }

    fn update(&mut self) {
        self.state.progress1 = (self.state.progress1 + 4).min(100);
        self.state.progress2 = (self.state.progress2 + 3).min(100);
        self.state.progress3 = (self.state.progress3 + 0.02).min(1.0);
        self.state.progress4 = (self.state.progress4 + 1).min(100);
    }

    fn draw(&mut self) -> Result<()> {
        self.term.draw(|frame| {
            let state = self.state;
            let layout = Self::equal_layout(frame);
            Self::render_gauge1(state.progress1, frame, layout[0]);
            Self::render_gauge2(state.progress2, frame, layout[1]);
            Self::render_gauge3(state.progress3, frame, layout[2]);
            Self::render_gauge4(state.progress4, frame, layout[3]);
        })?;
        Ok(())
    }

    fn handle_events(&mut self, timeout: Duration) -> io::Result<()> {
        if event::poll(timeout)? {
            if let Event::Key(key) = event::read()? {
                if key.kind == KeyEventKind::Press {
                    if let KeyCode::Char('q') = key.code {
                        self.should_quit = true;
                    }
                }
            }
        }
        Ok(())
    }

    fn equal_layout(frame: &Frame) -> Rc<[Rect]> {
        Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Percentage(25),
                Constraint::Percentage(25),
                Constraint::Percentage(25),
                Constraint::Percentage(25),
            ])
            .split(frame.size())
    }

    fn render_gauge1(progress: u16, frame: &mut Frame, area: Rect) {
        let title = Self::title_block("Gauge with percentage progress");
        let gauge = Gauge::default()
            .block(title)
            .gauge_style(Style::new().light_red())
            .percent(progress);
        frame.render_widget(gauge, area);
    }

    fn render_gauge2(progress: u16, frame: &mut Frame, area: Rect) {
        let title = Self::title_block("Gauge with percentage progress and custom label");
        let label = format!("{}/100", progress);
        let gauge = Gauge::default()
            .block(title)
            .gauge_style(Style::new().blue().on_light_blue())
            .percent(progress)
            .label(label);
        frame.render_widget(gauge, area);
    }

    fn render_gauge3(progress: f64, frame: &mut Frame, area: Rect) {
        let title =
            Self::title_block("Gauge with ratio progress, custom label with style, and unicode");
        let label = Span::styled(
            format!("{:.2}%", progress * 100.0),
            Style::new().red().italic().bold(),
        );
        let gauge = Gauge::default()
            .block(title)
            .gauge_style(Style::default().fg(Color::Yellow))
            .ratio(progress)
            .label(label)
            .use_unicode(true);
        frame.render_widget(gauge, area);
    }

    fn render_gauge4(progress: u16, frame: &mut Frame, area: Rect) {
        let title = Self::title_block("Gauge with percentage progress and label");
        let label = format!("{}/100", progress);
        let gauge = Gauge::default()
            .block(title)
            .gauge_style(Style::new().green().italic())
            .percent(progress)
            .label(label);
        frame.render_widget(gauge, area);
    }

    fn title_block(title: &str) -> Block {
        let title = Title::from(title).alignment(Alignment::Center);
        Block::default().title(title).borders(Borders::TOP)
    }
}

struct Term {
    terminal: Terminal<CrosstermBackend<Stdout>>,
}

impl Term {
    pub fn start() -> io::Result<Term> {
        stdout().execute(EnterAlternateScreen)?;
        enable_raw_mode()?;
        let terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
        Ok(Self { terminal })
    }

    pub fn stop() -> io::Result<()> {
        disable_raw_mode()?;
        stdout().execute(LeaveAlternateScreen)?;
        Ok(())
    }

    fn draw(&mut self, frame: impl FnOnce(&mut Frame)) -> Result<()> {
        self.terminal.draw(frame)?;
        Ok(())
    }
}