Color url index in open-link

concerns #13
embed
Manos Pitsidianakis 2018-07-23 22:21:41 +03:00
parent d962da665f
commit 0bcea12400
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
4 changed files with 66 additions and 8 deletions

View File

@ -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.

View File

@ -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));

View File

@ -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<u8> {
fn new_draft(_context: &mut Context) -> Vec<u8> {
// TODO: Generate proper message-id https://www.jwz.org/doc/mid.html
let mut v = String::with_capacity(500);
v.push_str("From: \n");

View File

@ -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);