|
|
|
@ -1,8 +1,28 @@
|
|
|
|
|
/*
|
|
|
|
|
* meli - ui crate.
|
|
|
|
|
*
|
|
|
|
|
* Copyright 2017-2018 Manos Pitsidianakis
|
|
|
|
|
*
|
|
|
|
|
* This file is part of meli.
|
|
|
|
|
*
|
|
|
|
|
* meli is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* meli is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with meli. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
/// `MailView`.
|
|
|
|
|
pub struct MailListing {
|
|
|
|
@ -21,6 +41,12 @@ pub struct MailListing {
|
|
|
|
|
view: Option<MailView>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for MailListing {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self::new()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MailListing {
|
|
|
|
|
/// Helper function to format entry strings for MailListing */
|
|
|
|
|
/* TODO: Make this configurable */
|
|
|
|
@ -67,11 +93,9 @@ impl MailListing {
|
|
|
|
|
// Get mailbox as a reference.
|
|
|
|
|
//
|
|
|
|
|
loop {
|
|
|
|
|
match context.accounts[self.cursor_pos.0].status(self.cursor_pos.1) {
|
|
|
|
|
Ok(()) => { break; },
|
|
|
|
|
Err(_) => {
|
|
|
|
|
// TODO: Show progress visually
|
|
|
|
|
}
|
|
|
|
|
// TODO: Show progress visually
|
|
|
|
|
if let Ok(()) = context.accounts[self.cursor_pos.0].status(self.cursor_pos.1) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let mailbox = &mut context.accounts[self.cursor_pos.0][self.cursor_pos.1]
|
|
|
|
@ -92,7 +116,7 @@ impl MailListing {
|
|
|
|
|
Color::Default,
|
|
|
|
|
((0, 0), (MAX_COLS - 1, 0)),
|
|
|
|
|
true,
|
|
|
|
|
);
|
|
|
|
|
);
|
|
|
|
|
self.content = content;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
@ -101,31 +125,29 @@ 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 local_collection: Vec<usize> = mailbox.threaded_collection.clone();
|
|
|
|
|
let mut threads: Vec<&Container> = mailbox.threads.iter().map(|v| v).collect();
|
|
|
|
|
local_collection.sort_by(|a, b| {
|
|
|
|
|
match self.sort {
|
|
|
|
|
(SortField::Date, SortOrder::Desc) => {
|
|
|
|
|
mailbox.thread(*b).date().cmp(&mailbox.thread(*a).date())
|
|
|
|
|
},
|
|
|
|
|
(SortField::Date, SortOrder::Asc) => {
|
|
|
|
|
mailbox.thread(*a).date().cmp(&mailbox.thread(*b).date())
|
|
|
|
|
},
|
|
|
|
|
(SortField::Subject, SortOrder::Desc) => {
|
|
|
|
|
let a = mailbox.thread(*a);
|
|
|
|
|
let b = mailbox.thread(*b);
|
|
|
|
|
let ma = &mailbox.collection[*a.message().as_ref().unwrap()];
|
|
|
|
|
let mb = &mailbox.collection[*b.message().as_ref().unwrap()];
|
|
|
|
|
ma.subject().cmp(&mb.subject())
|
|
|
|
|
},
|
|
|
|
|
(SortField::Subject, SortOrder::Asc) => {
|
|
|
|
|
let a = mailbox.thread(*a);
|
|
|
|
|
let b = mailbox.thread(*b);
|
|
|
|
|
let ma = &mailbox.collection[*a.message().as_ref().unwrap()];
|
|
|
|
|
let mb = &mailbox.collection[*b.message().as_ref().unwrap()];
|
|
|
|
|
mb.subject().cmp(&ma.subject())
|
|
|
|
|
},
|
|
|
|
|
let threads: &Vec<Container> = &mailbox.threads;
|
|
|
|
|
local_collection.sort_by(|a, b| match self.sort {
|
|
|
|
|
(SortField::Date, SortOrder::Desc) => {
|
|
|
|
|
mailbox.thread(*b).date().cmp(&mailbox.thread(*a).date())
|
|
|
|
|
}
|
|
|
|
|
(SortField::Date, SortOrder::Asc) => {
|
|
|
|
|
mailbox.thread(*a).date().cmp(&mailbox.thread(*b).date())
|
|
|
|
|
}
|
|
|
|
|
(SortField::Subject, SortOrder::Desc) => {
|
|
|
|
|
let a = mailbox.thread(*a);
|
|
|
|
|
let b = mailbox.thread(*b);
|
|
|
|
|
let ma = &mailbox.collection[*a.message().as_ref().unwrap()];
|
|
|
|
|
let mb = &mailbox.collection[*b.message().as_ref().unwrap()];
|
|
|
|
|
ma.subject().cmp(&mb.subject())
|
|
|
|
|
}
|
|
|
|
|
(SortField::Subject, SortOrder::Asc) => {
|
|
|
|
|
let a = mailbox.thread(*a);
|
|
|
|
|
let b = mailbox.thread(*b);
|
|
|
|
|
let ma = &mailbox.collection[*a.message().as_ref().unwrap()];
|
|
|
|
|
let mb = &mailbox.collection[*b.message().as_ref().unwrap()];
|
|
|
|
|
mb.subject().cmp(&ma.subject())
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
let mut iter = local_collection.iter().enumerate().peekable();
|
|
|
|
@ -137,14 +159,14 @@ impl MailListing {
|
|
|
|
|
.count();
|
|
|
|
|
/* This is just a desugared for loop so that we can use .peek() */
|
|
|
|
|
while let Some((idx, i)) = iter.next() {
|
|
|
|
|
let container = threads[*i];
|
|
|
|
|
let container = &threads[*i];
|
|
|
|
|
let indentation = container.indentation();
|
|
|
|
|
|
|
|
|
|
if indentation == 0 {
|
|
|
|
|
thread_idx += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert!(container.has_message() == true);
|
|
|
|
|
assert!(container.has_message());
|
|
|
|
|
match iter.peek() {
|
|
|
|
|
Some(&(_, x)) if threads[*x].indentation() == indentation => {
|
|
|
|
|
indentations.pop();
|
|
|
|
@ -180,13 +202,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);
|
|
|
|
@ -236,7 +258,7 @@ impl MailListing {
|
|
|
|
|
bg_color,
|
|
|
|
|
((0, y), (MAX_COLS - 1, y)),
|
|
|
|
|
false,
|
|
|
|
|
);
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
for x in x..MAX_COLS {
|
|
|
|
|
content[(x, y)].set_ch(' ');
|
|
|
|
@ -269,15 +291,19 @@ impl MailListing {
|
|
|
|
|
} else {
|
|
|
|
|
Color::Default
|
|
|
|
|
};
|
|
|
|
|
let bg_color =
|
|
|
|
|
if !envelope.is_seen() {
|
|
|
|
|
Color::Byte(251)
|
|
|
|
|
} else if idx % 2 == 0 {
|
|
|
|
|
Color::Byte(236)
|
|
|
|
|
} else {
|
|
|
|
|
Color::Default
|
|
|
|
|
};
|
|
|
|
|
change_colors(&mut self.content, ((0, idx), (MAX_COLS-1, idx)), fg_color, bg_color);
|
|
|
|
|
let bg_color = if !envelope.is_seen() {
|
|
|
|
|
Color::Byte(251)
|
|
|
|
|
} else if idx % 2 == 0 {
|
|
|
|
|
Color::Byte(236)
|
|
|
|
|
} else {
|
|
|
|
|
Color::Default
|
|
|
|
|
};
|
|
|
|
|
change_colors(
|
|
|
|
|
&mut self.content,
|
|
|
|
|
((0, idx), (MAX_COLS - 1, idx)),
|
|
|
|
|
fg_color,
|
|
|
|
|
bg_color,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn highlight_line(&self, grid: &mut CellBuffer, area: Area, idx: usize, context: &Context) {
|
|
|
|
@ -301,14 +327,12 @@ impl MailListing {
|
|
|
|
|
};
|
|
|
|
|
let bg_color = if self.cursor_pos.2 == idx {
|
|
|
|
|
Color::Byte(246)
|
|
|
|
|
} else if !envelope.is_seen() {
|
|
|
|
|
Color::Byte(251)
|
|
|
|
|
} else if idx % 2 == 0 {
|
|
|
|
|
Color::Byte(236)
|
|
|
|
|
} else {
|
|
|
|
|
if !envelope.is_seen() {
|
|
|
|
|
Color::Byte(251)
|
|
|
|
|
} else if idx % 2 == 0 {
|
|
|
|
|
Color::Byte(236)
|
|
|
|
|
} else {
|
|
|
|
|
Color::Default
|
|
|
|
|
}
|
|
|
|
|
Color::Default
|
|
|
|
|
};
|
|
|
|
|
change_colors(grid, area, fg_color, bg_color);
|
|
|
|
|
}
|
|
|
|
@ -337,7 +361,7 @@ impl MailListing {
|
|
|
|
|
if self.cursor_pos.2 != self.new_cursor_pos.2 && prev_page_no == page_no {
|
|
|
|
|
let old_cursor_pos = self.cursor_pos;
|
|
|
|
|
self.cursor_pos = self.new_cursor_pos;
|
|
|
|
|
for idx in [old_cursor_pos.2, self.new_cursor_pos.2].iter() {
|
|
|
|
|
for idx in &[old_cursor_pos.2, self.new_cursor_pos.2] {
|
|
|
|
|
if *idx >= self.length {
|
|
|
|
|
continue; //bounds check
|
|
|
|
|
}
|
|
|
|
@ -370,8 +394,6 @@ impl MailListing {
|
|
|
|
|
context,
|
|
|
|
|
);
|
|
|
|
|
context.dirty_areas.push_back(area);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn make_thread_entry(
|
|
|
|
@ -379,7 +401,7 @@ impl MailListing {
|
|
|
|
|
idx: usize,
|
|
|
|
|
indent: usize,
|
|
|
|
|
container: &Container,
|
|
|
|
|
indentations: &Vec<bool>,
|
|
|
|
|
indentations: &[bool],
|
|
|
|
|
idx_width: usize,
|
|
|
|
|
) -> String {
|
|
|
|
|
let has_sibling = container.has_sibling();
|
|
|
|
@ -508,10 +530,9 @@ impl Component for MailListing {
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
/* TODO: Move the box drawing business in separate functions */
|
|
|
|
|
if get_x(upper_left) > 0 {
|
|
|
|
|
if grid[(get_x(upper_left) - 1, mid)].ch() == VERT_BOUNDARY {
|
|
|
|
|
grid[(get_x(upper_left) - 1, mid)].set_ch(LIGHT_VERTICAL_AND_RIGHT);
|
|
|
|
|
}
|
|
|
|
|
if get_x(upper_left) > 0 && grid[(get_x(upper_left) - 1, mid)].ch() == VERT_BOUNDARY
|
|
|
|
|
{
|
|
|
|
|
grid[(get_x(upper_left) - 1, mid)].set_ch(LIGHT_VERTICAL_AND_RIGHT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i in get_x(upper_left)..=get_x(bottom_right) {
|
|
|
|
@ -524,17 +545,18 @@ impl Component for MailListing {
|
|
|
|
|
// TODO: Make headers view configurable
|
|
|
|
|
|
|
|
|
|
if !self.dirty {
|
|
|
|
|
self.view
|
|
|
|
|
.as_mut()
|
|
|
|
|
.map(|v| v.draw(grid, (set_y(upper_left, mid + 1), bottom_right), context));
|
|
|
|
|
if let Some(v) = self.view.as_mut() {
|
|
|
|
|
v.draw(grid, (set_y(upper_left, mid + 1), bottom_right), context);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
self.view = Some(MailView::new(self.cursor_pos, None, None));
|
|
|
|
|
self.view
|
|
|
|
|
.as_mut()
|
|
|
|
|
.map(|v| v.draw(grid, (set_y(upper_left, mid + 1), bottom_right), context));
|
|
|
|
|
self.view.as_mut().unwrap().draw(
|
|
|
|
|
grid,
|
|
|
|
|
(set_y(upper_left, mid + 1), bottom_right),
|
|
|
|
|
context,
|
|
|
|
|
);
|
|
|
|
|
self.dirty = false;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fn process_event(&mut self, event: &UIEvent, context: &mut Context) {
|
|
|
|
@ -551,11 +573,11 @@ impl Component for MailListing {
|
|
|
|
|
self.dirty = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
UIEventType::Input(Key::Char('\n')) if self.unfocused == false => {
|
|
|
|
|
UIEventType::Input(Key::Char('\n')) if !self.unfocused => {
|
|
|
|
|
self.unfocused = true;
|
|
|
|
|
self.dirty = true;
|
|
|
|
|
}
|
|
|
|
|
UIEventType::Input(Key::Char('m')) if self.unfocused == false => {
|
|
|
|
|
UIEventType::Input(Key::Char('m')) if !self.unfocused => {
|
|
|
|
|
use std::process::{Command, Stdio};
|
|
|
|
|
/* Kill input thread so that spawned command can be sole receiver of stdin */
|
|
|
|
|
{
|
|
|
|
@ -602,7 +624,7 @@ impl Component for MailListing {
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
UIEventType::Input(Key::Char('i')) if self.unfocused == true => {
|
|
|
|
|
UIEventType::Input(Key::Char('i')) if self.unfocused => {
|
|
|
|
|
self.unfocused = false;
|
|
|
|
|
self.dirty = true;
|
|
|
|
|
self.view = None;
|
|
|
|
@ -683,20 +705,20 @@ impl Component for MailListing {
|
|
|
|
|
self.refresh_mailbox(context);
|
|
|
|
|
self.dirty = true;
|
|
|
|
|
return;
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
Action::ViewMailbox(idx) => {
|
|
|
|
|
self.new_cursor_pos.1 = *idx;
|
|
|
|
|
self.dirty = true;
|
|
|
|
|
self.refresh_mailbox(context);
|
|
|
|
|
return;
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
Action::Sort(field, order) => {
|
|
|
|
|
self.sort = (field.clone(), order.clone());
|
|
|
|
|
self.dirty = true;
|
|
|
|
|
self.refresh_mailbox(context);
|
|
|
|
|
return;
|
|
|
|
|
},
|
|
|
|
|
_ => {},
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
},
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|