summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManos Pitsidianakis <el13635@mail.ntua.gr>2019-07-15 00:04:00 +0300
committerManos Pitsidianakis <el13635@mail.ntua.gr>2019-07-15 00:12:01 +0300
commitf13da6a26a64f8cf29de161bb50dbf396696e094 (patch)
treecddbf20cc9e95fe5f7d4f24da122fe89cfb1db64
parentf3d019f7ed1acdb65985e55e0315f6dba7978275 (diff)
ui: Add pipe action for Pager
-rw-r--r--ui/src/components/utilities.rs37
-rw-r--r--ui/src/execute.rs26
-rw-r--r--ui/src/execute/actions.rs6
3 files changed, 67 insertions, 2 deletions
diff --git a/ui/src/components/utilities.rs b/ui/src/components/utilities.rs
index cca21c6a..4b3d4cea 100644
--- a/ui/src/components/utilities.rs
+++ b/ui/src/components/utilities.rs
@@ -493,7 +493,7 @@ impl Component for Pager {
}
fn process_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool {
let shortcuts = &self.get_shortcuts(context)[Self::DESCRIPTION];
- match *event {
+ match event {
UIEvent::Input(ref key) if *key == shortcuts["scroll_up"] => {
if self.cursor_pos > 0 {
self.cursor_pos -= 1;
@@ -521,6 +521,41 @@ impl Component for Pager {
UIEvent::ChangeMode(UIMode::Normal) => {
self.dirty = true;
}
+ UIEvent::Action(Pager(Pipe(ref bin, ref args))) => {
+ use std::io::Write;
+ use std::process::{Command, Stdio};
+ let mut command_obj = match Command::new(bin)
+ .args(args.as_slice())
+ .stdin(Stdio::piped())
+ .stdout(Stdio::piped())
+ .spawn()
+ {
+ Ok(o) => o,
+ Err(e) => {
+ context.replies.push_back(UIEvent::StatusEvent(
+ StatusEvent::DisplayMessage(format!(
+ "Could not pipe to {}: {}",
+ bin, e
+ )),
+ ));
+ return true;
+ }
+ };
+ let stdin = command_obj.stdin.as_mut().expect("failed to open stdin");
+ stdin
+ .write_all(self.text.as_bytes())
+ .expect("Failed to write to stdin");
+
+ context
+ .replies
+ .push_back(UIEvent::StatusEvent(StatusEvent::DisplayMessage(format!(
+ "Pager text piped to '{}{}{}'",
+ &bin,
+ if args.is_empty() { "" } else { " " },
+ args.join(", ")
+ ))));
+ return true;
+ }
UIEvent::Resize => {
self.dirty = true;
self.max_cursor_pos = None;
diff --git a/ui/src/execute.rs b/ui/src/execute.rs
index 7008064a..a7833415 100644
--- a/ui/src/execute.rs
+++ b/ui/src/execute.rs
@@ -28,6 +28,7 @@ pub mod actions;
pub use crate::actions::Action::{self, *};
pub use crate::actions::ListingAction::{self, *};
pub use crate::actions::MailingListAction::{self, *};
+pub use crate::actions::PagerAction::{self, *};
pub use crate::actions::TabAction::{self, *};
/* Create a const table with every command part that can be auto-completed and its description */
@@ -161,6 +162,29 @@ define_commands!([
)
);
)
+ },
+ /* Pipe pager contents to binary */
+ { tags: ["pipe "],
+ desc: "pipe EXECUTABLE ARGS",
+ parser:(
+ named!( pipe<Action>,
+ alt_complete!(
+ do_parse!(
+ ws!(tag!("pipe"))
+ >> bin: map_res!(is_not!(" "), std::str::from_utf8)
+ >> is_a!(" ")
+ >> args: separated_list!(is_a!(" "), is_not!(" "))
+ >> ({
+ Pager(Pipe(bin.to_string(), args.into_iter().map(|v| String::from_utf8(v.to_vec()).unwrap()).collect::<Vec<String>>()))
+ })) | do_parse!(
+ ws!(tag!("pipe"))
+ >> bin: ws!(map_res!(is_not!(" "), std::str::from_utf8))
+ >> ({
+ Pager(Pipe(bin.to_string(), Vec::new()))
+ })
+ ))
+ );
+ )
}
]);
@@ -207,5 +231,5 @@ named!(
alt_complete!(toggle | envelope_action | filter | toggle_thread_snooze)
);
named!(pub parse_command<Action>,
- alt_complete!( goto | listing_action | sort | subsort | close | mailinglist | setenv | printenv)
+ alt_complete!( goto | listing_action | sort | subsort | close | mailinglist | setenv | printenv | pipe)
);
diff --git a/ui/src/execute/actions.rs b/ui/src/execute/actions.rs
index 0b13bdf7..9b569c53 100644
--- a/ui/src/execute/actions.rs
+++ b/ui/src/execute/actions.rs
@@ -60,6 +60,11 @@ pub enum MailingListAction {
}
#[derive(Debug)]
+pub enum PagerAction {
+ Pipe(String, Vec<String>),
+}
+
+#[derive(Debug)]
pub enum Action {
Listing(ListingAction),
ViewMailbox(usize),
@@ -68,6 +73,7 @@ pub enum Action {
Tab(TabAction),
ToggleThreadSnooze,
MailingListAction(MailingListAction),
+ Pager(PagerAction),
SetEnv(String, String),
PrintEnv(String),
}