diff options
author | Canop <cano.petrole@gmail.com> | 2019-12-19 18:02:17 +0100 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2019-12-19 18:02:17 +0100 |
commit | f1ae27205a3d6b22b5dbddf6cee869fb19526fdf (patch) | |
tree | 961a810b5f7ca72d1595ff6ad697edc49e7f9203 | |
parent | 7c5050ab77490bcd50731a4ea66f539039bdf7ef (diff) |
better handling of resize
- don't query the size after the start (use the resize event)
- recompute tree for the correct height
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | src/app.rs | 7 | ||||
-rw-r--r-- | src/app_state.rs | 2 | ||||
-rw-r--r-- | src/browser_states.rs | 46 | ||||
-rw-r--r-- | src/browser_verbs.rs | 2 | ||||
-rw-r--r-- | src/commands.rs | 4 | ||||
-rw-r--r-- | src/help_states.rs | 35 | ||||
-rw-r--r-- | src/screens.rs | 10 | ||||
-rw-r--r-- | src/verbs.rs | 2 |
9 files changed, 58 insertions, 52 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index d41ee2a..6023f2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ ### master - should not panic anymore when opening arbitrary files on server - allow more keys for verbs. For example you can use `enter` (this one won't apply on directories but only on files) +- display all possible verb completions in status +- don't query the terminal size after start: use the new Resize event of Crossterm <a name="v0.10.4"></a> ### v0.10.4 - 2019-12-16 @@ -97,7 +97,6 @@ impl App { ) -> Result<Command, ProgramError> { let mut cmd = cmd; debug!("action: {:?}", &cmd.action); - screen.read_size(con)?; let mut error: Option<String> = None; let cmd_result = self.mut_state().apply(&mut cmd, screen, con)?; match cmd_result { @@ -113,8 +112,10 @@ impl App { self.push(boxed_state); cmd = new_cmd; } - AppStateCmdResult::RefreshState => { - file_sizes::clear_cache(); + AppStateCmdResult::RefreshState{clear_cache} => { + if clear_cache { + file_sizes::clear_cache(); + } cmd = self.mut_state().refresh(screen, con); } AppStateCmdResult::PopState => { diff --git a/src/app_state.rs b/src/app_state.rs index 6f8f8b8..2752c2a 100644 --- a/src/app_state.rs +++ b/src/app_state.rs @@ -19,7 +19,7 @@ pub enum AppStateCmdResult { NewState(Box<dyn AppState>, Command), PopStateAndReapply, // the state asks the command be executed on a previous state PopState, - RefreshState, + RefreshState{clear_cache: bool}, } impl AppStateCmdResult { diff --git a/src/browser_states.rs b/src/browser_states.rs index a322fdc..88d53b9 100644 --- a/src/browser_states.rs +++ b/src/browser_states.rs @@ -276,7 +276,7 @@ impl AppState for BrowserState { PrefixSearchResult::TooManyMatches(completions) => Status::new( task, Composite::from_inline(&format!( - "Possible completions: {}", + "Possible verbs: {}", completions.iter().map(|c| format!("*{}*", c)).collect::<Vec<String>>().join(", "), )), false, @@ -341,16 +341,6 @@ impl AppState for BrowserState { } Action::OpenSelection => self.open_selection_stay_in_broot(screen, con), Action::AltOpenSelection => self.open_selection_quit_broot(screen, con), - Action::VerbIndex(index) => { - let verb = &con.verb_store.verbs[*index]; - self.execute_verb(verb, &verb.invocation, screen, con) - } - Action::VerbInvocate(invocation) => match con.verb_store.search(&invocation.name) { - PrefixSearchResult::Match(verb) => { - self.execute_verb(verb, &invocation, screen, con) - } - _ => Ok(AppStateCmdResult::verb_not_found(&invocation.name)), - }, Action::FuzzyPatternEdit(pat) => { match pat.len() { 0 => { @@ -362,16 +352,6 @@ impl AppState for BrowserState { } Ok(AppStateCmdResult::Keep) } - Action::RegexEdit(pat, flags) => Ok(match Pattern::regex(pat, flags) { - Ok(regex_pattern) => { - self.pending_pattern = regex_pattern; - AppStateCmdResult::Keep - } - Err(e) => { - // FIXME details - AppStateCmdResult::DisplayError(format!("{}", e)) - } - }), Action::Help => Ok(AppStateCmdResult::NewState( Box::new(HelpState::new(screen, con)), Command::new(), @@ -390,6 +370,30 @@ impl AppState for BrowserState { } Ok(AppStateCmdResult::Keep) } + Action::RegexEdit(pat, flags) => Ok(match Pattern::regex(pat, flags) { + Ok(regex_pattern) => { + self.pending_pattern = regex_pattern; + AppStateCmdResult::Keep + } + Err(e) => { + // FIXME details + AppStateCmdResult::DisplayError(format!("{}", e)) + } + }), + Action::Resize(w, h) => { + screen.set_terminal_size(*w, *h, con); + Ok(AppStateCmdResult::RefreshState{clear_cache: false}) + } + Action::VerbIndex(index) => { + let verb = &con.verb_store.verbs[*index]; + self.execute_verb(verb, &verb.invocation, screen, con) + } + Action::VerbInvocate(invocation) => match con.verb_store.search(&invocation.name) { + PrefixSearchResult::Match(verb) => { + self.execute_verb(verb, &invocation, screen, con) + } + _ => Ok(AppStateCmdResult::verb_not_found(&invocation.name)), + }, _ => Ok(AppStateCmdResult::Keep), } } diff --git a/src/browser_verbs.rs b/src/browser_verbs.rs index 6de6a3b..c28f340 100644 --- a/src/browser_verbs.rs +++ b/src/browser_verbs.rs @@ -105,7 +105,7 @@ impl VerbExecutor for BrowserState { external::print_path(&self.displayed_tree().selected_line().target(), con)? } ":print_tree" => external::print_tree(&self.displayed_tree(), screen, con)?, - ":refresh" => AppStateCmdResult::RefreshState, + ":refresh" => AppStateCmdResult::RefreshState{clear_cache: true}, ":select_first" => { self.displayed_tree_mut().try_select_first(); AppStateCmdResult::Keep diff --git a/src/commands.rs b/src/commands.rs index 437693c..e97ca74 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -45,6 +45,7 @@ pub enum Action { Help, // goes to help state Click(u16, u16), // usually a mouse click DoubleClick(u16, u16), // always come after a simple click at same position + Resize(u16, u16), // terminal was resized to those dimensions Unparsed, // or unparsable } @@ -229,6 +230,9 @@ impl Command { return; } } + Event::Resize(w, h) => { + self.action = Action::Resize(*w, *h); + } Event::Wheel(lines_count) => { self.action = Action::MoveSelection(*lines_count); } diff --git a/src/help_states.rs b/src/help_states.rs index 9057c7c..2a7137a 100644 --- a/src/help_states.rs +++ b/src/help_states.rs @@ -26,12 +26,11 @@ use { }, }; - /// an application state dedicated to help pub struct HelpState { pub scroll: i32, // scroll position pub area: Area, - screen_size: (u16, u16), // kept to detect when the background should be cleared + dirty: bool, // background must be cleared } impl HelpState { @@ -40,23 +39,11 @@ impl HelpState { HelpState { area, scroll: 0, - screen_size: (0, 0), - } - } - - /// return true when the screen area changed - fn resize_area(&mut self, screen: &Screen) -> bool { - if self.screen_size == (screen.width, screen.height) { - return false; + dirty: true, } - self.screen_size = (screen.width, screen.height); - self.area = Area::new(0, 0, screen.width, screen.height - 2); - self.area.pad_for_max_width(110); - true } } - impl AppState for HelpState { fn has_pending_task(&self) -> bool { @@ -79,6 +66,15 @@ impl AppState for HelpState { ) -> Result<AppStateCmdResult, ProgramError> { Ok(match &cmd.action { Action::Back => AppStateCmdResult::PopState, + Action::MoveSelection(dy) => { + self.scroll += *dy; + AppStateCmdResult::Keep + } + Action::Resize(w, h) => { + screen.set_terminal_size(*w, *h, con); + self.dirty = true; + AppStateCmdResult::RefreshState{clear_cache: false} + } Action::VerbIndex(index) => { let verb = &con.verb_store.verbs[*index]; self.execute_verb(verb, &verb.invocation, screen, con)? @@ -89,10 +85,6 @@ impl AppState for HelpState { } _ => AppStateCmdResult::verb_not_found(&invocation.name), }, - Action::MoveSelection(dy) => { - self.scroll += *dy; - AppStateCmdResult::Keep - } _ => AppStateCmdResult::Keep, }) } @@ -111,9 +103,12 @@ impl AppState for HelpState { screen: &Screen, con: &AppContext ) -> Result<(), ProgramError> { - if self.resize_area(screen) { + if self.dirty { screen.skin.default.queue_bg(w)?; screen.clear(w)?; + self.area = Area::new(0, 0, screen.width, screen.height - 2); + self.area.pad_for_max_width(110); + self.dirty = false; } let text = help_content::build_text(con); let fmt_text = FmtText::from_text(&screen.help_skin, text, Some((self.area.width - 1) as usize)); diff --git a/src/screens.rs b/src/screens.rs index 79927a6..ae1e1e6 100644 --- a/src/screens.rs +++ b/src/screens.rs @@ -46,17 +46,17 @@ impl Screen { screen.read_size(con)?; Ok(screen) } - /// - /// Note: with the resize event we could theoretically avoid to call - /// this so often - pub fn read_size(&mut self, con: &AppContext) -> Result<(), ProgramError> { - let (w, h) = termimad::terminal_size(); + pub fn set_terminal_size(&mut self, w: u16, h: u16, con: &AppContext) { self.width = w; self.height = h; if let Some(h) = con.launch_args.height { self.height = h; } self.input_field.change_area(0, h-1, w - FLAGS_AREA_WIDTH); + } + pub fn read_size(&mut self, con: &AppContext) -> Result<(), ProgramError> { + let (w, h) = termimad::terminal_size(); + self.set_terminal_size(w, h, con); Ok(()) } /// move the cursor to x,y and clears the line. diff --git a/src/verbs.rs b/src/verbs.rs index 6b19a96..3c4dac2 100644 --- a/src/verbs.rs +++ b/src/verbs.rs @@ -276,7 +276,7 @@ impl Verb { match execution { Ok(()) => { debug!("ok"); - AppStateCmdResult::RefreshState + AppStateCmdResult::RefreshState{clear_cache: true} } Err(e) => { warn!("launchable failed : {:?}", e); |