2018-08-09 16:50:33 +03:00
|
|
|
/*
|
|
|
|
* meli - ui crate.
|
|
|
|
*
|
|
|
|
* Copyright 2017-2018 Manos Pitsidianakis
|
|
|
|
*
|
|
|
|
* This file is part of meli.
|
|
|
|
*
|
|
|
|
* meli is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* meli is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with meli. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::process::{Command, Stdio};
|
|
|
|
|
|
|
|
pub struct HtmlView {
|
|
|
|
pager: Pager,
|
2018-08-10 11:06:47 +03:00
|
|
|
bytes: Vec<u8>,
|
2018-08-09 16:50:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl HtmlView {
|
|
|
|
pub fn new(bytes: Vec<u8>) -> Self {
|
|
|
|
let mut html_filter = Command::new("w3m")
|
2018-08-10 11:06:47 +03:00
|
|
|
.args(&["-I", "utf-8", "-T", "text/html"])
|
2018-08-09 16:50:33 +03:00
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.spawn()
|
|
|
|
.expect("Failed to start html filter process");
|
2018-08-10 11:06:47 +03:00
|
|
|
html_filter
|
|
|
|
.stdin
|
|
|
|
.as_mut()
|
|
|
|
.unwrap()
|
|
|
|
.write_all(&bytes)
|
|
|
|
.expect("Failed to write to w3m stdin");
|
|
|
|
let mut display_text =
|
|
|
|
String::from("Text piped through `w3m`. Press `v` to open in web browser. \n\n");
|
|
|
|
display_text.push_str(&String::from_utf8_lossy(
|
|
|
|
&html_filter.wait_with_output().unwrap().stdout,
|
|
|
|
));
|
2018-08-09 16:50:33 +03:00
|
|
|
|
|
|
|
let buf = MailView::plain_text_to_buf(&display_text, true);
|
2018-08-21 19:51:48 +03:00
|
|
|
let pager = Pager::from_buf(buf.split_newlines(), None);
|
2018-08-10 11:06:47 +03:00
|
|
|
HtmlView { pager, bytes }
|
2018-08-09 16:50:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-11 18:00:21 +03:00
|
|
|
impl fmt::Display for HtmlView {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
// TODO display subject/info
|
|
|
|
write!(f, "view")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 16:50:33 +03:00
|
|
|
impl Component for HtmlView {
|
|
|
|
fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
|
|
|
|
self.pager.draw(grid, area, context);
|
|
|
|
}
|
|
|
|
fn process_event(&mut self, event: &UIEvent, context: &mut Context) {
|
|
|
|
match event.event_type {
|
|
|
|
UIEventType::Input(Key::Char('v')) => {
|
|
|
|
// TODO: Optional filter that removes outgoing resource requests (images and
|
|
|
|
// scripts)
|
|
|
|
let binary = query_default_app("text/html");
|
|
|
|
if let Ok(binary) = binary {
|
|
|
|
let mut p = create_temp_file(&self.bytes, None);
|
|
|
|
Command::new(&binary)
|
|
|
|
.arg(p.path())
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.spawn()
|
2018-08-10 11:06:47 +03:00
|
|
|
.unwrap_or_else(|_| panic!("Failed to start {}", binary.display()));
|
2018-08-09 16:50:33 +03:00
|
|
|
context.temp_files.push(p);
|
|
|
|
} else {
|
|
|
|
context.replies.push_back(UIEvent {
|
|
|
|
id: 0,
|
|
|
|
event_type: UIEventType::StatusNotification(format!(
|
2018-08-10 11:06:47 +03:00
|
|
|
"Couldn't find a default application for html files."
|
|
|
|
)),
|
2018-08-09 16:50:33 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return;
|
2018-08-10 11:06:47 +03:00
|
|
|
}
|
|
|
|
_ => {}
|
2018-08-09 16:50:33 +03:00
|
|
|
}
|
|
|
|
self.pager.process_event(event, context);
|
|
|
|
}
|
|
|
|
fn is_dirty(&self) -> bool {
|
|
|
|
self.pager.is_dirty()
|
|
|
|
}
|
2018-08-11 18:00:21 +03:00
|
|
|
fn set_dirty(&mut self) {}
|
2018-08-09 16:50:33 +03:00
|
|
|
}
|