aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: f4163a809499c7c11efa0907656d346880cd49de (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
use crossterm::{
    event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use std::{error::Error, io, ptr};
use tui::{
    backend::{Backend, CrosstermBackend},
    layout::{Constraint, Direction, Layout},
    style::{Color, Modifier, Style},
    text::Spans,
    widgets::{self, Block, Borders, Cell, Paragraph, Table, Tabs, Widget},
    Frame, Terminal,
};
use volute::{
    app::{App, CONFIG_TAB_INDEX, INPUT_TAB_INDEX},
    input::InputMode,
};

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(())
}

// Input handling
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.tab_index {
                INPUT_TAB_INDEX => match app.input_mode {
                    InputMode::Normal => match key.code {
                        KeyCode::Char('q') => {
                            return Ok(());
                        }
                        KeyCode::Char('L') => app.next_tab(),
                        KeyCode::Char('H') => app.previous_tab(),
                        KeyCode::Char('j') => app.next_row(),
                        KeyCode::Char('k') => app.previous_row(),
                        KeyCode::Char('l') => app.next_column(),
                        KeyCode::Char('h') => app.previous_column(),
                        KeyCode::Char('i') => {
                            app.input_mode = InputMode::Insert;
                        }
                        KeyCode::Char('p') => app.insert_row(),
                        KeyCode::Char('d') => app.remove_row(),
                        _ => {}
                    },
                    InputMode::Insert => match key.code {
                        KeyCode::Esc | KeyCode::Enter => {
                            app.input_mode = InputMode::Normal;
                        }
                        KeyCode::Char(c) => app.selected_input_param_mut().push(c),
                        KeyCode::Backspace => app.selected_input_param_mut().pop(),
                        _ => {}
                    },
                },
                CONFIG_TAB_INDEX => match key.code {
                    KeyCode::Char('q') => {
                        return Ok(());
                    }
                    KeyCode::Char('L') => app.next_tab(),
                    KeyCode::Char('H') => app.previous_tab(),
                    _ => {}
                },
                _ => unreachable!(),
            }
        }
    }
}

fn ui<B: Backend>(f: &mut Frame<B>, app: &App) {
    let titles = app.tab_titles().iter().map(|t| Spans::from(*t)).collect();
    let tabs = Tabs::new(titles)
        .block(Block::default().borders(Borders::ALL).title("Tabs"))
        .select(app.tab_index)
        .highlight_style(
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD),
        );

    match app.tab_index {
        INPUT_TAB_INDEX => {
            let layout = Layout::default()
                .direction(Direction::Vertical)
                .constraints(
                    [
                        Constraint::Length(3),                           // Tabs
                        Constraint::Length(app.rows().len() as u16 + 3), // Input table
                        Constraint::Max(100),                            // Spacer
                        Constraint::Length(1),                           // Footer
                    ]
                    .as_ref(),
                )
                .split(f.size());
            f.render_widget(tabs, layout[0]);
            f.render_widget(input_table(app), layout[1]);
            f.render_widget(footer(app), layout[3]);
        }
        CONFIG_TAB_INDEX => {
            let layout = Layout::default()
                .direction(Direction::Vertical)
                .constraints([Constraint::Length(3), Constraint::Min(3)].as_ref())
                .split(f.size());
            f.render_widget(tabs, layout[0]);
            f.render_widget(Paragraph::new("Config tab"), layout[1]);
        }
        _ => unreachable!(),
    }
}

fn input_table(app: &App) -> impl Widget {
    let rows = app.rows().iter().map(|row| {
        let cells = row.iter().map(|item| {
            if ptr::eq(item, app.selected_input_param()) {
                Cell::from(item.string()).style(match app.input_mode {
                    InputMode::Normal => Style::default().fg(Color::Yellow),
                    InputMode::Insert => Style::default()
                        .fg(Color::Blue)
                        .add_modifier(Modifier::ITALIC),
                })
            } else {
                Cell::from(item.string())
            }
        });
        widgets::Row::new(cells)
    });

    Table::new(rows)
        .header(widgets::Row::new(vec!["rpm", "ve", "map"]))
        .block(Block::default().borders(Borders::ALL).title("inputs"))
        .widths(&[
            Constraint::Length(5), // rpm
            Constraint::Length(3), // ve
            Constraint::Length(3), // map
        ])
}

fn footer(app: &App) -> impl Widget {
    match app.input_mode {
        InputMode::Normal => {
            Paragraph::new("Normal").style(Style::default().fg(Color::Black).bg(Color::Yellow))
        }
        InputMode::Insert => {
            Paragraph::new("Insert").style(Style::default().fg(Color::Black).bg(Color::Blue))
        }
    }
}