Make parser for ex commands and move actions to their own mod

embed
Manos Pitsidianakis 2018-08-05 16:26:42 +03:00
parent 375b256a4e
commit e4760e4d25
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
6 changed files with 59 additions and 23 deletions

View File

@ -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;
}
_ => {},
},
_ => {}
}

View File

@ -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();

View File

@ -0,0 +1,10 @@
#[derive(Debug)]
pub enum MailListingAction {
ToggleThreaded,
}
#[derive(Debug)]
pub enum Action {
MailListing(MailListingAction),
ViewMailbox(usize),
}

View File

@ -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!(pub sort<&str>,
preceded!(tag!("sort "),
map_res!(call!(alpha), std::str::from_utf8))
);
*/
//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 parse_command<Action>,
alt_complete!( goto | toggle)
);

View File

@ -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();

View File

@ -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