ui: correctly display multipart html in Views

- Use Alt-r for entering raw mode
- Use r to exit raw mode
- added shortcuts
- add attachment footer in HtmlView::new
embed
Manos Pitsidianakis 2019-05-07 01:57:44 +03:00
parent 59d912e2ee
commit 35bac364b1
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
3 changed files with 61 additions and 45 deletions

View File

@ -396,18 +396,15 @@ impl Component for MailView {
self.pager = None;
let attachment = &body.attachments()[aidx];
self.subview = Some(Box::new(HtmlView::new(
decode(&attachment, None),
&attachment,
context,
self.coordinates.0,
)));
self.mode = ViewMode::Subview;
}
ViewMode::Normal if body.is_html() => {
self.subview = Some(Box::new(HtmlView::new(
decode(&body, None),
context,
self.coordinates.0,
)));
self.subview =
Some(Box::new(HtmlView::new(&body, context, self.coordinates.0)));
self.pager = None;
self.mode = ViewMode::Subview;
}
@ -547,25 +544,24 @@ impl Component for MailView {
self.cmd_buf.clone(),
)));
}
UIEvent::Input(Key::Char('r'))
if self.mode == ViewMode::Normal || self.mode == ViewMode::Raw =>
UIEvent::Input(Key::Alt('r'))
if self.mode == ViewMode::Normal || self.mode == ViewMode::Subview =>
{
self.mode = if self.mode == ViewMode::Raw {
ViewMode::Normal
} else {
ViewMode::Raw
};
self.dirty = true;
self.mode = ViewMode::Raw;
self.set_dirty();
}
UIEvent::Input(Key::Char('r'))
if self.mode.is_attachment() || self.mode == ViewMode::Subview =>
if self.mode.is_attachment()
|| self.mode == ViewMode::Subview
|| self.mode == ViewMode::Url
|| self.mode == ViewMode::Raw =>
{
self.mode = ViewMode::Normal;
self.subview.take();
self.dirty = true;
self.set_dirty();
}
UIEvent::Input(Key::Char('a'))
if !self.cmd_buf.is_empty() && self.mode == ViewMode::Normal =>
if !self.cmd_buf.is_empty()
&& (self.mode == ViewMode::Normal || self.mode == ViewMode::Subview) =>
{
let lidx = self.cmd_buf.parse::<usize>().unwrap();
self.cmd_buf.clear();
@ -734,6 +730,31 @@ impl Component for MailView {
_ => {}
}
}
fn get_shortcuts(&self, context: &Context) -> ShortcutMap {
let mut map = if let Some(ref sbv) = self.subview {
sbv.get_shortcuts(context)
} else if let Some(ref pgr) = self.pager {
pgr.get_shortcuts(context)
} else {
FnvHashMap::with_capacity_and_hasher(4, Default::default())
};
map.insert("add_addresses_to_contacts", Key::Char('c'));
map.insert("view_raw_source", Key::Alt('r'));
if self.mode.is_attachment() || self.mode == ViewMode::Subview || self.mode == ViewMode::Raw
{
map.insert("return_to_normal_view", Key::Char('r'));
}
map.insert("open_attachment", Key::Char('a'));
if self.mode == ViewMode::Url {
map.insert("go_to_url", Key::Char('g'));
}
if self.mode == ViewMode::Normal || self.mode == ViewMode::Url {
map.insert("toggle_url_mode", Key::Char('u'));
}
map
}
fn id(&self) -> ComponentId {
self.id

View File

@ -315,17 +315,13 @@ impl Component for EnvelopeView {
ViewMode::Attachment(aidx) if body.attachments()[aidx].is_html() => {
let attachment = &body.attachments()[aidx];
self.subview = Some(Box::new(HtmlView::new(
decode(&attachment, None),
&attachment,
context,
self.account_pos,
)));
}
ViewMode::Normal if body.is_html() => {
self.subview = Some(Box::new(HtmlView::new(
decode(&body, None),
context,
self.account_pos,
)));
self.subview = Some(Box::new(HtmlView::new(&body, context, self.account_pos)));
self.mode = ViewMode::Subview;
}
_ => {

View File

@ -31,10 +31,12 @@ pub struct HtmlView {
}
impl HtmlView {
pub fn new(bytes: Vec<u8>, context: &mut Context, account_pos: usize) -> Self {
pub fn new(body: &Attachment, context: &mut Context, account_pos: usize) -> Self {
let id = ComponentId::new_v4();
let bytes: Vec<u8> = decode_rec(body, None);
let settings = context.accounts[account_pos].runtime_settings.conf();
if let Some(filter_invocation) = settings.html_filter() {
let mut display_text = if let Some(filter_invocation) = settings.html_filter() {
let parts = split_command!(filter_invocation);
let (cmd, args) = (parts[0], &parts[1..]);
let command_obj = Command::new(cmd)
@ -50,13 +52,7 @@ impl HtmlView {
)),
String::new(),
));
let pager = Pager::from_string(
String::from_utf8_lossy(&bytes).to_string(),
None,
None,
None,
);
HtmlView { pager, bytes, id }
String::from_utf8_lossy(&bytes).to_string()
} else {
let mut html_filter = command_obj.unwrap();
html_filter
@ -72,9 +68,7 @@ impl HtmlView {
display_text.push_str(&String::from_utf8_lossy(
&html_filter.wait_with_output().unwrap().stdout,
));
let pager = Pager::from_string(display_text, None, None, None);
HtmlView { pager, bytes, id }
display_text
}
} else {
if let Ok(mut html_filter) = Command::new("w3m")
@ -96,8 +90,7 @@ impl HtmlView {
&html_filter.wait_with_output().unwrap().stdout,
));
let pager = Pager::from_string(display_text, None, None, None);
HtmlView { pager, bytes, id }
display_text
} else {
context.replies.push_back(UIEvent::Notification(
Some(format!(
@ -105,15 +98,21 @@ impl HtmlView {
)),
String::new(),
));
let pager = Pager::from_string(
String::from_utf8_lossy(&bytes).to_string(),
None,
None,
None,
);
HtmlView { pager, bytes, id }
String::from_utf8_lossy(&bytes).to_string()
}
};
if body.count_attachments() > 1 {
display_text =
body.attachments()
.iter()
.enumerate()
.fold(display_text, |mut s, (idx, a)| {
s.push_str(&format!("[{}] {}\n\n\n", idx, a));
s
});
}
let pager = Pager::from_string(display_text, None, None, None);
HtmlView { pager, bytes, id }
}
}