aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: fa871eaddaa9cd2654cf58b166e53862360c1118 (plain) (blame)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use crossterm::{
    event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use std::{error::Error, io};
use tui::{
    backend::{Backend, CrosstermBackend},
    layout::{Constraint, Direction, Layout},
    style::{Color, Style},
    widgets::{Block, Borders, Paragraph},
    Frame, Terminal,
};
use unicode_width::UnicodeWidthStr;

enum InputMode {
    Normal,
    Insert,
}

enum Row {
    Rpm(String),
    Ve(String),
    Map(String),
}

impl Row {
    fn push(&mut self, c: char) {
        match self {
            Row::Rpm(rpm) => {
                rpm.push(c);
                *self = Row::Rpm(rpm.to_string());
            }
            Row::Ve(ve) => {
                ve.push(c);
                *self = Row::Ve(ve.to_string());
            }
            Row::Map(map) => {
                map.push(c);
                *self = Row::Map(map.to_string());
            }
        }
    }

    fn pop(&mut self) {
        match self {
            Row::Rpm(rpm) => {
                rpm.pop();
                *self = Row::Rpm(rpm.to_string());
            }
            Row::Ve(ve) => {
                ve.pop();
                *self = Row::Rpm(ve.to_string());
            }
            Row::Map(map) => {
                map.pop();
                *self = Row::Map(map.to_string());
            }
        }
    }

    fn string(&self) -> String {
        match self {
            Row::Rpm(rpm) => rpm.to_string(),
            Row::Ve(ve) => ve.to_string(),
            Row::Map(map) => map.to_string(),
        }
    }

    fn next(&self) -> Self {
        match self {
            Row::Rpm(_) => Row::Ve(String::new()),
            Row::Ve(_) => Row::Map(String::new()),
            Row::Map(_) => Row::Rpm(String::new()),
        }
    }

    fn previous(&self) -> Self {
        match self {
            Row::Rpm(_) => Row::Map(String::new()),
            Row::Ve(_) => Row::Rpm(String::new()),
            Row::Map(_) => Row::Ve(String::new()),
        }
    }
}

struct Column {
    rpm: Row,
    ve: Row,
    map: Row,
}

/// App holds the state of the application
struct App {
    column: Column,

    selected_row: Row,

    input_mode: InputMode,
}

impl Default for App {
    fn default() -> App {
        App {
            column: Column {
                rpm: Row::Rpm(String::from("7000")),
                ve: Row::Ve(String::from("95")),
                map: Row::Map(String::from("150")),
            },
            selected_row: Row::Rpm(String::new()),
            input_mode: InputMode::Normal,
        }
    }
}

fn main() -> Result<(), Box<dyn Error>> {
    // setup terminal
    enable_raw_mode()?;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    // create app and run it
    let app = App::default();
    let res = run_app(&mut terminal, app);

    // restore terminal
    disable_raw_mode()?;
    execute!(
        terminal.backend_mut(),
        LeaveAlternateScreen,
        DisableMouseCapture
    )?;
    terminal.show_cursor()?;

    if let Err(err) = res {
        println!("{:?}", err)
    }

    Ok(())
}

fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<()> {
    loop {
        terminal.draw(|f| ui(f, &app))?;

        if let Event::Key(key) = event::read()? {
            match app.input_mode {
                InputMode::Normal => match key.code {
                    KeyCode::Char('i') => {
                        app.input_mode = InputMode::Insert;
                    }
                    KeyCode::Char('q') => {
                        return Ok(());
                    }
                    KeyCode::Char('j') => {
                        app.selected_row = app.selected_row.next();
                    }
                    KeyCode::Char('k') => {
                        app.selected_row = app.selected_row.previous();
                    }
                    _ => {}
                },
                InputMode::Insert => match key.code {
                    KeyCode::Enter => {
                        app.input_mode = InputMode::Normal;
                    }
                    KeyCode::Char(c) => match app.selected_row {
                        Row::Rpm(_) => {
                            app.column.rpm.push(c);
                        }
                        Row::Ve(_) => {
                            app.column.ve.push(c);
                        }
                        Row::Map(_) => {
                            app.column.map.push(c);
                        }
                    },
                    KeyCode::Backspace => match app.selected_row {
                        Row::Rpm(_) => {
                            app.column.rpm.pop();
                        }
                        Row::Ve(_) => {
                            app.column.ve.pop();
                        }
                        Row::Map(_) => {
                            app.column.map.pop();
                        }
                    },
                    KeyCode::Esc => {
                        app.input_mode = InputMode::Normal;
                    }
                    _ => {}
                },
            }
        }
    }
}

fn ui<B: Backend>(f: &mut Frame<B>, app: &App) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .margin(2)
        .constraints([Constraint::Length(3), Constraint::Length(3), Constraint::Length(3)].as_ref())
        .split(f.size());

    let rpm = Paragraph::new(app.column.rpm.string())
        .style(match app.selected_row {
            Row::Rpm(_) => Style::default().fg(Color::Yellow),
            _ => Style::default(),
        })
        .block(Block::default().borders(Borders::ALL).title("rpm"));
    f.render_widget(rpm, chunks[0]);

    let ve = Paragraph::new(app.column.ve.string())
        .style(match app.selected_row {
            Row::Ve(_) => Style::default().fg(Color::Yellow),
            _ => Style::default(),
        })
        .block(Block::default().borders(Borders::ALL).title("ve"));
    f.render_widget(ve, chunks[1]);

    let map = Paragraph::new(app.column.map.string())
        .style(match app.selected_row {
            Row::Map(_) => Style::default().fg(Color::Yellow),
            _ => Style::default(),
        })
        .block(Block::default().borders(Borders::ALL).title("map"));
    f.render_widget(map, chunks[2]);
}