diff options
author | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2018-08-05 16:26:42 +0300 |
---|---|---|
committer | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2019-06-10 19:40:26 +0300 |
commit | e4760e4d25f0ed9bf41f72a033254a891f415316 (patch) | |
tree | 2e42c9ada4f8e310744de72ab449369e86b72411 | |
parent | 375b256a4e8eead6a4d7989b667554d542723abb (diff) |
Make parser for ex commands and move actions to their own mod
-rw-r--r-- | ui/src/components/mail/listing.rs | 13 | ||||
-rw-r--r-- | ui/src/components/utilities.rs | 2 | ||||
-rw-r--r-- | ui/src/execute/actions.rs | 10 | ||||
-rw-r--r-- | ui/src/execute/mod.rs | 31 | ||||
-rw-r--r-- | ui/src/helpers.rs | 7 | ||||
-rw-r--r-- | ui/src/lib.rs | 19 |
6 files changed, 59 insertions, 23 deletions
diff --git a/ui/src/components/mail/listing.rs b/ui/src/components/mail/listing.rs index 52f6df9a..f9b81c2d 100644 --- a/ui/src/components/mail/listing.rs +++ b/ui/src/components/mail/listing.rs @@ -1,10 +1,6 @@ use super::*; const MAX_COLS: usize = 500; -#[derive(Debug)] -pub enum MailListingAction { - ToggleThreaded, -} /// A list of all mail (`Envelope`s) in a `Mailbox`. On `\n` it opens the `Envelope` content in a /// `Pager`. @@ -582,7 +578,16 @@ impl Component for MailListing { .threaded; self.refresh_mailbox(context); self.dirty = true; + return; + }, + Action::ViewMailbox(idx) => { + eprintln!("listing got viewmailbox({})", idx); + self.new_cursor_pos.1 = *idx; + self.dirty = true; + self.refresh_mailbox(context); + return; } + _ => {}, }, _ => {} } diff --git a/ui/src/components/utilities.rs b/ui/src/components/utilities.rs index e2c7de65..f605ea77 100644 --- a/ui/src/components/utilities.rs +++ b/ui/src/components/utilities.rs @@ -428,7 +428,7 @@ impl Component for StatusBar { self.dirty = true; } UIEventType::ChangeMode(m) => { - let offset = self.status.find('|').unwrap_or(self.status.len()); + let offset = self.status.find('|').unwrap_or_else(|| self.status.len()); self.status.replace_range(..offset, &format!("{} ", m)); self.dirty = true; self.mode = m.clone(); diff --git a/ui/src/execute/actions.rs b/ui/src/execute/actions.rs new file mode 100644 index 00000000..f4ccb55e --- /dev/null +++ b/ui/src/execute/actions.rs @@ -0,0 +1,10 @@ +#[derive(Debug)] +pub enum MailListingAction { + ToggleThreaded, +} + +#[derive(Debug)] +pub enum Action { + MailListing(MailListingAction), + ViewMailbox(usize), +} diff --git a/ui/src/execute/mod.rs b/ui/src/execute/mod.rs index 53b7ed9b..e8933501 100644 --- a/ui/src/execute/mod.rs +++ b/ui/src/execute/mod.rs @@ -1,7 +1,10 @@ /*! A parser module for user commands passed through the Ex mode. */ -use nom::digit; +use nom::{digit, }; use std; +pub mod actions; +pub use actions::*; + named!( usize_c<usize>, @@ -11,14 +14,22 @@ named!( ) ); -named!(pub goto<usize>, +named!(goto<Action>, preceded!(tag!("b "), - call!(usize_c)) - ); + map!(call!(usize_c), |v| Action::ViewMailbox(v)) + )); + +//named!(sort<&str>, +// preceded!(tag!("sort "), +// map_res!(call!(alpha), std::str::from_utf8)) +// ); + +named!(threaded<Action>, + map!(ws!(tag!("threaded")), |_| Action::MailListing(MailListingAction::ToggleThreaded))); +named!(toggle<Action>, + preceded!(tag!("toggle "), + alt_complete!( threaded ))); -/* -named!(pub sort<&str>, - preceded!(tag!("sort "), - map_res!(call!(alpha), std::str::from_utf8)) - ); - */ +named!(pub parse_command<Action>, + alt_complete!( goto | toggle) + ); diff --git a/ui/src/helpers.rs b/ui/src/helpers.rs index 8576ee53..e5484018 100644 --- a/ui/src/helpers.rs +++ b/ui/src/helpers.rs @@ -9,6 +9,12 @@ pub struct File { path: PathBuf, } +impl Drop for File { + fn drop(&mut self) { + std::fs::remove_file(self.path()).unwrap_or_else(|_| {}); + } +} + impl File { pub fn file(&mut self) -> std::fs::File { std::fs::File::create(&self.path).unwrap() @@ -18,7 +24,6 @@ impl File { } } -//TODO: add temp files to a list to reap them when dropped pub fn create_temp_file(bytes: &[u8], filename: Option<&PathBuf>) -> File { let mut dir = std::env::temp_dir(); diff --git a/ui/src/lib.rs b/ui/src/lib.rs index 8b20b338..ce7a332c 100644 --- a/ui/src/lib.rs +++ b/ui/src/lib.rs @@ -37,10 +37,10 @@ pub use helpers::*; #[macro_use] mod execute; +use execute::*; use self::cells::*; pub use self::components::*; pub use self::position::*; -use execute::goto; extern crate melib; extern crate mime_apps; @@ -66,10 +66,6 @@ use termion::{clear, cursor, style}; extern crate nom; use chan::Sender; -#[derive(Debug)] -pub enum Action { - MailListing(MailListingAction), -} /// `ThreadEvent` encapsulates all of the possible values we need to transfer between our threads /// to the main process. @@ -390,10 +386,13 @@ impl<W: Write> State<W> { fn parse_command(&mut self, cmd: String) { //TODO: Make ex mode useful - let result = goto(&cmd.as_bytes()).to_full_result(); + let result = parse_command(&cmd.as_bytes()).to_full_result(); if let Ok(v) = result { - self.refresh_mailbox(0, v); + eprintln!("result is {:?}", v); + self.rcv_event(UIEvent { id: 0, event_type: UIEventType::Action(v) }); +; + //self.refresh_mailbox(0, v); } } @@ -450,6 +449,12 @@ impl<W: Write> State<W> { for i in 0..self.entities.len() { self.entities[i].rcv_event(&event, &mut self.context); } + + if !self.context.replies.is_empty() { + let replies: Vec<UIEvent>= self.context.replies.drain(0..).collect(); + // Pass replies to self and call count on the map iterator to force evaluation + replies.into_iter().map(|r| self.rcv_event(r)).count(); + } } /// Tries to load a mailbox's content |