From 0bcea12400dc9c9dd8073e3c802af8cb0e2a97ca Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Mon, 23 Jul 2018 22:21:41 +0300 Subject: [PATCH] Color url index in open-link concerns #13 --- ui/src/cells.rs | 12 ++++++++++++ ui/src/components/mail/view.rs | 29 +++++++++++++++++++++++------ ui/src/components/mod.rs | 7 ++++++- ui/src/components/utilities.rs | 26 +++++++++++++++++++++++++- 4 files changed, 66 insertions(+), 8 deletions(-) diff --git a/ui/src/cells.rs b/ui/src/cells.rs index 0d05e0254..ce3bd5cdc 100644 --- a/ui/src/cells.rs +++ b/ui/src/cells.rs @@ -3,6 +3,7 @@ colors and attributes. */ use std::ops::{Index, IndexMut, Deref, DerefMut}; +use std::convert::From; use super::position::*; use termion::color::AnsiValue; @@ -163,6 +164,17 @@ impl Default for CellBuffer { } } +impl<'a> From<&'a String> for CellBuffer { + fn from(s: &'a String) -> Self { + let len = s.len(); + let mut buf = CellBuffer::new(len, 1, Cell::default()); + for (idx, c) in s.chars().enumerate() { + buf[(idx, 0)].set_ch(c); + } + buf + } +} + /// A single point on a terminal display. /// /// A `Cell` contains a character and style. diff --git a/ui/src/components/mail/view.rs b/ui/src/components/mail/view.rs index 60d7c5a12..90ba031ca 100644 --- a/ui/src/components/mail/view.rs +++ b/ui/src/components/mail/view.rs @@ -117,29 +117,46 @@ impl Component for MailView { }; if self.dirty { - let text = { + let buf = { 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]; + + let finder = LinkFinder::new(); + let mut t = envelope.body().text().to_string(); let mut text = match self.mode { ViewMode::Url => { - let finder = LinkFinder::new(); - 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)); } t }, - _ => envelope.body().text().to_string(), + _ => envelope.body().text().to_string() }; if envelope.body().count_attachments() > 1 { text = envelope.body().attachments().iter().enumerate().fold(text, |mut s, (idx, a)| { s.push_str(&format!("[{}] {}\n\n", idx, a)); s }); } - text + let mut buf = CellBuffer::from(&text); + match self.mode { + ViewMode::Url => { + let c_slice: &mut [Cell] = &mut buf; + for (lidx, l) in finder.links(&envelope.body().text()).enumerate() { + let i = l.start()+ (lidx * 3); + for c in c_slice[i..i+3].iter_mut() { + c.set_fg(Color::Byte(226)); + } + + + + } + }, + _ => {}, + } + buf }; let cursor_pos = self.pager.as_mut().map(|p| p.cursor_pos()); // TODO: pass string instead of envelope - self.pager = Some(Pager::from_string(text, context, cursor_pos)); + self.pager = Some(Pager::from_buf(buf)); self.dirty = false; } self.pager.as_mut().map(|p| p.draw(grid, (set_y(upper_left, y + 1),bottom_right), context)); diff --git a/ui/src/components/mod.rs b/ui/src/components/mod.rs index 498be780d..5b9218463 100644 --- a/ui/src/components/mod.rs +++ b/ui/src/components/mod.rs @@ -84,6 +84,11 @@ pub trait Component { } } +pub fn copy_area_with_break(grid_dest: &mut CellBuffer, grid_src: &CellBuffer, dest: Area, src: Area) { + + +} + /// Copy a source `Area` to a destination. pub fn copy_area(grid_dest: &mut CellBuffer, grid_src: &CellBuffer, dest: Area, src: Area) { if !is_valid_area!(dest) || !is_valid_area!(src) { @@ -174,7 +179,7 @@ fn clear_area(grid: &mut CellBuffer, area: Area) { } } -fn new_draft(context: &mut Context) -> Vec { +fn new_draft(_context: &mut Context) -> Vec { // TODO: Generate proper message-id https://www.jwz.org/doc/mid.html let mut v = String::with_capacity(500); v.push_str("From: \n"); diff --git a/ui/src/components/utilities.rs b/ui/src/components/utilities.rs index b5822eabf..264ece737 100644 --- a/ui/src/components/utilities.rs +++ b/ui/src/components/utilities.rs @@ -178,7 +178,7 @@ impl Pager { content: content, } } - pub fn new_from_str(s: &str) -> Self { + pub fn from_str(s: &str) -> Self { let lines: Vec<&str> = s.trim().split('\n').collect(); let height = lines.len(); let width = lines.iter().map(|l| l.len()).max().unwrap_or(0); @@ -192,6 +192,30 @@ impl Pager { content: content, } } + pub fn from_buf(buf: CellBuffer) -> Self { + let lines: Vec<&[Cell]> = buf.split(|cell| cell.ch() == '\n').collect(); + let height = lines.len(); + let width = lines.iter().map(|l| l.len()).max().unwrap_or(0); + let mut content = CellBuffer::new(width, height, Cell::with_char(' ')); + { + let mut x = 0; + let mut y = 0; + let c_slice: &mut [Cell] = &mut content; + for l in lines { + let y_r = y * width; + x = l.len() + y_r; + c_slice[y_r..x].copy_from_slice(l); + y += 1; + } + } + Pager { + cursor_pos: 0, + height: height, + width: width, + dirty: true, + content: content, + } + } pub fn print_string(content: &mut CellBuffer, s: &str) { let lines: Vec<&str> = s.trim().split('\n').collect(); let width = lines.iter().map(|l| l.len()).max().unwrap_or(0);