diff options
author | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2018-08-03 13:46:08 +0300 |
---|---|---|
committer | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2019-06-10 19:40:26 +0300 |
commit | b21d30c2ef9a1be537703a342dae5aad054ba769 (patch) | |
tree | 0cbfbfa55daa954a63393cea070a485b37725fad /ui | |
parent | 4e5721563e8dbc276ee2f7119c146c2fdeacdb14 (diff) |
Async loading of mailboxes on startup
Diffstat (limited to 'ui')
-rw-r--r-- | ui/src/components/mail/listing.rs | 53 | ||||
-rw-r--r-- | ui/src/components/mail/view.rs | 8 | ||||
-rw-r--r-- | ui/src/components/mod.rs | 10 | ||||
-rw-r--r-- | ui/src/components/utilities.rs | 52 | ||||
-rw-r--r-- | ui/src/lib.rs | 15 |
5 files changed, 98 insertions, 40 deletions
diff --git a/ui/src/components/mail/listing.rs b/ui/src/components/mail/listing.rs index f9b81c2d..dca64a7a 100644 --- a/ui/src/components/mail/listing.rs +++ b/ui/src/components/mail/listing.rs @@ -1,9 +1,10 @@ use super::*; + const MAX_COLS: usize = 500; /// A list of all mail (`Envelope`s) in a `Mailbox`. On `\n` it opens the `Envelope` content in a -/// `Pager`. +/// `MailView`. pub struct MailListing { /// (x, y, z): x is accounts, y is folders, z is index inside a folder. cursor_pos: (usize, usize, usize), @@ -24,7 +25,7 @@ impl MailListing { /* TODO: Make this configurable */ fn make_entry_string(e: &Envelope, idx: usize) -> String { format!( - "{} {} {:.85}", + "{} {} {}", idx, &e.datetime().format("%Y-%m-%d %H:%M:%S").to_string(), e.subject() @@ -55,17 +56,25 @@ impl MailListing { let threaded = context.accounts[self.cursor_pos.0] .runtime_settings .threaded; - // Get mailbox as a reference. - let mailbox = &mut context.accounts[self.cursor_pos.0][self.cursor_pos.1] - .as_ref() - .unwrap() - .as_ref() - .unwrap(); // Inform State that we changed the current folder view. context.replies.push_back(UIEvent { id: 0, event_type: UIEventType::RefreshMailbox((self.cursor_pos.0, self.cursor_pos.1)), }); + // Get mailbox as a reference. + // + loop { + eprintln!("loop round"); + match context.accounts[self.cursor_pos.0].status(self.cursor_pos.1) { + Ok(()) => { break; }, + Err(a) => { + eprintln!("status returned {:?}", a); + } + } + } + let mailbox = &mut context.accounts[self.cursor_pos.0][self.cursor_pos.1] + .as_ref() + .unwrap(); self.length = if threaded { mailbox.threaded_collection.len() @@ -81,7 +90,7 @@ impl MailListing { Color::Default, ((0, 0), (MAX_COLS - 1, 0)), true, - ); + ); self.content = content; return; } @@ -90,7 +99,7 @@ impl MailListing { if threaded { let mut indentations: Vec<bool> = Vec::with_capacity(6); let mut thread_idx = 0; // needed for alternate thread colors - /* Draw threaded view. */ + /* Draw threaded view. */ let mut iter = mailbox.threaded_collection.iter().enumerate().peekable(); let len = mailbox .threaded_collection @@ -143,13 +152,13 @@ impl MailListing { container, &indentations, len, - ), - &mut content, - fg_color, - bg_color, - ((0, idx), (MAX_COLS - 1, idx)), - false, - ); + ), + &mut content, + fg_color, + bg_color, + ((0, idx), (MAX_COLS - 1, idx)), + false, + ); for x in x..MAX_COLS { content[(x, idx)].set_ch(' '); content[(x, idx)].set_bg(bg_color); @@ -199,7 +208,7 @@ impl MailListing { bg_color, ((0, y), (MAX_COLS - 1, y)), false, - ); + ); for x in x..MAX_COLS { content[(x, y)].set_ch(' '); @@ -218,8 +227,6 @@ impl MailListing { .threaded; let mailbox = &context.accounts[self.cursor_pos.0][self.cursor_pos.1] .as_ref() - .unwrap() - .as_ref() .unwrap(); let envelope: &Envelope = if threaded { let i = mailbox.threaded_mail(idx); @@ -563,6 +570,12 @@ impl Component for MailListing { self.dirty = true; self.view = None; } + UIEventType::MailboxUpdate((ref idxa, ref idxf)) => { + if *idxa == self.new_cursor_pos.1 && *idxf == self.new_cursor_pos.0 { + self.refresh_mailbox(context); + self.dirty = true; + } + } UIEventType::ChangeMode(UIMode::Normal) => { self.dirty = true; } diff --git a/ui/src/components/mail/view.rs b/ui/src/components/mail/view.rs index 74546100..8c1a47cf 100644 --- a/ui/src/components/mail/view.rs +++ b/ui/src/components/mail/view.rs @@ -62,8 +62,6 @@ impl Component for MailView { .threaded; let mailbox = &mut context.accounts[self.coordinates.0][self.coordinates.1] .as_ref() - .unwrap() - .as_ref() .unwrap(); let envelope_idx: usize = if threaded { mailbox.threaded_mail(self.coordinates.2) @@ -158,8 +156,6 @@ impl Component for MailView { let mailbox_idx = self.coordinates; // coordinates are mailbox idxs let mailbox = &mut context.accounts[mailbox_idx.0][mailbox_idx.1] .as_ref() - .unwrap() - .as_ref() .unwrap(); let envelope: &Envelope = &mailbox.collection[envelope_idx]; @@ -269,8 +265,6 @@ impl Component for MailView { .threaded; let mailbox = &mut context.accounts[self.coordinates.0][self.coordinates.1] .as_ref() - .unwrap() - .as_ref() .unwrap(); let envelope_idx: usize = if threaded { mailbox.threaded_mail(self.coordinates.2) @@ -340,8 +334,6 @@ impl Component for MailView { .threaded; let mailbox = &mut context.accounts[self.coordinates.0][self.coordinates.1] .as_ref() - .unwrap() - .as_ref() .unwrap(); let envelope_idx: usize = if threaded { mailbox.threaded_mail(self.coordinates.2) diff --git a/ui/src/components/mod.rs b/ui/src/components/mod.rs index 770f04e7..dc56caaf 100644 --- a/ui/src/components/mod.rs +++ b/ui/src/components/mod.rs @@ -31,7 +31,7 @@ pub mod notifications; pub mod utilities; pub use mail::*; -pub use utilities::*; +pub use self::utilities::*; use super::cells::{CellBuffer, Color}; use super::position::Area; @@ -138,6 +138,14 @@ pub fn copy_area(grid_dest: &mut CellBuffer, grid_src: &CellBuffer, dest: Area, } let mut src_x = get_x(upper_left!(src)); let mut src_y = get_y(upper_left!(src)); + let (cols, rows) = grid_src.size(); + if src_x >= cols || src_y >= rows { + eprintln!( + "DEBUG: src area outside of grid_src in copy_area", + ); + return; + + } for y in get_y(upper_left!(dest))..=get_y(bottom_right!(dest)) { 'for_x: for x in get_x(upper_left!(dest))..=get_x(bottom_right!(dest)) { diff --git a/ui/src/components/utilities.rs b/ui/src/components/utilities.rs index f605ea77..f28ddf05 100644 --- a/ui/src/components/utilities.rs +++ b/ui/src/components/utilities.rs @@ -413,10 +413,14 @@ impl Component for StatusBar { self.container.rcv_event(event, context); match &event.event_type { UIEventType::RefreshMailbox((ref idx_a, ref idx_f)) => { + match context.accounts[*idx_a].status(*idx_f) { + Ok(()) => {}, + Err(_) => { + return; + } + } let m = &context.accounts[*idx_a][*idx_f] .as_ref() - .unwrap() - .as_ref() .unwrap(); self.status = format!( "{} | Mailbox: {}, Messages: {}, New: {}", @@ -483,3 +487,47 @@ impl Component for TextBox { return; } } + +pub struct Progress { + description: String, + total_work: usize, + finished: usize, +} + +impl Progress { + pub fn new(s: String, total_work: usize) -> Self { + Progress { + description: s, + total_work: total_work, + finished: 0, + } + } + + pub fn add_work(&mut self, n: usize) -> () { + if self.finished >= self.total_work { + return; + } + self.finished += n; + } + + pub fn percentage(&self) -> usize { + if self.total_work > 0 { + 100 * self.finished / self.total_work + } else { + 0 + } + } + + pub fn description(&self) -> &str { + &self.description + } +} + +impl Component for Progress { + fn draw(&mut self, _grid: &mut CellBuffer, _area: Area, _context: &mut Context) { + unimplemented!() + } + fn process_event(&mut self, _event: &UIEvent, _context: &mut Context) { + return; + } +} diff --git a/ui/src/lib.rs b/ui/src/lib.rs index 88016b4d..17f66724 100644 --- a/ui/src/lib.rs +++ b/ui/src/lib.rs @@ -75,7 +75,7 @@ pub enum ThreadEvent { Input(Key), /// A watched folder has been refreshed. RefreshMailbox { - name: String, + hash: u64, }, UIEventType(UIEventType), //Decode { _ }, // For gpg2 signature check @@ -83,7 +83,7 @@ pub enum ThreadEvent { impl From<RefreshEvent> for ThreadEvent { fn from(event: RefreshEvent) -> Self { - ThreadEvent::RefreshMailbox { name: event.folder } + ThreadEvent::RefreshMailbox { hash: event.hash } } } @@ -109,6 +109,7 @@ pub enum UIEventType { EditDraft(File), Action(Action), StatusNotification(String), + MailboxUpdate((usize, usize)), } /// An event passed from `State` to its Entities. @@ -248,7 +249,7 @@ impl State<std::io::Stdout> { cursor::Hide, clear::All, cursor::Goto(1, 1) - ).unwrap(); + ).unwrap(); s.flush(); for account in &mut s.context.accounts { let sender = s.sender.clone(); @@ -455,15 +456,11 @@ impl<W: Write> State<W> { /// Tries to load a mailbox's content pub fn refresh_mailbox(&mut self, account_idx: usize, folder_idx: usize) { let flag = match &mut self.context.accounts[account_idx][folder_idx] { - Some(Ok(_)) => true, - Some(Err(e)) => { + Ok(_) => true, + Err(e) => { eprintln!("error {:?}", e); false } - None => { - eprintln!("None"); - false - } }; if flag { self.rcv_event(UIEvent { |