summaryrefslogtreecommitdiffstats
path: root/src/sidebar.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/sidebar.rs')
-rw-r--r--src/sidebar.rs34
1 files changed, 26 insertions, 8 deletions
diff --git a/src/sidebar.rs b/src/sidebar.rs
index ecc2e29..922e446 100644
--- a/src/sidebar.rs
+++ b/src/sidebar.rs
@@ -2,11 +2,17 @@ use std::path::PathBuf;
use anyhow::Result;
use std::fmt;
use std::ffi::OsStr;
+use cursive::view::Nameable;
+use cursive::views::NamedView;
+use cursive::Cursive;
+
+use crate::main_view::MainView;
+use cursive_tree_view::TreeView;
#[derive(Debug)]
pub struct TreeEntry {
name: String,
- dir: Option<PathBuf>,
+ path: PathBuf,
}
impl fmt::Display for TreeEntry {
@@ -15,23 +21,34 @@ impl fmt::Display for TreeEntry {
}
}
+pub const SIDEBAR_VIEW_NAME: &'static str = "sidebar_tree_view";
-pub fn sidebar<I>(iter: I) -> Result<cursive_tree_view::TreeView<TreeEntry>>
+pub fn sidebar<I>(iter: I) -> Result<NamedView<cursive_tree_view::TreeView<TreeEntry>>>
where I: Iterator<Item = PathBuf>
{
let mut tv = cursive_tree_view::TreeView::default();
+ tv.set_on_submit(|siv: &mut Cursive, row: usize| {
+ let borrow = siv.call_on_name(SIDEBAR_VIEW_NAME, move |tree: &mut TreeView<TreeEntry>| {
+ tree.borrow_item(row).map(|b| b.path.clone())
+ })
+ .flatten();
+
+ if let Some(path) = borrow {
+ siv.call_on_name(crate::main_view::MAIN_VIEW_NAME, move |main_view: &mut MainView| {
+ main_view.load_maildir(path);
+ None as Option<()>
+ });
+ }
+ });
+
+ // Fill the tree from the iterator
iter.map(|path| {
path.file_name()
.and_then(OsStr::to_str)
.map(String::from)
.ok_or_else(|| anyhow!("UTF8 error"))
- .map(|name| {
- TreeEntry {
- name,
- dir: Some(path)
- }
- })
+ .map(|name| TreeEntry { name, path })
})
.enumerate()
.fold(Ok(tv), |accu, (i, item)| {
@@ -40,4 +57,5 @@ pub fn sidebar<I>(iter: I) -> Result<cursive_tree_view::TreeView<TreeEntry>>
.map(|_| tv)
})
})
+ .map(|tv| tv.with_name(SIDEBAR_VIEW_NAME))
}