diff options
Diffstat (limited to 'src/main_view.rs')
-rw-r--r-- | src/main_view.rs | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/src/main_view.rs b/src/main_view.rs new file mode 100644 index 0000000..9c92a7e --- /dev/null +++ b/src/main_view.rs @@ -0,0 +1,98 @@ +use std::path::PathBuf; +use anyhow::Result; +use cursive::{View, Printer, XY, direction::Direction, view::Selector, Rect, event::Event, event::EventResult}; +use cursive::view::Nameable; +use cursive::Cursive; +use cursive::views::NamedView; +use mailparse::MailHeaderMap; +use mailparse::ParsedMail; + +use crate::mailstore::{ MailStore, Mail }; + +pub const MAIN_VIEW_NAME: &'static str = "main_view"; + +pub struct MainView { + tabs: cursive_tabs::TabView<usize>, +} + +impl View for MainView { + fn draw(&self, printer: &Printer) { + self.tabs.draw(printer) + } + + fn layout(&mut self, xy: XY<usize>) { + self.tabs.layout(xy) + } + + fn needs_relayout(&self) -> bool { + self.tabs.needs_relayout() + } + + fn required_size(&mut self, constraint: XY<usize>) -> XY<usize> { + self.tabs.required_size(constraint) + } + + fn on_event(&mut self, e: Event) -> EventResult { + self.tabs.on_event(e) + } + + fn call_on_any<'a>(&mut self, s: &Selector, tpl: &'a mut (dyn FnMut(&mut (dyn View + 'static)) + 'a)) { + self.tabs.call_on_any(s, tpl); + } + + fn focus_view(&mut self, s: &Selector) -> Result<(), ()> { + self.tabs.focus_view(s) + } + + fn take_focus(&mut self, source: Direction) -> bool { + self.tabs.take_focus(source) + } + + fn important_area(&self, view_size: XY<usize>) -> Rect { + self.tabs.important_area(view_size) + } + + fn type_name(&self) -> &'static str { + self.tabs.type_name() + } + +} + +impl MainView { + pub fn new() -> NamedView<Self> { + let mut tabs = cursive_tabs::TabView::default(); + tabs.add_tab(0, cursive::views::TextView::new("Test")); + MainView { tabs }.with_name(MAIN_VIEW_NAME) + } + + pub fn load_maildir(&mut self, pb: PathBuf) -> Result<()> { + let mut md = MailStore::from_path(pb); + md.load()?; + let list_view = MainView::list_view_for(md.cur_mail().iter().map(Mail::parsed))?; + + self.tabs.add_tab(1, list_view.with_name("Tab")); + Ok(()) + } + + fn list_view_for<'a, I>(i: I) -> Result<cursive::views::ListView> + where I: Iterator<Item = &'a ParsedMail> + { + let mut lv = cursive::views::ListView::new(); + + for elem in i { + let s = format!("{}: {} -> {} | {}", + elem.headers.get_first_value("Date").unwrap_or_else(|| String::from("No date")), + elem.headers.get_first_value("From").unwrap_or_else(|| String::from("No From")), + elem.headers.get_first_value("To").unwrap_or_else(|| String::from("No To")), + elem.headers.get_first_value("Subject").unwrap_or_else(|| String::from("No Subject"))); + + lv.add_child("", cursive::views::TextView::new(s)); + } + + Ok(lv) + } + +} + + + |