diff --git a/src/command.rs b/src/command.rs index a6fccc66..96454e91 100644 --- a/src/command.rs +++ b/src/command.rs @@ -651,13 +651,27 @@ define_commands!([ desc: "print ACCOUNT SETTING", tokens: &[One(Literal("print")), One(AccountName), One(QuotedStringValue)], parser:( - fn print_setting(input: &[u8]) -> IResult<&[u8], Action> { + fn print_account_setting(input: &[u8]) -> IResult<&[u8], Action> { let (input, _) = tag("print")(input.trim())?; let (input, _) = is_a(" ")(input)?; let (input, account) = quoted_argument(input)?; let (input, _) = is_a(" ")(input)?; let (input, setting) = quoted_argument(input)?; - Ok((input, AccountAction(account.to_string(), PrintSetting(setting.to_string())))) + let (input, _) = eof(input)?; + Ok((input, AccountAction(account.to_string(), PrintAccountSetting(setting.to_string())))) + } + ) + }, + { tags: ["print "], + desc: "print SETTING", + tokens: &[One(Literal("print")), One(QuotedStringValue)], + parser:( + fn print_setting(input: &[u8]) -> IResult<&[u8], Action> { + let (input, _) = tag("print")(input.trim())?; + let (input, _) = is_a(" ")(input)?; + let (input, setting) = quoted_argument(input)?; + let (input, _) = eof(input)?; + Ok((input, PrintSetting(setting.to_string()))) } ) } @@ -719,7 +733,7 @@ fn compose_action(input: &[u8]) -> IResult<&[u8], Action> { } fn account_action(input: &[u8]) -> IResult<&[u8], Action> { - alt((reindex, print_setting))(input) + alt((reindex, print_account_setting))(input) } fn view(input: &[u8]) -> IResult<&[u8], Action> { @@ -744,6 +758,7 @@ pub fn parse_command(input: &[u8]) -> Result { delete_mailbox, rename_mailbox, account_action, + print_setting, ))(input) .map(|(_, v)| v) .map_err(|err| err.into()) diff --git a/src/command/actions.rs b/src/command/actions.rs index 4bc6446c..13d128a9 100644 --- a/src/command/actions.rs +++ b/src/command/actions.rs @@ -91,7 +91,7 @@ pub enum ComposeAction { #[derive(Debug)] pub enum AccountAction { ReIndex, - PrintSetting(String), + PrintAccountSetting(String), } #[derive(Debug)] @@ -120,6 +120,7 @@ pub enum Action { Compose(ComposeAction), Mailbox(AccountName, MailboxOperation), AccountAction(AccountName, AccountAction), + PrintSetting(String), } impl Action { @@ -138,6 +139,7 @@ impl Action { Action::Compose(_) => false, Action::Mailbox(_, _) => true, Action::AccountAction(_, _) => false, + Action::PrintSetting(_) => false, } } } diff --git a/src/conf.rs b/src/conf.rs index 21d316aa..a85bff19 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -849,6 +849,7 @@ impl DotAddressable for IndexStyle {} impl DotAddressable for u64 {} impl DotAddressable for crate::terminal::Color {} impl DotAddressable for crate::terminal::Attr {} +impl DotAddressable for crate::terminal::Key {} impl DotAddressable for usize {} impl DotAddressable for Query {} impl DotAddressable for melib::LoggingLevel {} @@ -880,6 +881,7 @@ impl DotAddressable for LogSettings { } } } + impl DotAddressable for Settings { fn lookup(&self, parent_field: &str, path: &[&str]) -> Result { match path.first() { @@ -890,7 +892,7 @@ impl DotAddressable for Settings { "pager" => self.pager.lookup(field, tail), "listing" => self.listing.lookup(field, tail), "notifications" => Err(MeliError::new("unimplemented")), - "shortcuts" => Err(MeliError::new("unimplemented")), + "shortcuts" => self.shortcuts.lookup(field, tail), "tags" => Err(MeliError::new("unimplemented")), "composing" => Err(MeliError::new("unimplemented")), "pgp" => Err(MeliError::new("unimplemented")), diff --git a/src/conf/shortcuts.rs b/src/conf/shortcuts.rs index f6b40abb..04acbd72 100644 --- a/src/conf/shortcuts.rs +++ b/src/conf/shortcuts.rs @@ -19,7 +19,9 @@ * along with meli. If not, see . */ +use super::DotAddressable; use crate::terminal::Key; +use melib::{MeliError, Result}; use std::collections::HashMap; #[macro_export] @@ -67,6 +69,33 @@ impl Default for Shortcuts { } } +impl DotAddressable for Shortcuts { + fn lookup(&self, parent_field: &str, path: &[&str]) -> Result { + match path.first() { + Some(field) => { + let tail = &path[1..]; + match *field { + "general" => self.general.lookup(field, tail), + "listing" => self.listing.lookup(field, tail), + "composing" => self.composing.lookup(field, tail), + "compact_listing" | "compact-listing" => { + self.compact_listing.lookup(field, tail) + } + "contact_list" | "contact-list" => self.contact_list.lookup(field, tail), + "envelope_view" | "envelope-view" => self.envelope_view.lookup(field, tail), + "thread_view" | "thread-view" => self.thread_view.lookup(field, tail), + "pager" => self.pager.lookup(field, tail), + other => Err(MeliError::new(format!( + "{} has no field named {}", + parent_field, other + ))), + } + } + None => Ok(toml::to_string(self).map_err(|err| err.to_string())?), + } + } +} + /// Create a struct holding all of a Component's shortcuts. #[macro_export] macro_rules! shortcut_key_values { @@ -105,6 +134,24 @@ macro_rules! shortcut_key_values { } } } + + impl DotAddressable for $name { + fn lookup(&self, parent_field: &str, path: &[&str]) -> Result { + match path.first() { + Some(field) => { + let tail = &path[1..]; + match *field { + $(stringify!($fname) => self.$fname.lookup(field, tail),)* + other => Err(MeliError::new(format!( + "{} has no field named {}", + parent_field, other + ))), + } + } + None => Ok(toml::to_string(self).map_err(|err| err.to_string())?), + } + } + } } } diff --git a/src/state.rs b/src/state.rs index a3858c9f..2adafcd5 100644 --- a/src/state.rs +++ b/src/state.rs @@ -931,7 +931,35 @@ impl State { Some(NotificationType::ERROR), )); } - AccountAction(ref _account_name, PrintSetting(ref setting)) => { + AccountAction(ref account_name, PrintAccountSetting(ref setting)) => { + /* + let path = setting.split(".").collect::>(); + if let Some(pos) = self + .context + .accounts + .iter() + .position(|a| a.name() == account_name) + { + self.context.replies.push_back(UIEvent::StatusEvent( + StatusEvent::UpdateStatus(format!( + "{}", + self.context.accounts[pos] + .settings + .lookup("settings", &path) + .unwrap_or_else(|err| err.to_string()) + )), + )); + } else { + self.context.replies.push_back(UIEvent::Notification( + None, + format!("Account {} was not found.", account_name), + Some(NotificationType::ERROR), + )); + return; + } + */ + } + PrintSetting(ref setting) => { let path = setting.split(".").collect::>(); self.context .replies