diff options
author | Canop <cano.petrole@gmail.com> | 2019-01-11 17:27:32 +0100 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2019-01-11 17:27:32 +0100 |
commit | d856a79d9492f171d84ab2193226c7a96e3ab0bb (patch) | |
tree | 45082bb539b2d6057a54c817152359e43b504768 | |
parent | e5436616d25364a40cd5daae07a9bc7f5cb45119 (diff) |
more relevant information in the status line when using the arrow keys
-rw-r--r-- | src/browser_states.rs | 45 | ||||
-rw-r--r-- | src/commands.rs | 92 |
2 files changed, 70 insertions, 67 deletions
diff --git a/src/browser_states.rs b/src/browser_states.rs index 02932f7..6d43683 100644 --- a/src/browser_states.rs +++ b/src/browser_states.rs @@ -179,37 +179,36 @@ impl AppState for BrowserState { } fn write_status(&self, screen: &mut Screen, cmd: &Command, con: &AppContext) -> io::Result<()> { - match &cmd.action { - Action::PatternEdit(_) => { - screen.write_status_text("Hit <enter> to select, <esc> to remove the filter") - } - Action::VerbEdit(verb_key) => match con.verb_store.get(&verb_key) { - Some(verb) => screen.write_status_text( + if let Some(verb_key) = &cmd.parts.verb { + if let Some(verb) = con.verb_store.get(&verb_key) { + screen.write_status_text( &format!( "Hit <enter> to {} : {}", &verb.name, verb.description_for(&self) ) .to_string(), - ), - None => screen.write_status_text( + ) + } else { + screen.write_status_text( // TODO show what verbs start with the currently edited verb key "Type a verb then <enter> to execute it (hit '?' for the list of verbs)", - ), - }, - _ => { - let tree = self.displayed_tree(); - if tree.selection == 0 { - screen.write_status_text( - "Hit <enter> to quit, '?' for help, or type a few file's letters to navigate", - ) - } else { - let line = &tree.lines[tree.selection]; - screen.write_status_text(match line.is_dir() { - true => "Hit <enter> to focus, or type a space then a verb", - false => "Hit <enter> to open the file, or type a space then a verb", - }) - } + ) + } + } else if let Some(_) = &cmd.parts.pattern { + screen.write_status_text("Hit <enter> to select, <esc> to remove the filter") + } else { + let tree = self.displayed_tree(); + if tree.selection == 0 { + screen.write_status_text( + "Hit <enter> to quit, '?' for help, or type a few file's letters to navigate", + ) + } else { + let line = &tree.lines[tree.selection]; + screen.write_status_text(match line.is_dir() { + true => "Hit <enter> to focus, or type a space then a verb", + false => "Hit <enter> to open the file, or type a space then a verb", + }) } } } diff --git a/src/commands.rs b/src/commands.rs index 5ad96f1..0f8af76 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -5,22 +5,47 @@ use termion::event::Key; /// in the input. It's independant of the state of the application /// (verbs arent checked at this point) -struct CommandParts { - pattern: Option<String>, - verb: Option<String>, +#[derive(Debug)] +pub struct Command { + pub raw: String, // what's visible in the input + pub parts: CommandParts, // the parsed parts of the visible input + pub action: Action, // what's required, based on the last key (which may be not visible, like esc) +} + +#[derive(Debug, Clone)] +pub struct CommandParts { + pub pattern: Option<String>, + pub verb: Option<String>, // may be Some("") if the user already typed the separator +} + +#[derive(Debug)] +pub enum Action { + MoveSelection(i32), // up (neg) or down (positive) in the list + ScrollPage(i32), // in number of pages, not lines + OpenSelection, // open the selected line (which can't be the root by construct) + VerbEdit(String), // verb, unfinished + Verb(String), // verb + PatternEdit(String), // a pattern being edited + Back, // back to last app state, or clear pattern + Next, + Help(String), + Unparsed, // or unparsable } impl CommandParts { - fn from(raw: &str) -> CommandParts { - let mut cp = CommandParts { + fn new() -> CommandParts { + CommandParts { pattern: None, verb: None, - }; + } + } + fn from(raw: &str) -> CommandParts { + let mut cp = CommandParts::new(); lazy_static! { static ref RE: Regex = Regex::new( r"(?x) ^ - (?P<pattern>[^\s/:]*) + (?P<pattern>[^\s/:]+)? (?:[\s:]+(?P<verb>\S*))? $ " @@ -34,57 +59,35 @@ impl CommandParts { if let Some(verb) = c.name("verb") { cp.verb = Some(String::from(verb.as_str())); } - } else { - warn!("unexpected lack of capture"); } cp } } -#[derive(Debug)] -pub enum Action { - MoveSelection(i32), // up (neg) or down (positive) in the list - ScrollPage(i32), // in number of pages, not lines - OpenSelection, // open the selected line (which can't be the root by construct) - VerbEdit(String), // verb, unfinished - Verb(String), // verb - PatternEdit(String), // a pattern being edited - Back, // back to last app state, or clear pattern - Next, - Help(String), - Unparsed, // or unparsable -} - impl Action { - pub fn from(raw: &str, finished: bool) -> Action { - let cp = CommandParts::from(raw); - if let Some(verb) = cp.verb { + pub fn from(cp: &CommandParts, finished: bool) -> Action { + if let Some(verb) = &cp.verb { return match finished { false => Action::VerbEdit(String::from(verb.as_str())), true => Action::Verb(String::from(verb.as_str())), }; } - if let Some(pattern) = cp.pattern { + if finished { + return Action::OpenSelection; + } + if let Some(pattern) = &cp.pattern { let pattern = pattern.as_str(); - return match finished { - false => Action::PatternEdit(String::from(pattern)), - true => Action::OpenSelection, - }; + return Action::PatternEdit(String::from(pattern)); } Action::Unparsed } } -#[derive(Debug)] -pub struct Command { - pub raw: String, - pub action: Action, -} - impl Command { pub fn new() -> Command { Command { raw: String::new(), + parts: CommandParts::new(), action: Action::Unparsed, } } @@ -93,10 +96,9 @@ impl Command { // which would be cleaner) pub fn pop_verb(&self) -> Command { let mut c = Command::new(); - let cp = CommandParts::from(&self.raw); - if cp.verb.is_some() { - if let Some(pat) = cp.pattern { - c.raw = pat; + if self.parts.verb.is_some() { + if let Some(pat) = &self.parts.pattern { + c.raw = pat.to_owned(); } } c @@ -111,7 +113,7 @@ impl Command { self.action = Action::Help(self.raw.to_owned()); } Key::Char('\n') => { - self.action = Action::from(&self.raw, true); + self.action = Action::from(&self.parts, true); } Key::Up => { self.action = Action::MoveSelection(-1); @@ -127,7 +129,8 @@ impl Command { } Key::Char(c) => { self.raw.push(c); - self.action = Action::from(&self.raw, false); + self.parts = CommandParts::from(&self.raw); + self.action = Action::from(&self.parts, false); } Key::Esc => { self.action = Action::Back; @@ -137,7 +140,8 @@ impl Command { self.action = Action::Back; } else { self.raw.pop(); - self.action = Action::from(&self.raw, false); + self.parts = CommandParts::from(&self.raw); + self.action = Action::from(&self.parts, false); } } _ => {} |