Skip to content

Commit bbd8303

Browse files
committed
keypress refactor
1 parent 9e556a1 commit bbd8303

File tree

2 files changed

+48
-36
lines changed

2 files changed

+48
-36
lines changed

src/textui/mod.rs

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -98,44 +98,11 @@ impl App {
9898
// @todo this should be moved to ui.rs so that mod.rs is independent of crossterm
9999
pub fn keypress(&mut self, key: crossterm::event::KeyCode) -> Result<()> {
100100
match key {
101-
KeyCode::Char('h') => self.toggle_help(),
102-
103-
// Escape jumps back to filehost selector
104-
KeyCode::Esc => {
105-
self.set_current_widget(AppWidgets::FileSelector);
106-
self.file_action.unselect();
107-
}
108-
109101
KeyCode::Enter => {
110102
match self.active_widget {
111-
// Enter in file selector triggers an action on the selected file
112-
AppWidgets::FileSelector => {
113-
self.active_widget = AppWidgets::FileAction;
114-
if !self.file_action.is_selected() {
115-
self.file_action.state.select(Some(0));
116-
}
117-
}
118-
// Enter in action widget trigges an action on the prg
119-
AppWidgets::FileAction => {
120-
self.set_current_widget(AppWidgets::FileSelector);
121-
match self.file_action.state.selected() {
122-
Some(0) => self.run(false)?, // run
123-
Some(1) => self.run(true)?, // reset, then run
124-
Some(2) => self.activate_cbm_browser()?,
125-
_ => {}
126-
};
127-
self.file_action.unselect();
128-
}
129-
AppWidgets::CBMBrowser => {
130-
match self.cbm_browser.state.selected() {
131-
_ => {
132-
self.run(false)?;
133-
self.busy = false;
134-
self.active_widget = AppWidgets::FileSelector;
135-
}
136-
};
137-
self.file_action.unselect();
138-
}
103+
AppWidgets::FileSelector => self.select_filehost_item()?,
104+
AppWidgets::FileAction => self.select_file_action()?,
105+
AppWidgets::CBMBrowser => self.select_cbm_item()?,
139106
_ => {}
140107
}
141108
}
@@ -183,6 +150,49 @@ impl App {
183150
}
184151
}
185152

153+
fn escape_to_filehost_browser(&mut self) {
154+
self.set_current_widget(AppWidgets::FileSelector);
155+
self.file_action.unselect();
156+
}
157+
158+
/// Select currently highlighted file in FileHost browser
159+
fn select_filehost_item(&mut self) -> Result<(), anyhow::Error> {
160+
// when selecting file, go to file action widget
161+
self.active_widget = AppWidgets::FileAction;
162+
if !self.file_action.is_selected() {
163+
self.file_action.state.select(Some(0));
164+
};
165+
Ok(())
166+
}
167+
168+
/// Select currently highlighted action in file action widget
169+
fn select_file_action(&mut self) -> Result<(), anyhow::Error> {
170+
// when done, return to filehost browser
171+
self.set_current_widget(AppWidgets::FileSelector);
172+
match self.file_action.state.selected() {
173+
Some(0) => self.run(false)?, // run
174+
Some(1) => self.run(true)?, // reset, then run
175+
Some(2) => self.activate_cbm_browser()?,
176+
_ => {}
177+
};
178+
self.file_action.unselect();
179+
Ok(())
180+
}
181+
182+
/// Select currently highlighted item in CBM browser
183+
fn select_cbm_item(&mut self) -> Result<(), anyhow::Error> {
184+
match self.cbm_browser.state.selected() {
185+
_ => {
186+
self.run(false)?;
187+
self.busy = false;
188+
self.active_widget = AppWidgets::FileSelector;
189+
}
190+
};
191+
self.cbm_browser.unselect();
192+
self.file_action.unselect();
193+
Ok(())
194+
}
195+
186196
/// Toggles the help pop-up
187197
fn toggle_help(&mut self) {
188198
if self.active_widget != AppWidgets::Help {

src/textui/terminal.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> Result<()> {
6666
if let Event::Key(key) = event::read()? {
6767
match key.code {
6868
KeyCode::Char('q') => return Ok(()),
69+
KeyCode::Char('h') => app.toggle_help(),
70+
KeyCode::Esc => app.escape_to_filehost_browser(),
6971
KeyCode::Char('R') => {
7072
crate::serial::reset(&mut app.port)?;
7173
app.add_message("Reset MEGA65");

0 commit comments

Comments
 (0)