compose: add show_comments arg to attachment_displays_to_text()

Toggle display of attachment comments (for example "this html attachment
was rendered with X filter...") when rendering text.
jmap-eventsource
Manos Pitsidianakis 2020-11-24 10:30:54 +02:00
parent d4f508642a
commit 73372ff1e7
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
1 changed files with 89 additions and 33 deletions

View File

@ -94,6 +94,7 @@ impl ViewMode {
pub enum AttachmentDisplay { pub enum AttachmentDisplay {
InlineText { InlineText {
inner: Attachment, inner: Attachment,
comment: Option<String>,
text: String, text: String,
}, },
InlineOther { InlineOther {
@ -315,7 +316,7 @@ impl MailView {
self.attachment_tree = attachment_tree_s; self.attachment_tree = attachment_tree_s;
self.attachment_paths = paths; self.attachment_paths = paths;
let body_text = let body_text =
self.attachment_displays_to_text(&display, context); self.attachment_displays_to_text(&display, context, true);
self.state = MailViewState::Loaded { self.state = MailViewState::Loaded {
display, display,
body, body,
@ -421,12 +422,28 @@ impl MailView {
&self, &self,
displays: &[AttachmentDisplay], displays: &[AttachmentDisplay],
context: &mut Context, context: &mut Context,
show_comments: bool,
) -> String { ) -> String {
let mut acc = String::new(); let mut acc = String::new();
for d in displays { for d in displays {
use AttachmentDisplay::*; use AttachmentDisplay::*;
match d { match d {
InlineText { inner: _, text } => acc.push_str(&text), InlineText {
inner: _,
text,
comment: Some(comment),
} if show_comments => {
acc.push_str(comment);
if !acc.ends_with("\n\n") {
acc.push_str("\n\n");
}
acc.push_str(&text);
}
InlineText {
inner: _,
text,
comment: _,
} => acc.push_str(&text),
InlineOther { inner } => { InlineOther { inner } => {
if !acc.ends_with("\n\n") { if !acc.ends_with("\n\n") {
acc.push_str("\n\n"); acc.push_str("\n\n");
@ -443,33 +460,53 @@ impl MailView {
handle: _, handle: _,
job_id: _, job_id: _,
} => { } => {
acc.push_str("Waiting for signature verification.\n\n"); if show_comments {
acc.push_str(&self.attachment_displays_to_text(display, context)); acc.push_str("Waiting for signature verification.\n\n");
}
acc.push_str(&self.attachment_displays_to_text(
display,
context,
show_comments,
));
} }
SignedUnverified { inner: _, display } => { SignedUnverified { inner: _, display } => {
acc.push_str("Unverified signature.\n\n"); if show_comments {
acc.push_str(&self.attachment_displays_to_text(display, context)) acc.push_str("Unverified signature.\n\n");
}
acc.push_str(&self.attachment_displays_to_text(display, context, show_comments))
} }
SignedFailed { SignedFailed {
inner: _, inner: _,
display, display,
error, error,
} => { } => {
acc.push_str(&format!("Failed to verify signature: {}.\n\n", error)); if show_comments {
acc.push_str(&self.attachment_displays_to_text(display, context)); acc.push_str(&format!("Failed to verify signature: {}.\n\n", error));
}
acc.push_str(&self.attachment_displays_to_text(
display,
context,
show_comments,
));
} }
SignedVerified { SignedVerified {
inner: _, inner: _,
display, display,
description, description,
} => { } => {
if description.is_empty() { if show_comments {
acc.push_str("Verified signature.\n\n"); if description.is_empty() {
} else { acc.push_str("Verified signature.\n\n");
acc.push_str(&description); } else {
acc.push_str("\n\n"); acc.push_str(&description);
acc.push_str("\n\n");
}
} }
acc.push_str(&self.attachment_displays_to_text(display, context)); acc.push_str(&self.attachment_displays_to_text(
display,
context,
show_comments,
));
} }
EncryptedPending { .. } => acc.push_str("Waiting for decryption result."), EncryptedPending { .. } => acc.push_str("Waiting for decryption result."),
EncryptedFailed { inner: _, error } => { EncryptedFailed { inner: _, error } => {
@ -481,13 +518,19 @@ impl MailView {
plaintext_display, plaintext_display,
description, description,
} => { } => {
if description.is_empty() { if show_comments {
acc.push_str("Succesfully decrypted.\n\n"); if description.is_empty() {
} else { acc.push_str("Succesfully decrypted.\n\n");
acc.push_str(&description); } else {
acc.push_str("\n\n"); acc.push_str(&description);
acc.push_str("\n\n");
}
} }
acc.push_str(&self.attachment_displays_to_text(plaintext_display, context)); acc.push_str(&self.attachment_displays_to_text(
plaintext_display,
context,
show_comments,
));
} }
} }
} }
@ -507,7 +550,11 @@ impl MailView {
use AttachmentDisplay::*; use AttachmentDisplay::*;
cur_path.push(i); cur_path.push(i);
match d { match d {
InlineText { inner, text: _ } InlineText {
inner,
text: _,
comment: _,
}
| InlineOther { inner } | InlineOther { inner }
| Attachment { inner } | Attachment { inner }
| SignedPending { | SignedPending {
@ -589,14 +636,15 @@ impl MailView {
err.to_string(), err.to_string(),
Some(NotificationType::Error(melib::ErrorKind::External)), Some(NotificationType::Error(melib::ErrorKind::External)),
)); ));
let mut s = format!( let comment = Some(format!(
"Failed to start html filter process: `{}`. Press `v` to open in web browser. \n\n", "Failed to start html filter process: `{}`. Press `v` to open in web browser. \n\n",
filter_invocation filter_invocation
); ));
s.push_str(&String::from_utf8_lossy(&bytes)); let text = String::from_utf8_lossy(&bytes).to_string();
acc.push(AttachmentDisplay::InlineText { acc.push(AttachmentDisplay::InlineText {
inner: a.clone(), inner: a.clone(),
text: s, comment,
text,
}); });
} }
Ok(mut html_filter) => { Ok(mut html_filter) => {
@ -606,16 +654,18 @@ impl MailView {
.unwrap() .unwrap()
.write_all(&bytes) .write_all(&bytes)
.expect("Failed to write to stdin"); .expect("Failed to write to stdin");
let mut s = format!( let comment = Some(format!(
"Text piped through `{}`. Press `v` to open in web browser. \n\n", "Text piped through `{}`. Press `v` to open in web browser. \n\n",
filter_invocation filter_invocation
);
s.push_str(&String::from_utf8_lossy(
&html_filter.wait_with_output().unwrap().stdout,
)); ));
let text = String::from_utf8_lossy(
&html_filter.wait_with_output().unwrap().stdout,
)
.to_string();
acc.push(AttachmentDisplay::InlineText { acc.push(AttachmentDisplay::InlineText {
inner: a.clone(), inner: a.clone(),
text: s, comment,
text,
}); });
} }
} }
@ -623,6 +673,7 @@ impl MailView {
let bytes = decode(a, None); let bytes = decode(a, None);
acc.push(AttachmentDisplay::InlineText { acc.push(AttachmentDisplay::InlineText {
inner: a.clone(), inner: a.clone(),
comment: None,
text: String::from_utf8_lossy(&bytes).to_string(), text: String::from_utf8_lossy(&bytes).to_string(),
}); });
} else if let ContentType::Multipart { } else if let ContentType::Multipart {
@ -639,6 +690,7 @@ impl MailView {
let bytes = decode(&parts[text_attachment_pos], None); let bytes = decode(&parts[text_attachment_pos], None);
acc.push(AttachmentDisplay::InlineText { acc.push(AttachmentDisplay::InlineText {
inner: a.clone(), inner: a.clone(),
comment: None,
text: String::from_utf8_lossy(&bytes).to_string(), text: String::from_utf8_lossy(&bytes).to_string(),
}); });
} else { } else {
@ -779,7 +831,11 @@ impl MailView {
let first = path[0]; let first = path[0];
use AttachmentDisplay::*; use AttachmentDisplay::*;
let root_attachment = match &display[first] { let root_attachment = match &display[first] {
InlineText { inner, text: _ } InlineText {
inner,
text: _,
comment: _,
}
| InlineOther { inner } | InlineOther { inner }
| Attachment { inner } | Attachment { inner }
| SignedPending { | SignedPending {
@ -1428,7 +1484,7 @@ impl Component for MailView {
self.attachment_tree = attachment_tree_s; self.attachment_tree = attachment_tree_s;
self.attachment_paths = paths; self.attachment_paths = paths;
let body_text = let body_text =
self.attachment_displays_to_text(&display, context); self.attachment_displays_to_text(&display, context, true);
self.state = MailViewState::Loaded { self.state = MailViewState::Loaded {
bytes, bytes,
body, body,
@ -1526,7 +1582,7 @@ impl Component for MailView {
let mut new_body_text = String::new(); let mut new_body_text = String::new();
if let MailViewState::Loaded { ref display, .. } = self.state { if let MailViewState::Loaded { ref display, .. } = self.state {
new_body_text = new_body_text =
self.attachment_displays_to_text(&display, context); self.attachment_displays_to_text(&display, context, true);
let (paths, attachment_tree_s) = let (paths, attachment_tree_s) =
self.attachment_displays_to_tree(&display); self.attachment_displays_to_tree(&display);
self.attachment_tree = attachment_tree_s; self.attachment_tree = attachment_tree_s;