diff options
author | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2018-08-05 12:44:31 +0300 |
---|---|---|
committer | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2019-06-10 19:40:26 +0300 |
commit | 375b256a4e8eead6a4d7989b667554d542723abb (patch) | |
tree | 11f7119f97b28e294c59e3bbffcd308283fef0e3 | |
parent | 63ce729205f4063112ebdd9ba5bdef858f1028c9 (diff) |
Add raw mode view in email
-rw-r--r-- | melib/src/mailbox/email/mod.rs | 4 | ||||
-rw-r--r-- | ui/src/components/mail/view.rs | 181 | ||||
-rw-r--r-- | ui/src/components/utilities.rs | 4 |
3 files changed, 105 insertions, 84 deletions
diff --git a/melib/src/mailbox/email/mod.rs b/melib/src/mailbox/email/mod.rs index fe9eced3..5f68f9d7 100644 --- a/melib/src/mailbox/email/mod.rs +++ b/melib/src/mailbox/email/mod.rs @@ -358,6 +358,10 @@ impl Envelope { let _strings: Vec<String> = self.to.iter().map(|a| format!("{}", a)).collect(); _strings.join(", ") } + pub fn bytes(&self) -> Vec<u8> { + let mut operation = self.operation_token.generate(); + operation.as_bytes().map(|v| v.into()).unwrap_or_else(|_| Vec::new()) + } pub fn body(&self) -> Attachment { let mut operation = self.operation_token.generate(); let file = operation.as_bytes(); diff --git a/ui/src/components/mail/view.rs b/ui/src/components/mail/view.rs index 649737ac..a5644ea0 100644 --- a/ui/src/components/mail/view.rs +++ b/ui/src/components/mail/view.rs @@ -9,7 +9,7 @@ enum ViewMode { Normal, Url, Attachment(usize), - // Raw, + Raw, } impl ViewMode { @@ -73,76 +73,84 @@ impl Component for MailView { let envelope: &Envelope = &mailbox.collection[envelope_idx]; - let (x, y) = write_string_to_grid( - &format!("Date: {}", envelope.date_as_str()), - grid, - Color::Byte(33), - Color::Default, - area, - true, - ); - for x in x..=get_x(bottom_right) { - grid[(x, y)].set_ch(' '); - grid[(x, y)].set_bg(Color::Default); - grid[(x, y)].set_fg(Color::Default); - } - let (x, y) = write_string_to_grid( - &format!("From: {}", envelope.from_to_string()), - grid, - Color::Byte(33), - Color::Default, - (set_y(upper_left, y + 1), bottom_right), - true, - ); - for x in x..=get_x(bottom_right) { - grid[(x, y)].set_ch(' '); - grid[(x, y)].set_bg(Color::Default); - grid[(x, y)].set_fg(Color::Default); - } - let (x, y) = write_string_to_grid( - &format!("To: {}", envelope.to_to_string()), - grid, - Color::Byte(33), - Color::Default, - (set_y(upper_left, y + 1), bottom_right), - true, - ); - for x in x..=get_x(bottom_right) { - grid[(x, y)].set_ch(' '); - grid[(x, y)].set_bg(Color::Default); - grid[(x, y)].set_fg(Color::Default); - } - let (x, y) = write_string_to_grid( - &format!("Subject: {}", envelope.subject()), - grid, - Color::Byte(33), - Color::Default, - (set_y(upper_left, y + 1), bottom_right), - true, - ); - for x in x..=get_x(bottom_right) { - grid[(x, y)].set_ch(' '); - grid[(x, y)].set_bg(Color::Default); - grid[(x, y)].set_fg(Color::Default); - } - let (x, y) = write_string_to_grid( - &format!("Message-ID: {}", envelope.message_id_raw()), - grid, - Color::Byte(33), - Color::Default, - (set_y(upper_left, y + 1), bottom_right), - true, - ); - for x in x..=get_x(bottom_right) { - grid[(x, y)].set_ch(' '); - grid[(x, y)].set_bg(Color::Default); - grid[(x, y)].set_fg(Color::Default); + if self.mode == ViewMode::Raw { + clear_area(grid, area); + context + .dirty_areas + .push_back(area); + (envelope_idx, get_y(upper_left) - 1) + } else { + let (x, y) = write_string_to_grid( + &format!("Date: {}", envelope.date_as_str()), + grid, + Color::Byte(33), + Color::Default, + area, + true, + ); + for x in x..=get_x(bottom_right) { + grid[(x, y)].set_ch(' '); + grid[(x, y)].set_bg(Color::Default); + grid[(x, y)].set_fg(Color::Default); + } + let (x, y) = write_string_to_grid( + &format!("From: {}", envelope.from_to_string()), + grid, + Color::Byte(33), + Color::Default, + (set_y(upper_left, y + 1), bottom_right), + true, + ); + for x in x..=get_x(bottom_right) { + grid[(x, y)].set_ch(' '); + grid[(x, y)].set_bg(Color::Default); + grid[(x, y)].set_fg(Color::Default); + } + let (x, y) = write_string_to_grid( + &format!("To: {}", envelope.to_to_string()), + grid, + Color::Byte(33), + Color::Default, + (set_y(upper_left, y + 1), bottom_right), + true, + ); + for x in x..=get_x(bottom_right) { + grid[(x, y)].set_ch(' '); + grid[(x, y)].set_bg(Color::Default); + grid[(x, y)].set_fg(Color::Default); + } + let (x, y) = write_string_to_grid( + &format!("Subject: {}", envelope.subject()), + grid, + Color::Byte(33), + Color::Default, + (set_y(upper_left, y + 1), bottom_right), + true, + ); + for x in x..=get_x(bottom_right) { + grid[(x, y)].set_ch(' '); + grid[(x, y)].set_bg(Color::Default); + grid[(x, y)].set_fg(Color::Default); + } + let (x, y) = write_string_to_grid( + &format!("Message-ID: {}", envelope.message_id_raw()), + grid, + Color::Byte(33), + Color::Default, + (set_y(upper_left, y + 1), bottom_right), + true, + ); + for x in x..=get_x(bottom_right) { + grid[(x, y)].set_ch(' '); + grid[(x, y)].set_bg(Color::Default); + grid[(x, y)].set_fg(Color::Default); + } + clear_area(grid, (set_y(upper_left, y + 1), set_y(bottom_right, y + 2))); + context + .dirty_areas + .push_back((upper_left, set_y(bottom_right, y + 1))); + (envelope_idx, y + 1) } - clear_area(grid, (set_y(upper_left, y + 1), set_y(bottom_right, y + 2))); - context - .dirty_areas - .push_back((upper_left, set_y(bottom_right, y + 1))); - (envelope_idx, y + 1) }; if self.dirty { @@ -157,11 +165,8 @@ impl Component for MailView { let finder = LinkFinder::new(); let mut text = match self.mode { - ViewMode::Url => { + ViewMode::Normal => { let mut t = envelope.body().text().to_string(); - for (lidx, l) in finder.links(&envelope.body().text()).enumerate() { - t.insert_str(l.start() + (lidx * 3), &format!("[{}]", lidx)); - } if envelope.body().count_attachments() > 1 { t = envelope.body().attachments().iter().enumerate().fold( t, @@ -173,14 +178,14 @@ impl Component for MailView { } t } - ViewMode::Attachment(aidx) => { - let attachments = envelope.body().attachments(); - let mut ret = format!("Viewing attachment. Press `r` to return \n"); - ret.push_str(&attachments[aidx].text()); - ret - } - _ => { + ViewMode::Raw => { + String::from_utf8_lossy(&envelope.bytes()).into_owned() + }, + ViewMode::Url => { let mut t = envelope.body().text().to_string(); + for (lidx, l) in finder.links(&envelope.body().text()).enumerate() { + t.insert_str(l.start() + (lidx * 3), &format!("[{}]", lidx)); + } if envelope.body().count_attachments() > 1 { t = envelope.body().attachments().iter().enumerate().fold( t, @@ -192,6 +197,12 @@ impl Component for MailView { } t } + ViewMode::Attachment(aidx) => { + let attachments = envelope.body().attachments(); + let mut ret = format!("Viewing attachment. Press `r` to return \n"); + ret.push_str(&attachments[aidx].text()); + ret + } }; let mut buf = CellBuffer::from(&text); match self.mode { @@ -236,6 +247,14 @@ impl Component for MailView { //TODO:this should be an Action self.cmd_buf.push(c); } + UIEventType::Input(Key::Char('r')) if self.mode == ViewMode::Normal || self.mode == ViewMode::Raw => { + self.mode = if self.mode == ViewMode::Raw { + ViewMode::Normal + } else { + ViewMode::Raw + }; + self.dirty = true; + } UIEventType::Input(Key::Char('r')) if self.mode.is_attachment() => { //TODO:one quit shortcut? self.mode = ViewMode::Normal; diff --git a/ui/src/components/utilities.rs b/ui/src/components/utilities.rs index 43573813..e2c7de65 100644 --- a/ui/src/components/utilities.rs +++ b/ui/src/components/utilities.rs @@ -259,15 +259,13 @@ impl Component for Pager { if !self.is_dirty() { return; } - let upper_left = upper_left!(area); - let bottom_right = bottom_right!(area); self.dirty = false; if self.height == 0 || self.height == self.cursor_pos || self.width == 0 { return; } - clear_area(grid, (upper_left, bottom_right)); + clear_area(grid, area); //let pager_context: usize = context.settings.pager.pager_context; //let pager_stop: bool = context.settings.pager.pager_stop; //let rows = y(bottom_right) - y(upper_left); |