ui: print and set environment variables

master
Manos Pitsidianakis 2019-06-26 18:56:29 +03:00
parent 8fefdf80ad
commit 34054d46ea
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
3 changed files with 39 additions and 2 deletions

View File

@ -135,6 +135,26 @@ named!(
)
);
named!(
setenv<Action>,
do_parse!(
ws!(tag!("setenv"))
>> key: map_res!(take_until1!("="), std::str::from_utf8)
>> ws!(tag!("="))
>> val: map_res!(call!(not_line_ending), std::str::from_utf8)
>> (SetEnv(key.to_string(), val.to_string()))
)
);
named!(
printenv<Action>,
do_parse!(
ws!(tag!("env"))
>> key: map_res!(call!(not_line_ending), std::str::from_utf8)
>> (PrintEnv(key.to_string()))
)
);
named!(pub parse_command<Action>,
alt_complete!( goto | listing_action | sort | subsort | close | mailinglist)
alt_complete!( goto | listing_action | sort | subsort | close | mailinglist | setenv | printenv)
);

View File

@ -68,4 +68,6 @@ pub enum Action {
Tab(TabAction),
ToggleThreadSnooze,
MailingListAction(MailingListAction),
SetEnv(String, String),
PrintEnv(String),
}

View File

@ -33,6 +33,7 @@ use melib::backends::{FolderHash, NotifyFn};
use chan::{Receiver, Sender};
use fnv::FnvHashMap;
use std::env;
use std::io::Write;
use std::result;
use std::thread;
@ -471,7 +472,21 @@ impl State {
let result = parse_command(&cmd.as_bytes()).to_full_result();
if let Ok(v) = result {
self.rcv_event(UIEvent::Action(v));
match v {
SetEnv(key, val) => {
env::set_var(key.as_str(), val.as_str());
}
PrintEnv(key) => {
self.context.replies.push_back(UIEvent::StatusEvent(
StatusEvent::DisplayMessage(
env::var(key.as_str()).unwrap_or_else(|e| e.to_string()),
),
));
}
v => {
self.rcv_event(UIEvent::Action(v));
}
}
}
}