From d8d43a16fef045a2116ff126e7b6e27817b526fc Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Mon, 19 Sep 2022 21:40:12 +0300 Subject: [PATCH] HtmlView: add html_open config setting Add config setting in case xdg query default app for text/html mime type doesn't yield results. --- docs/meli.conf.5 | 5 +++++ src/components/mail/view.rs | 14 ++++++++++---- src/components/mail/view/html.rs | 22 ++++++++++++++++++++-- src/conf/overrides.rs | 6 ++++++ src/conf/pager.rs | 13 +++++++++++++ 5 files changed, 54 insertions(+), 6 deletions(-) diff --git a/docs/meli.conf.5 b/docs/meli.conf.5 index 8c33508d..81383d46 100644 --- a/docs/meli.conf.5 +++ b/docs/meli.conf.5 @@ -1020,6 +1020,11 @@ Always show headers when scrolling. Pipe html attachments through this filter before display .\" default value .Pq Em none +.It Ic html_open Ar String +.Pq Em optional +A command to open html files. +.\" default value +.Pq Em none .It Ic filter Ar String .Pq Em optional A command to pipe mail output through for viewing in pager. diff --git a/src/components/mail/view.rs b/src/components/mail/view.rs index e45fdca9..36bae497 100644 --- a/src/components/mail/view.rs +++ b/src/components/mail/view.rs @@ -1431,7 +1431,9 @@ impl Component for MailView { let mut text = "Viewing attachment. Press `r` to return \n".to_string(); if let Some(attachment) = self.open_attachment(aidx, context) { if attachment.is_html() { - self.subview = Some(Box::new(HtmlView::new(attachment, context))); + let mut subview = Box::new(HtmlView::new(attachment, context)); + subview.set_coordinates(Some(self.coordinates)); + self.subview = Some(subview); self.mode = ViewMode::Subview; } else { text.push_str(&attachment.text()); @@ -1462,7 +1464,9 @@ impl Component for MailView { } } ViewMode::Normal if body.is_html() => { - self.subview = Some(Box::new(HtmlView::new(body, context))); + let mut subview = Box::new(HtmlView::new(body, context)); + subview.set_coordinates(Some(self.coordinates)); + self.subview = Some(subview); self.mode = ViewMode::Subview; } ViewMode::Normal @@ -1483,7 +1487,7 @@ impl Component for MailView { _ => false, } => { - self.subview = Some(Box::new(HtmlView::new( + let mut subview = Box::new(HtmlView::new( body.content_type .parts() .unwrap() @@ -1491,7 +1495,9 @@ impl Component for MailView { .find(|a| a.is_html()) .unwrap_or(body), context, - ))); + )); + subview.set_coordinates(Some(self.coordinates)); + self.subview = Some(subview); self.mode = ViewMode::Subview; self.initialised = false; } diff --git a/src/components/mail/view/html.rs b/src/components/mail/view/html.rs index ca9ec280..34f28fcf 100644 --- a/src/components/mail/view/html.rs +++ b/src/components/mail/view/html.rs @@ -27,6 +27,7 @@ use std::process::{Command, Stdio}; pub struct HtmlView { pager: Pager, bytes: Vec, + coordinates: Option<(AccountHash, MailboxHash, EnvelopeHash)>, id: ComponentId, } @@ -111,7 +112,16 @@ impl HtmlView { } let colors = crate::conf::value(context, "mail.view.body"); let pager = Pager::from_string(display_text, None, None, None, colors); - HtmlView { pager, bytes, id } + HtmlView { + pager, + bytes, + id, + coordinates: None, + } + } + + pub fn set_coordinates(&mut self, new_value: Option<(AccountHash, MailboxHash, EnvelopeHash)>) { + self.coordinates = new_value; } } @@ -131,7 +141,15 @@ impl Component for HtmlView { } if let UIEvent::Input(Key::Char('v')) = event { - if let Ok(command) = query_default_app("text/html") { + let command = if let Some(coordinates) = self.coordinates { + mailbox_settings!(context[coordinates.0][&coordinates.1].pager.html_open) + .as_ref() + .map(|s| s.to_string()) + .or_else(|| query_default_app("text/html").ok()) + } else { + query_default_app("text/html").ok() + }; + if let Some(command) = command { let p = create_temp_file(&self.bytes, None, None, true); let (exec_cmd, argument) = super::desktop_exec_to_command(&command, p.path.display().to_string(), false); diff --git a/src/conf/overrides.rs b/src/conf/overrides.rs index 73e97d40..344c2e0b 100644 --- a/src/conf/overrides.rs +++ b/src/conf/overrides.rs @@ -88,6 +88,11 @@ pub struct PagerSettingsOverride { #[serde(deserialize_with = "non_empty_string")] #[serde(default)] pub url_launcher: Option>, + #[doc = " A command to open html files."] + #[doc = " Default: None"] + #[serde(deserialize_with = "non_empty_string", alias = "html-open")] + #[serde(default)] + pub html_open: Option>, } impl Default for PagerSettingsOverride { fn default() -> Self { @@ -104,6 +109,7 @@ impl Default for PagerSettingsOverride { auto_choose_multipart_alternative: None, show_date_in_my_timezone: None, url_launcher: None, + html_open: None, } } } diff --git a/src/conf/pager.rs b/src/conf/pager.rs index 35fe8322..cce1dd45 100644 --- a/src/conf/pager.rs +++ b/src/conf/pager.rs @@ -87,14 +87,25 @@ pub struct PagerSettings { alias = "auto-choose-multipart-alternative" )] pub auto_choose_multipart_alternative: ToggleFlag, + /// Show Date: in my timezone /// Default: true #[serde(default = "internal_value_true", alias = "show-date-in-my-timezone")] pub show_date_in_my_timezone: ToggleFlag, + /// A command to launch URLs with. The URL will be given as the first argument of the command. /// Default: None #[serde(default = "none", deserialize_with = "non_empty_string")] pub url_launcher: Option, + + /// A command to open html files. + /// Default: None + #[serde( + default = "none", + deserialize_with = "non_empty_string", + alias = "html-open" + )] + pub html_open: Option, } impl Default for PagerSettings { @@ -106,6 +117,7 @@ impl Default for PagerSettings { pager_ratio: 80, filter: None, html_filter: None, + html_open: None, format_flowed: true, split_long_lines: true, minimum_width: 80, @@ -128,6 +140,7 @@ impl DotAddressable for PagerSettings { "pager_ratio" => self.pager_ratio.lookup(field, tail), "filter" => self.filter.lookup(field, tail), "html_filter" => self.html_filter.lookup(field, tail), + "html_open" => self.html_open.lookup(field, tail), "format_flowed" => self.format_flowed.lookup(field, tail), "split_long_lines" => self.split_long_lines.lookup(field, tail), "minimum_width" => self.minimum_width.lookup(field, tail),