🐝 I really like where this mua is(was?) headed, but it seems as though there has not been much activity recently.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.4 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. /*
  2. * meli - ui crate.
  3. *
  4. * Copyright 2017-2018 Manos Pitsidianakis
  5. *
  6. * This file is part of meli.
  7. *
  8. * meli is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * meli is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with meli. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. use super::*;
  22. use std::io::Write;
  23. use std::process::{Command, Stdio};
  24. #[derive(Debug)]
  25. pub struct HtmlView {
  26. pager: Pager,
  27. bytes: Vec<u8>,
  28. }
  29. impl HtmlView {
  30. pub fn new(bytes: Vec<u8>) -> Self {
  31. let mut html_filter = Command::new("w3m")
  32. .args(&["-I", "utf-8", "-T", "text/html"])
  33. .stdin(Stdio::piped())
  34. .stdout(Stdio::piped())
  35. .spawn()
  36. .expect("Failed to start html filter process");
  37. html_filter
  38. .stdin
  39. .as_mut()
  40. .unwrap()
  41. .write_all(&bytes)
  42. .expect("Failed to write to w3m stdin");
  43. let mut display_text =
  44. String::from("Text piped through `w3m`. Press `v` to open in web browser. \n\n");
  45. display_text.push_str(&String::from_utf8_lossy(
  46. &html_filter.wait_with_output().unwrap().stdout,
  47. ));
  48. let buf = MailView::plain_text_to_buf(&display_text, true);
  49. let pager = Pager::from_buf(buf.split_newlines(), None);
  50. HtmlView { pager, bytes }
  51. }
  52. }
  53. impl fmt::Display for HtmlView {
  54. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  55. // TODO display subject/info
  56. write!(f, "view")
  57. }
  58. }
  59. impl Component for HtmlView {
  60. fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
  61. self.pager.draw(grid, area, context);
  62. }
  63. fn process_event(&mut self, event: &UIEvent, context: &mut Context) -> bool {
  64. if self.pager.process_event(event, context) {
  65. return true;
  66. }
  67. if let UIEventType::Input(Key::Char('v')) = event.event_type {
  68. // TODO: Optional filter that removes outgoing resource requests (images and
  69. // scripts)
  70. let binary = query_default_app("text/html");
  71. if let Ok(binary) = binary {
  72. let mut p = create_temp_file(&self.bytes, None);
  73. Command::new(&binary)
  74. .arg(p.path())
  75. .stdin(Stdio::piped())
  76. .stdout(Stdio::piped())
  77. .spawn()
  78. .unwrap_or_else(|_| panic!("Failed to start {}", binary.display()));
  79. context.temp_files.push(p);
  80. } else {
  81. context.replies.push_back(UIEvent {
  82. id: 0,
  83. event_type: UIEventType::StatusEvent(StatusEvent::DisplayMessage(
  84. "Couldn't find a default application for html files.".to_string(),
  85. )),
  86. });
  87. }
  88. return true;
  89. }
  90. false
  91. }
  92. fn is_dirty(&self) -> bool {
  93. self.pager.is_dirty()
  94. }
  95. fn set_dirty(&mut self) {
  96. self.pager.set_dirty();
  97. }
  98. }