From 2b6d1e0dbf47750ee4377d532fb6b38496e4b336 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Thu, 23 Aug 2018 15:36:52 +0300 Subject: Run clippy lints --- ui/src/components/mail/compose.rs | 22 +++--- ui/src/components/mail/listing/compact.rs | 42 +++++------ ui/src/components/mail/listing/mod.rs | 29 ++++--- ui/src/components/mail/mod.rs | 4 +- ui/src/components/mail/view/envelope.rs | 83 +++++++++++--------- ui/src/components/mail/view/html.rs | 45 ++++++----- ui/src/components/mail/view/mod.rs | 91 ++++++++++++---------- ui/src/components/mail/view/thread.rs | 121 +++++++++++++++++++----------- 8 files changed, 243 insertions(+), 194 deletions(-) (limited to 'ui/src/components/mail') diff --git a/ui/src/components/mail/compose.rs b/ui/src/components/mail/compose.rs index edb30be7..3996cbe1 100644 --- a/ui/src/components/mail/compose.rs +++ b/ui/src/components/mail/compose.rs @@ -65,7 +65,7 @@ impl Component for Composer { let width = width!(area); let mid = if width > 80 { let width = width - 80; - let mid = width / 2;; + let mid = width / 2; if self.dirty { for i in get_y(upper_left)..=get_y(bottom_right) { @@ -78,17 +78,22 @@ impl Component for Composer { } } mid - } else { 0 }; + } else { + 0 + }; if self.dirty { - for i in get_x(upper_left)+ mid + 1..=get_x(upper_left) + mid + 79 { + for i in get_x(upper_left) + mid + 1..=get_x(upper_left) + mid + 79 { grid[(i, header_height)].set_ch(HORZ_BOUNDARY); grid[(i, header_height)].set_fg(Color::Default); grid[(i, header_height)].set_bg(Color::Default); } } - let body_area = ((mid + 1, header_height+2), (mid + 78, get_y(bottom_right))); + let body_area = ( + (mid + 1, header_height + 2), + (mid + 78, get_y(bottom_right)), + ); if self.dirty { context.dirty_areas.push_back(area); @@ -97,8 +102,7 @@ impl Component for Composer { match self.mode { ViewMode::Overview => { self.pager.draw(grid, body_area, context); - - }, + } } } @@ -134,12 +138,12 @@ impl Component for Composer { .expect("failed to execute process"); let result = f.read_to_string(); self.buffer = result.clone(); - self.pager.update_from_string(result); + self.pager.update_from_str(result.as_str()); context.restore_input(); self.dirty = true; return true; - }, - _ => {}, + } + _ => {} } false } diff --git a/ui/src/components/mail/listing/compact.rs b/ui/src/components/mail/listing/compact.rs index 96ab3d94..c9e3edd1 100644 --- a/ui/src/components/mail/listing/compact.rs +++ b/ui/src/components/mail/listing/compact.rs @@ -67,14 +67,14 @@ impl CompactListing { &CompactListing::format_date(e), e.subject(), len - ) + ) } else { format!( "{} {} {:.85}", idx, &CompactListing::format_date(e), e.subject(), - ) + ) } } @@ -86,7 +86,7 @@ impl CompactListing { length: 0, sort: (Default::default(), Default::default()), subsort: (Default::default(), Default::default()), - content: content, + content, dirty: true, unfocused: false, view: None, @@ -110,7 +110,10 @@ impl CompactListing { // loop { // TODO: Show progress visually - if let Ok(_) = context.accounts[self.cursor_pos.0].status(self.cursor_pos.1) { + if context.accounts[self.cursor_pos.0] + .status(self.cursor_pos.1) + .is_ok() + { break; } } @@ -118,7 +121,6 @@ impl CompactListing { .as_ref() .unwrap(); - self.length = mailbox.threads.root_len(); self.content = CellBuffer::new(MAX_COLS, self.length + 1, Cell::with_char(' ')); if self.length == 0 { @@ -139,9 +141,9 @@ impl CompactListing { let i = if let Some(i) = container.message() { i } else { - threads.containers()[ - container.first_child().unwrap() - ].message().unwrap() + threads.containers()[container.first_child().unwrap()] + .message() + .unwrap() }; let root_envelope: &Envelope = &mailbox.collection[i]; let fg_color = if has_unseen { @@ -163,16 +165,13 @@ impl CompactListing { bg_color, ((0, idx), (MAX_COLS - 1, idx)), false, - ); + ); for x in x..MAX_COLS { self.content[(x, idx)].set_ch(' '); self.content[(x, idx)].set_bg(bg_color); } - - } - } fn highlight_line(&self, grid: &mut CellBuffer, area: Area, idx: usize, context: &Context) { @@ -185,9 +184,9 @@ impl CompactListing { let i = if let Some(i) = container.message() { i } else { - threads.containers()[ - container.first_child().unwrap() - ].message().unwrap() + threads.containers()[container.first_child().unwrap()] + .message() + .unwrap() }; let root_envelope: &Envelope = &mailbox.collection[i]; let fg_color = if !root_envelope.is_seen() { @@ -303,11 +302,7 @@ impl Component for CompactListing { return; } self.view = Some(ThreadView::new(self.cursor_pos, context)); - self.view.as_mut().unwrap().draw( - grid, - area, - context, - ); + self.view.as_mut().unwrap().draw(grid, area, context); self.dirty = false; } } @@ -440,8 +435,7 @@ impl Component for CompactListing { self.dirty = true; self.refresh_mailbox(context); return true; - } - // _ => {} + } // _ => {} }, _ => {} } @@ -451,7 +445,9 @@ impl Component for CompactListing { self.dirty || self.view.as_ref().map(|p| p.is_dirty()).unwrap_or(false) } fn set_dirty(&mut self) { - self.view.as_mut().map(|p| p.set_dirty()); + if let Some(p) = self.view.as_mut() { + p.set_dirty(); + } self.dirty = true; } } diff --git a/ui/src/components/mail/listing/mod.rs b/ui/src/components/mail/listing/mod.rs index 132f4408..b109c599 100644 --- a/ui/src/components/mail/listing/mod.rs +++ b/ui/src/components/mail/listing/mod.rs @@ -79,7 +79,7 @@ impl MailListing { local_collection: Vec::new(), sort: (Default::default(), Default::default()), subsort: (Default::default(), Default::default()), - content: content, + content, dirty: true, unfocused: false, view: None, @@ -107,7 +107,10 @@ impl MailListing { // loop { // TODO: Show progress visually - if let Ok(_) = context.accounts[self.cursor_pos.0].status(self.cursor_pos.1) { + if context.accounts[self.cursor_pos.0] + .status(self.cursor_pos.1) + .is_ok() + { break; } } @@ -162,7 +165,6 @@ impl MailListing { continue; } - match iter.peek() { Some(&x) if threads[x].indentation() == indentation => { indentations.pop(); @@ -198,7 +200,7 @@ impl MailListing { container, &indentations, len, - // context.accounts[self.cursor_pos.0].backend.operation(envelope.hash()) + // context.accounts[self.cursor_pos.0].backend.operation(envelope.hash()) ), &mut self.content, fg_color, @@ -522,9 +524,7 @@ impl Component for MailListing { .threaded(); let account = &mut context.accounts[self.cursor_pos.0]; let (hash, is_seen) = { - let mailbox = &mut account[self.cursor_pos.1] - .as_mut() - .unwrap(); + let mailbox = &mut account[self.cursor_pos.1].as_mut().unwrap(); let envelope: &mut Envelope = if threaded { let i = mailbox.threaded_mail(idx); &mut mailbox.collection[i] @@ -538,9 +538,7 @@ impl Component for MailListing { let backend = &account.backend; backend.operation(hash) }; - let mailbox = &mut account[self.cursor_pos.1] - .as_mut() - .unwrap(); + let mailbox = &mut account[self.cursor_pos.1].as_mut().unwrap(); let envelope: &mut Envelope = if threaded { let i = mailbox.threaded_mail(idx); &mut mailbox.collection[i] @@ -598,9 +596,7 @@ impl Component for MailListing { .conf() .threaded(); let account = &context.accounts[self.cursor_pos.0]; - let mailbox = &account[self.cursor_pos.1] - .as_ref() - .unwrap(); + let mailbox = &account[self.cursor_pos.1].as_ref().unwrap(); let mut coordinates = self.cursor_pos; coordinates.2 = if threaded { mailbox.threaded_mail(self.cursor_pos.2) @@ -792,8 +788,7 @@ impl Component for MailListing { self.dirty = true; self.refresh_mailbox(context); return true; - } - // _ => {} + } // _ => {} }, _ => {} } @@ -803,7 +798,9 @@ impl Component for MailListing { self.dirty || self.view.as_ref().map(|p| p.is_dirty()).unwrap_or(false) } fn set_dirty(&mut self) { - self.view.as_mut().map(|p| p.set_dirty()); + if let Some(p) = self.view.as_mut() { + p.set_dirty(); + }; self.dirty = true; } } diff --git a/ui/src/components/mail/mod.rs b/ui/src/components/mail/mod.rs index 24d1890d..5def2bbd 100644 --- a/ui/src/components/mail/mod.rs +++ b/ui/src/components/mail/mod.rs @@ -65,10 +65,8 @@ impl AccountMenu { index: i, entries: { let mut entries = Vec::with_capacity(a.len()); - let mut idx = 0; - for acc in a.list_folders() { + for (idx, acc) in a.list_folders().into_iter().enumerate() { entries.push((idx, acc)); - idx += 1; } entries }, diff --git a/ui/src/components/mail/view/envelope.rs b/ui/src/components/mail/view/envelope.rs index 8f043612..2a809d08 100644 --- a/ui/src/components/mail/view/envelope.rs +++ b/ui/src/components/mail/view/envelope.rs @@ -68,7 +68,7 @@ impl EnvelopeView { wrapper: EnvelopeWrapper, pager: Option, subview: Option>, - ) -> Self { + ) -> Self { EnvelopeView { pager, subview, @@ -81,33 +81,34 @@ impl EnvelopeView { } /// Returns the string to be displayed in the Viewer - fn attachment_to_text(&self, body: Attachment) -> String { + fn attachment_to_text(&self, body: &Attachment) -> String { let finder = LinkFinder::new(); let body_text = String::from_utf8_lossy(&decode_rec( - &body, - Some(Box::new(|a: &Attachment, v: &mut Vec| { - if a.content_type().is_text_html() { - use std::io::Write; - use std::process::{Command, Stdio}; + &body, + Some(Box::new(|a: &Attachment, v: &mut Vec| { + if a.content_type().is_text_html() { + use std::io::Write; + use std::process::{Command, Stdio}; - let mut html_filter = Command::new("w3m") - .args(&["-I", "utf-8", "-T", "text/html"]) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn() - .expect("Failed to start html filter process"); + let mut html_filter = Command::new("w3m") + .args(&["-I", "utf-8", "-T", "text/html"]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + .expect("Failed to start html filter process"); - html_filter - .stdin - .as_mut() - .unwrap() - .write_all(&v) - .expect("Failed to write to w3m stdin"); - *v = b"Text piped through `w3m`. Press `v` to open in web browser. \n\n".to_vec(); - v.extend(html_filter.wait_with_output().unwrap().stdout); - } - })), - )).into_owned(); + html_filter + .stdin + .as_mut() + .unwrap() + .write_all(&v) + .expect("Failed to write to w3m stdin"); + *v = b"Text piped through `w3m`. Press `v` to open in web browser. \n\n" + .to_vec(); + v.extend(html_filter.wait_with_output().unwrap().stdout); + } + })), + )).into_owned(); match self.mode { ViewMode::Normal | ViewMode::Subview => { let mut t = body_text.to_string(); @@ -158,7 +159,7 @@ impl EnvelopeView { } } } - pub fn plain_text_to_buf(s: &String, highlight_urls: bool) -> CellBuffer { + pub fn plain_text_to_buf(s: &str, highlight_urls: bool) -> CellBuffer { let mut buf = CellBuffer::from(s); if highlight_urls { @@ -197,7 +198,7 @@ impl Component for EnvelopeView { let upper_left = upper_left!(area); let bottom_right = bottom_right!(area); - let y :usize = { + let y: usize = { let envelope: &Envelope = &self.wrapper; if self.mode == ViewMode::Raw { @@ -219,7 +220,7 @@ impl Component for EnvelopeView { grid[(x, y)].set_fg(Color::Default); } let (x, y) = write_string_to_grid( - &format!("From: {}", envelope.from_to_string()), + &format!("From: {}", envelope.field_from_to_string()), grid, Color::Byte(33), Color::Default, @@ -232,7 +233,7 @@ impl Component for EnvelopeView { grid[(x, y)].set_fg(Color::Default); } let (x, y) = write_string_to_grid( - &format!("To: {}", envelope.to_to_string()), + &format!("To: {}", envelope.field_to_to_string()), grid, Color::Byte(33), Color::Default, @@ -293,7 +294,7 @@ impl Component for EnvelopeView { } _ => { let buf = { - let text = self.attachment_to_text(body); + let text = self.attachment_to_text(&body); // URL indexes must be colored (ugh..) EnvelopeView::plain_text_to_buf(&text, self.mode == ViewMode::Url) }; @@ -344,7 +345,9 @@ impl Component for EnvelopeView { self.dirty = true; return true; } - UIEventType::Input(Key::Char('r')) if self.mode.is_attachment() || self.mode == ViewMode::Subview => { + UIEventType::Input(Key::Char('r')) + if self.mode.is_attachment() || self.mode == ViewMode::Subview => + { self.mode = ViewMode::Normal; self.subview.take(); self.dirty = true; @@ -358,12 +361,19 @@ impl Component for EnvelopeView { { let envelope: &Envelope = self.wrapper.envelope(); - if let Some(u) = envelope.body_bytes(self.wrapper.buffer()).attachments().get(lidx) { + if let Some(u) = envelope + .body_bytes(self.wrapper.buffer()) + .attachments() + .get(lidx) + { match u.content_type() { ContentType::MessageRfc822 => { self.mode = ViewMode::Subview; - self.subview = Some(Box::new(Pager::from_str(&String::from_utf8_lossy(&decode_rec(u, None)).to_string(), None))); - }, + self.subview = Some(Box::new(Pager::from_str( + &String::from_utf8_lossy(&decode_rec(u, None)).to_string(), + None, + ))); + } ContentType::Text { .. } => { self.mode = ViewMode::Attachment(lidx); @@ -416,7 +426,7 @@ impl Component for EnvelopeView { } }; return true; - }, + } UIEventType::Input(Key::Char('g')) if !self.cmd_buf.is_empty() && self.mode == ViewMode::Url => { @@ -425,7 +435,10 @@ impl Component for EnvelopeView { let url = { let envelope: &Envelope = self.wrapper.envelope(); let finder = LinkFinder::new(); - let mut t = envelope.body_bytes(self.wrapper.buffer()).text().to_string(); + let mut t = envelope + .body_bytes(self.wrapper.buffer()) + .text() + .to_string(); let links: Vec = finder.links(&t).collect(); if let Some(u) = links.get(lidx) { u.as_str().to_string() diff --git a/ui/src/components/mail/view/html.rs b/ui/src/components/mail/view/html.rs index bb308a0f..a9dbc717 100644 --- a/ui/src/components/mail/view/html.rs +++ b/ui/src/components/mail/view/html.rs @@ -70,31 +70,28 @@ impl Component for HtmlView { if self.pager.process_event(event, context) { return true; } - 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() - .unwrap_or_else(|_| panic!("Failed to start {}", binary.display())); - context.temp_files.push(p); - } else { - context.replies.push_back(UIEvent { - id: 0, - event_type: UIEventType::StatusNotification(format!( - "Couldn't find a default application for html files." - )), - }); - } - return true; + if let UIEventType::Input(Key::Char('v')) = event.event_type { + // 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() + .unwrap_or_else(|_| panic!("Failed to start {}", binary.display())); + context.temp_files.push(p); + } else { + context.replies.push_back(UIEvent { + id: 0, + event_type: UIEventType::StatusNotification( + "Couldn't find a default application for html files.".to_string(), + ), + }); } - _ => {} + return true; } false } diff --git a/ui/src/components/mail/view/mod.rs b/ui/src/components/mail/view/mod.rs index e830bd02..f5344718 100644 --- a/ui/src/components/mail/view/mod.rs +++ b/ui/src/components/mail/view/mod.rs @@ -82,7 +82,7 @@ impl MailView { coordinates: (usize, usize, usize), pager: Option, subview: Option>, - ) -> Self { + ) -> Self { MailView { coordinates, pager, @@ -95,33 +95,34 @@ impl MailView { } /// Returns the string to be displayed in the Viewer - fn attachment_to_text(&self, body: Attachment) -> String { + fn attachment_to_text(&self, body: &Attachment) -> String { let finder = LinkFinder::new(); let body_text = String::from_utf8_lossy(&decode_rec( - &body, - Some(Box::new(|a: &Attachment, v: &mut Vec| { - if a.content_type().is_text_html() { - use std::io::Write; - use std::process::{Command, Stdio}; + &body, + Some(Box::new(|a: &Attachment, v: &mut Vec| { + if a.content_type().is_text_html() { + use std::io::Write; + use std::process::{Command, Stdio}; - let mut html_filter = Command::new("w3m") - .args(&["-I", "utf-8", "-T", "text/html"]) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn() - .expect("Failed to start html filter process"); + let mut html_filter = Command::new("w3m") + .args(&["-I", "utf-8", "-T", "text/html"]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + .expect("Failed to start html filter process"); - html_filter - .stdin - .as_mut() - .unwrap() - .write_all(&v) - .expect("Failed to write to w3m stdin"); - *v = b"Text piped through `w3m`. Press `v` to open in web browser. \n\n".to_vec(); - v.extend(html_filter.wait_with_output().unwrap().stdout); - } - })), - )).into_owned(); + html_filter + .stdin + .as_mut() + .unwrap() + .write_all(&v) + .expect("Failed to write to w3m stdin"); + *v = b"Text piped through `w3m`. Press `v` to open in web browser. \n\n" + .to_vec(); + v.extend(html_filter.wait_with_output().unwrap().stdout); + } + })), + )).into_owned(); match self.mode { ViewMode::Normal | ViewMode::Subview => { let mut t = body_text.to_string(); @@ -172,7 +173,7 @@ impl MailView { } } } - pub fn plain_text_to_buf(s: &String, highlight_urls: bool) -> CellBuffer { + pub fn plain_text_to_buf(s: &str, highlight_urls: bool) -> CellBuffer { let mut buf = CellBuffer::from(s); if highlight_urls { @@ -237,7 +238,7 @@ impl Component for MailView { grid[(x, y)].set_fg(Color::Default); } let (x, y) = write_string_to_grid( - &format!("From: {}", envelope.from_to_string()), + &format!("From: {}", envelope.field_from_to_string()), grid, Color::Byte(33), Color::Default, @@ -250,7 +251,7 @@ impl Component for MailView { grid[(x, y)].set_fg(Color::Default); } let (x, y) = write_string_to_grid( - &format!("To: {}", envelope.to_to_string()), + &format!("To: {}", envelope.field_to_to_string()), grid, Color::Byte(33), Color::Default, @@ -302,7 +303,9 @@ impl Component for MailView { .as_ref() .unwrap(); let envelope: &Envelope = &mailbox.collection[mailbox_idx.2]; - let op = context.accounts[mailbox_idx.0].backend.operation(envelope.hash()); + let op = context.accounts[mailbox_idx.0] + .backend + .operation(envelope.hash()); let body = envelope.body(op); match self.mode { ViewMode::Attachment(aidx) if body.attachments()[aidx].is_html() => { @@ -317,7 +320,7 @@ impl Component for MailView { } _ => { let buf = { - let text = self.attachment_to_text(body); + let text = self.attachment_to_text(&body); // URL indexes must be colored (ugh..) MailView::plain_text_to_buf(&text, self.mode == ViewMode::Url) }; @@ -365,7 +368,9 @@ impl Component for MailView { }; self.dirty = true; } - UIEventType::Input(Key::Char('r')) if self.mode.is_attachment() || self.mode == ViewMode::Subview => { + UIEventType::Input(Key::Char('r')) + if self.mode.is_attachment() || self.mode == ViewMode::Subview => + { self.mode = ViewMode::Normal; self.subview.take(); self.dirty = true; @@ -383,26 +388,30 @@ impl Component for MailView { .unwrap(); let envelope: &Envelope = &mailbox.collection[self.coordinates.2]; - let op = context.accounts[self.coordinates.0].backend.operation(envelope.hash()); + let op = context.accounts[self.coordinates.0] + .backend + .operation(envelope.hash()); if let Some(u) = envelope.body(op).attachments().get(lidx) { match u.content_type() { ContentType::MessageRfc822 => { self.mode = ViewMode::Subview; match EnvelopeWrapper::new(u.bytes().to_vec()) { Ok(wrapper) => { - self.subview = Some(Box::new(EnvelopeView::new(wrapper, None, None))); - }, + self.subview = + Some(Box::new(EnvelopeView::new(wrapper, None, None))); + } Err(e) => { context.replies.push_back(UIEvent { id: 0, - event_type: UIEventType::StatusNotification( - format!("{}", e) - ), + event_type: UIEventType::StatusNotification(format!( + "{}", + e + )), }); } } return true; - }, + } ContentType::Text { .. } => { self.mode = ViewMode::Attachment(lidx); @@ -468,7 +477,9 @@ impl Component for MailView { let envelope: &Envelope = &mailbox.collection[self.coordinates.2]; let finder = LinkFinder::new(); - let op = context.accounts[self.coordinates.0].backend.operation(envelope.hash()); + let op = context.accounts[self.coordinates.0] + .backend + .operation(envelope.hash()); let mut t = envelope.body(op).text().to_string(); let links: Vec = finder.links(&t).collect(); if let Some(u) = links.get(lidx) { @@ -500,7 +511,9 @@ impl Component for MailView { } self.dirty = true; } - _ => { return false; } + _ => { + return false; + } } true } diff --git a/ui/src/components/mail/view/thread.rs b/ui/src/components/mail/view/thread.rs index e00c0724..6fa105bc 100644 --- a/ui/src/components/mail/view/thread.rs +++ b/ui/src/components/mail/view/thread.rs @@ -31,7 +31,6 @@ struct ThreadEntry { msg_idx: usize, } - #[derive(Debug, Default)] pub struct ThreadView { new_cursor_pos: usize, @@ -49,7 +48,9 @@ pub struct ThreadView { impl ThreadView { pub fn new(coordinates: (usize, usize, usize), context: &Context) -> Self { let mut stack: Vec<(usize, usize)> = Vec::with_capacity(32); - let mailbox = &context.accounts[coordinates.0][coordinates.1].as_ref().unwrap(); + let mailbox = &context.accounts[coordinates.0][coordinates.1] + .as_ref() + .unwrap(); let threads = &mailbox.threads; let container = &threads.containers()[threads.root_set()[coordinates.2]]; @@ -66,7 +67,7 @@ impl ThreadView { entries: Vec::new(), cursor_pos: 1, new_cursor_pos: 0, - .. Default::default() + ..Default::default() }; let mut line = 0; let mut max_ind = 0; @@ -87,22 +88,28 @@ impl ThreadView { view.new_expanded_pos = view.entries.len() - 1; view.expanded_pos = view.new_expanded_pos + 1; - let height = 2*view.entries.len(); + let height = 2 * view.entries.len(); let mut width = 0; let mut strings: Vec = Vec::with_capacity(view.entries.len()); - for e in &view.entries { let envelope: &Envelope = &mailbox.collection[e.msg_idx]; - strings.push(format!(" {}{} - {}", " ".repeat(e.index.0 * 4), envelope.date_as_str(), envelope.from_to_string())); - width = cmp::max(width, e.index.0 + strings.last().as_ref().unwrap().len() + 1); - + strings.push(format!( + " {}{} - {}", + " ".repeat(e.index.0 * 4), + envelope.date_as_str(), + envelope.field_from_to_string() + )); + width = cmp::max( + width, + e.index.0 + strings.last().as_ref().unwrap().len() + 1, + ); } - let mut content = CellBuffer::new(width, height, Cell::default()); + let mut content = CellBuffer::new(width, height, Cell::default()); for (y, e) in view.entries.iter().enumerate() { - if y > 0 && content.get_mut(e.index.0 * 4, 2*y - 1).is_some() { - let index = (e.index.0 * 4, 2*y - 1); + if y > 0 && content.get_mut(e.index.0 * 4, 2 * y - 1).is_some() { + let index = (e.index.0 * 4, 2 * y - 1); if content[index].ch() == ' ' { let mut ctr = 1; while content[(e.index.0 * 4 + ctr, 2 * y - 1)].ch() == ' ' { @@ -120,14 +127,14 @@ impl ThreadView { &mut content, Color::Default, Color::Default, - ((e.index.0 + 1, 2*y), (width - 1, height - 1)), + ((e.index.0 + 1, 2 * y), (width - 1, height - 1)), true, - ); - content[(e.index.0 * 4, 2*y)].set_ch(VERT_BOUNDARY); + ); + content[(e.index.0 * 4, 2 * y)].set_ch(VERT_BOUNDARY); for i in (e.index.0 * 4)..width { - content[(i, 2*y + 1)].set_ch(HORZ_BOUNDARY); + content[(i, 2 * y + 1)].set_ch(HORZ_BOUNDARY); } - content[(e.index.0 *4, 2*y + 1)].set_ch(LIGHT_UP_AND_HORIZONTAL); + content[(e.index.0 * 4, 2 * y + 1)].set_ch(LIGHT_UP_AND_HORIZONTAL); } //view.mailview = MailView::new((view.coordinates.0, view.coordinates.1, view.entries[view.expanded_pos].msg_idx), None, None); view.content = content; @@ -144,23 +151,28 @@ impl ThreadView { let msg_idx = if let Some(i) = container.message() { i } else { - mailbox.threads.containers()[ - container.first_child().unwrap() - ].message().unwrap() + mailbox.threads.containers()[container.first_child().unwrap()] + .message() + .unwrap() }; let envelope: &Envelope = &mailbox.collection[msg_idx]; - let op = context.accounts[self.coordinates.0].backend.operation(envelope.hash()); + let op = context.accounts[self.coordinates.0] + .backend + .operation(envelope.hash()); let body = envelope.body(op); let mut body_text: String = " \n".repeat(6); body_text.push_str(&String::from_utf8_lossy(&decode_rec(&body, None))); - let mut buf = CellBuffer::from(&body_text).split_newlines(); - + let mut buf = CellBuffer::from(body_text.as_str()).split_newlines(); let date = format!("Date: {}\n", envelope.date_as_str()); - let from = format!("From: {}\n", envelope.from_to_string()); + let from = format!("From: {}\n", envelope.field_from_to_string()); let message_id = &format!("Message-ID: <{}>\n\n", envelope.message_id_raw()); - let mut width = [date.len(), from.len(), message_id.len(), buf.size().0].iter().map(|&v| v).max().unwrap_or(1); + let mut width = [date.len(), from.len(), message_id.len(), buf.size().0] + .iter() + .cloned() + .max() + .unwrap_or(1); let height = buf.size().1; if width > buf.size().0 { buf.resize(width, height, Cell::default()); @@ -176,7 +188,7 @@ impl ThreadView { Color::Default, ((ind, 0), (width, height)), true, - ); + ); write_string_to_grid( &from, &mut buf, @@ -184,7 +196,7 @@ impl ThreadView { Color::Default, ((ind, 1), (width, height)), true, - ); + ); write_string_to_grid( &message_id, &mut buf, @@ -192,7 +204,7 @@ impl ThreadView { Color::Default, ((ind, 2), (width, height)), true, - ); + ); ThreadEntry { index: (ind, idx, order), @@ -237,8 +249,8 @@ impl ThreadView { self.cursor_pos = self.new_cursor_pos; for &idx in &[old_cursor_pos, self.new_cursor_pos] { let new_area = ( - set_y(upper_left, get_y(upper_left) + 2*(idx % rows)), - set_y(bottom_right, get_y(upper_left) + 2*(idx % rows)), + set_y(upper_left, get_y(upper_left) + 2 * (idx % rows)), + set_y(bottom_right, get_y(upper_left) + 2 * (idx % rows)), ); self.highlight_line(grid, new_area, idx); context.dirty_areas.push_back(new_area); @@ -253,13 +265,16 @@ impl ThreadView { grid, &self.content, area, - ((0, 2*top_idx), (width - 1, height - 1)), + ((0, 2 * top_idx), (width - 1, height - 1)), ); self.highlight_line( grid, ( - set_y(upper_left, get_y(upper_left) + 2*(self.cursor_pos % rows)), - set_y(bottom_right, get_y(upper_left) + 2*(self.cursor_pos % rows)), + set_y(upper_left, get_y(upper_left) + 2 * (self.cursor_pos % rows)), + set_y( + bottom_right, + get_y(upper_left) + 2 * (self.cursor_pos % rows), + ), ), self.cursor_pos, ); @@ -292,22 +307,25 @@ impl Component for ThreadView { let mid = get_y(upper_left) + total_rows - bottom_entity_rows; if !self.dirty { - self.mailview.draw(grid, (set_y(upper_left, mid + 1), bottom_right), context); + self.mailview + .draw(grid, (set_y(upper_left, mid + 1), bottom_right), context); return; } self.dirty = false; let y = { - let mailbox = &mut context.accounts[self.coordinates.0][self.coordinates.1].as_ref().unwrap(); + let mailbox = &mut context.accounts[self.coordinates.0][self.coordinates.1] + .as_ref() + .unwrap(); let threads = &mailbox.threads; let container = &threads.containers()[threads.root_set()[self.coordinates.2]]; let i = if let Some(i) = container.message() { i } else { - threads.containers()[ - container.first_child().unwrap() - ].message().unwrap() + threads.containers()[container.first_child().unwrap()] + .message() + .unwrap() }; let envelope: &Envelope = &mailbox.collection[i]; @@ -318,7 +336,7 @@ impl Component for ThreadView { Color::Default, area, true, - ); + ); for x in x..=get_x(bottom_right) { grid[(x, y)].set_ch(' '); grid[(x, y)].set_bg(Color::Default); @@ -341,7 +359,7 @@ impl Component for ThreadView { &self.content, (set_y(upper_left, y), set_y(bottom_right, mid - 1)), ((0, 0), (width - 1, height - 1)), - ); + ); for x in get_x(upper_left)..=get_x(bottom_right) { grid[(x, mid)].set_ch(HORZ_BOUNDARY); } @@ -356,10 +374,23 @@ impl Component for ThreadView { if self.new_expanded_pos != self.expanded_pos { self.expanded_pos = self.new_expanded_pos; - self.mailview = MailView::new((self.coordinates.0, self.coordinates.1, self.entries[self.expanded_pos].msg_idx), None, None); + self.mailview = MailView::new( + ( + self.coordinates.0, + self.coordinates.1, + self.entries[self.expanded_pos].msg_idx, + ), + None, + None, + ); } - self.draw_list(grid, (set_y(upper_left, y), set_y(bottom_right, mid - 1)), context); - self.mailview.draw(grid, (set_y(upper_left, mid + 1), bottom_right), context); + self.draw_list( + grid, + (set_y(upper_left, y), set_y(bottom_right, mid - 1)), + context, + ); + self.mailview + .draw(grid, (set_y(upper_left, mid + 1), bottom_right), context); } fn process_event(&mut self, event: &UIEvent, context: &mut Context) -> bool { if self.mailview.process_event(event, context) { @@ -372,7 +403,7 @@ impl Component for ThreadView { self.dirty = true; } return true; - }, + } UIEventType::Input(Key::Down) => { let height = self.entries.len(); if height > 0 && self.cursor_pos + 1 < height { @@ -385,10 +416,10 @@ impl Component for ThreadView { self.new_expanded_pos = self.cursor_pos; self.dirty = true; return true; - }, + } UIEventType::Resize => { self.dirty = true; - }, + } _ => {} } false -- cgit v1.2.3