Skip to content

Commit a338a99

Browse files
committed
TUI refactoring
1 parent bbd8303 commit a338a99

File tree

2 files changed

+39
-59
lines changed

2 files changed

+39
-59
lines changed

src/textui/mod.rs

Lines changed: 23 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ mod ui;
1717

1818
use crate::{filehost, serial};
1919
use anyhow::Result;
20-
use crossterm::event::KeyCode;
2120
use serialport::SerialPort;
2221
use ui::{StatefulList, StatefulTable};
2322

@@ -95,69 +94,34 @@ impl App {
9594
Ok(())
9695
}
9796

98-
// @todo this should be moved to ui.rs so that mod.rs is independent of crossterm
99-
pub fn keypress(&mut self, key: crossterm::event::KeyCode) -> Result<()> {
100-
match key {
101-
KeyCode::Enter => {
102-
match self.active_widget {
103-
AppWidgets::FileSelector => self.select_filehost_item()?,
104-
AppWidgets::FileAction => self.select_file_action()?,
105-
AppWidgets::CBMBrowser => self.select_cbm_item()?,
106-
_ => {}
107-
}
108-
}
97+
/// Go to previous item in current widget (typically when pressing arrow up)
98+
pub fn previous_item(&mut self) {
99+
match self.active_widget {
100+
AppWidgets::CBMBrowser => self.cbm_browser.previous(),
101+
AppWidgets::FileAction => self.file_action.previous(),
102+
AppWidgets::FileSelector => self.filetable.previous(),
109103
_ => {}
110104
}
105+
}
106+
107+
/// Go to next item in current widget (typically when pressing arrow down)
108+
pub fn next_item(&mut self) {
111109
match self.active_widget {
112-
AppWidgets::CBMBrowser => match key {
113-
KeyCode::Down => {
114-
self.cbm_browser.next();
115-
Ok(())
116-
}
117-
KeyCode::Up => {
118-
self.cbm_browser.previous();
119-
Ok(())
120-
}
121-
_ => Ok(()),
122-
},
123-
AppWidgets::FileAction => match key {
124-
KeyCode::Down => {
125-
self.file_action.next();
126-
Ok(())
127-
}
128-
KeyCode::Up => {
129-
self.file_action.previous();
130-
Ok(())
131-
}
132-
_ => Ok(()),
133-
},
134-
AppWidgets::FileSelector => match key {
135-
KeyCode::Down => {
136-
self.filetable.next();
137-
Ok(())
138-
}
139-
KeyCode::Up => {
140-
self.filetable.previous();
141-
Ok(())
142-
}
143-
KeyCode::Char('s') => {
144-
self.sort_filehost();
145-
Ok(())
146-
}
147-
_ => Ok(()),
148-
},
149-
_ => Ok(()),
110+
AppWidgets::CBMBrowser => self.cbm_browser.next(),
111+
AppWidgets::FileAction => self.file_action.next(),
112+
AppWidgets::FileSelector => self.filetable.next(),
113+
_ => {}
150114
}
151115
}
152116

153-
fn escape_to_filehost_browser(&mut self) {
117+
fn return_to_filehost(&mut self) {
154118
self.set_current_widget(AppWidgets::FileSelector);
155119
self.file_action.unselect();
156120
}
157121

158122
/// Select currently highlighted file in FileHost browser
159123
fn select_filehost_item(&mut self) -> Result<(), anyhow::Error> {
160-
// when selecting file, go to file action widget
124+
// when selecting file, go to file action widget
161125
self.active_widget = AppWidgets::FileAction;
162126
if !self.file_action.is_selected() {
163127
self.file_action.state.select(Some(0));
@@ -259,4 +223,11 @@ impl App {
259223
}
260224
Ok(())
261225
}
226+
227+
/// Send reset signal to MEGA65
228+
pub fn reset(&mut self) -> Result<()> {
229+
crate::serial::reset(&mut self.port)?;
230+
self.add_message("Reset MEGA65");
231+
Ok(())
232+
}
262233
}

src/textui/terminal.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> Result<()> {
6767
match key.code {
6868
KeyCode::Char('q') => return Ok(()),
6969
KeyCode::Char('h') => app.toggle_help(),
70-
KeyCode::Esc => app.escape_to_filehost_browser(),
71-
KeyCode::Char('R') => {
72-
crate::serial::reset(&mut app.port)?;
73-
app.add_message("Reset MEGA65");
74-
}
70+
KeyCode::Char('s') => app.sort_filehost(),
71+
KeyCode::Esc => app.return_to_filehost(),
72+
KeyCode::Up => app.previous_item(),
73+
KeyCode::Down => app.next_item(),
74+
// We check for ENTER twice in order to allow update display to/from BUSY state
7575
KeyCode::Enter => {
7676
if app.cbm_browser.is_selected() {
7777
app.busy = true;
@@ -82,15 +82,24 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> Result<()> {
8282
}
8383
_ => {}
8484
}
85-
match app.keypress(key.code) {
85+
let result = match key.code {
86+
KeyCode::Char('R') => app.reset(),
87+
KeyCode::Enter => match app.active_widget {
88+
AppWidgets::FileSelector => app.select_filehost_item(),
89+
AppWidgets::FileAction => app.select_file_action(),
90+
AppWidgets::CBMBrowser => app.select_cbm_item(),
91+
_ => Ok(()),
92+
},
93+
_ => Ok(()),
94+
};
95+
match result {
8696
Ok(()) => {}
8797
Err(error) => {
8898
app.add_message(error.to_string().as_str());
8999
app.cbm_browser.unselect();
90100
app.active_widget = AppWidgets::FileSelector;
91101
}
92102
}
93-
//app.ok_message();
94103
}
95104
}
96105
}

0 commit comments

Comments
 (0)