ui: Add pipe action for Pager

embed
Manos Pitsidianakis 2019-07-15 00:04:00 +03:00
parent f3d019f7ed
commit f13da6a26a
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
3 changed files with 67 additions and 2 deletions

View File

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

View File

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

View File

@ -59,6 +59,11 @@ pub enum MailingListAction {
ListUnsubscribe,
}
#[derive(Debug)]
pub enum PagerAction {
Pipe(String, Vec<String>),
}
#[derive(Debug)]
pub enum Action {
Listing(ListingAction),
@ -68,6 +73,7 @@ pub enum Action {
Tab(TabAction),
ToggleThreadSnooze,
MailingListAction(MailingListAction),
Pager(PagerAction),
SetEnv(String, String),
PrintEnv(String),
}