From 34054d46ea7a3d5543e3cceeb7b77d175e7ebbcc Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Wed, 26 Jun 2019 18:56:29 +0300 Subject: [PATCH] ui: print and set environment variables --- ui/src/execute.rs | 22 +++++++++++++++++++++- ui/src/execute/actions.rs | 2 ++ ui/src/state.rs | 17 ++++++++++++++++- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/ui/src/execute.rs b/ui/src/execute.rs index c48b27395..e19b50516 100644 --- a/ui/src/execute.rs +++ b/ui/src/execute.rs @@ -135,6 +135,26 @@ named!( ) ); +named!( + setenv, + 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, + do_parse!( + ws!(tag!("env")) + >> key: map_res!(call!(not_line_ending), std::str::from_utf8) + >> (PrintEnv(key.to_string())) + ) +); + named!(pub parse_command, - alt_complete!( goto | listing_action | sort | subsort | close | mailinglist) + alt_complete!( goto | listing_action | sort | subsort | close | mailinglist | setenv | printenv) ); diff --git a/ui/src/execute/actions.rs b/ui/src/execute/actions.rs index 5b17a9473..0b13bdf72 100644 --- a/ui/src/execute/actions.rs +++ b/ui/src/execute/actions.rs @@ -68,4 +68,6 @@ pub enum Action { Tab(TabAction), ToggleThreadSnooze, MailingListAction(MailingListAction), + SetEnv(String, String), + PrintEnv(String), } diff --git a/ui/src/state.rs b/ui/src/state.rs index 96495f14f..36efc1fe1 100644 --- a/ui/src/state.rs +++ b/ui/src/state.rs @@ -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)); + } + } } }