conf/shortcuts.rs: implement DotAddressable for Shortcuts

memfd
Manos Pitsidianakis 2020-07-25 18:36:01 +03:00
parent 1cc1b0604c
commit 7fd511e149
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
5 changed files with 100 additions and 6 deletions

View File

@ -651,13 +651,27 @@ define_commands!([
desc: "print ACCOUNT SETTING", desc: "print ACCOUNT SETTING",
tokens: &[One(Literal("print")), One(AccountName), One(QuotedStringValue)], tokens: &[One(Literal("print")), One(AccountName), One(QuotedStringValue)],
parser:( 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, _) = tag("print")(input.trim())?;
let (input, _) = is_a(" ")(input)?; let (input, _) = is_a(" ")(input)?;
let (input, account) = quoted_argument(input)?; let (input, account) = quoted_argument(input)?;
let (input, _) = is_a(" ")(input)?; let (input, _) = is_a(" ")(input)?;
let (input, setting) = quoted_argument(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> { 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> { fn view(input: &[u8]) -> IResult<&[u8], Action> {
@ -744,6 +758,7 @@ pub fn parse_command(input: &[u8]) -> Result<Action, MeliError> {
delete_mailbox, delete_mailbox,
rename_mailbox, rename_mailbox,
account_action, account_action,
print_setting,
))(input) ))(input)
.map(|(_, v)| v) .map(|(_, v)| v)
.map_err(|err| err.into()) .map_err(|err| err.into())

View File

@ -91,7 +91,7 @@ pub enum ComposeAction {
#[derive(Debug)] #[derive(Debug)]
pub enum AccountAction { pub enum AccountAction {
ReIndex, ReIndex,
PrintSetting(String), PrintAccountSetting(String),
} }
#[derive(Debug)] #[derive(Debug)]
@ -120,6 +120,7 @@ pub enum Action {
Compose(ComposeAction), Compose(ComposeAction),
Mailbox(AccountName, MailboxOperation), Mailbox(AccountName, MailboxOperation),
AccountAction(AccountName, AccountAction), AccountAction(AccountName, AccountAction),
PrintSetting(String),
} }
impl Action { impl Action {
@ -138,6 +139,7 @@ impl Action {
Action::Compose(_) => false, Action::Compose(_) => false,
Action::Mailbox(_, _) => true, Action::Mailbox(_, _) => true,
Action::AccountAction(_, _) => false, Action::AccountAction(_, _) => false,
Action::PrintSetting(_) => false,
} }
} }
} }

View File

@ -849,6 +849,7 @@ impl DotAddressable for IndexStyle {}
impl DotAddressable for u64 {} impl DotAddressable for u64 {}
impl DotAddressable for crate::terminal::Color {} impl DotAddressable for crate::terminal::Color {}
impl DotAddressable for crate::terminal::Attr {} impl DotAddressable for crate::terminal::Attr {}
impl DotAddressable for crate::terminal::Key {}
impl DotAddressable for usize {} impl DotAddressable for usize {}
impl DotAddressable for Query {} impl DotAddressable for Query {}
impl DotAddressable for melib::LoggingLevel {} impl DotAddressable for melib::LoggingLevel {}
@ -880,6 +881,7 @@ impl DotAddressable for LogSettings {
} }
} }
} }
impl DotAddressable for Settings { impl DotAddressable for Settings {
fn lookup(&self, parent_field: &str, path: &[&str]) -> Result<String> { fn lookup(&self, parent_field: &str, path: &[&str]) -> Result<String> {
match path.first() { match path.first() {
@ -890,7 +892,7 @@ impl DotAddressable for Settings {
"pager" => self.pager.lookup(field, tail), "pager" => self.pager.lookup(field, tail),
"listing" => self.listing.lookup(field, tail), "listing" => self.listing.lookup(field, tail),
"notifications" => Err(MeliError::new("unimplemented")), "notifications" => Err(MeliError::new("unimplemented")),
"shortcuts" => Err(MeliError::new("unimplemented")), "shortcuts" => self.shortcuts.lookup(field, tail),
"tags" => Err(MeliError::new("unimplemented")), "tags" => Err(MeliError::new("unimplemented")),
"composing" => Err(MeliError::new("unimplemented")), "composing" => Err(MeliError::new("unimplemented")),
"pgp" => Err(MeliError::new("unimplemented")), "pgp" => Err(MeliError::new("unimplemented")),

View File

@ -19,7 +19,9 @@
* along with meli. If not, see <http://www.gnu.org/licenses/>. * along with meli. If not, see <http://www.gnu.org/licenses/>.
*/ */
use super::DotAddressable;
use crate::terminal::Key; use crate::terminal::Key;
use melib::{MeliError, Result};
use std::collections::HashMap; use std::collections::HashMap;
#[macro_export] #[macro_export]
@ -67,6 +69,33 @@ impl Default for Shortcuts {
} }
} }
impl DotAddressable for Shortcuts {
fn lookup(&self, parent_field: &str, path: &[&str]) -> Result<String> {
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. /// Create a struct holding all of a Component's shortcuts.
#[macro_export] #[macro_export]
macro_rules! shortcut_key_values { 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<String> {
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())?),
}
}
}
} }
} }

View File

@ -931,7 +931,35 @@ impl State {
Some(NotificationType::ERROR), Some(NotificationType::ERROR),
)); ));
} }
AccountAction(ref _account_name, PrintSetting(ref setting)) => { AccountAction(ref account_name, PrintAccountSetting(ref setting)) => {
/*
let path = setting.split(".").collect::<SmallVec<[&str; 16]>>();
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::<SmallVec<[&str; 16]>>(); let path = setting.split(".").collect::<SmallVec<[&str; 16]>>();
self.context self.context
.replies .replies