From 4681214e63fad92850b62335fb32c11fde51e94f Mon Sep 17 00:00:00 2001 From: Andy Wortman Date: Sun, 26 Nov 2017 22:40:58 -0800 Subject: colorize @ in tweet text update todo --- src/display/mod.rs | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- todo | 4 ---- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/src/display/mod.rs b/src/display/mod.rs index 0cc1050..3e9d725 100644 --- a/src/display/mod.rs +++ b/src/display/mod.rs @@ -686,12 +686,56 @@ pub fn render_twete_no_recurse(twete_id: &TweetId, tweeter: &tw::TwitterCache, d result.push(id_string); result.push(author_string); - let mut original_lines: Vec = twete.text -// .replace("\r", "\\r") + let raw_lines: Vec = twete.text .split("\n").map(|line| line.replace("\r", "\\r")).collect(); + // now colorize @'s: + let mut colorized_lines: Vec = vec![]; + + for line in raw_lines { + let mut name: Option = None; + let mut new_line = String::new(); + for c in line.chars() { + name = match name { + Some(mut handle) => { + match c { + 'a'...'z' | 'A'...'Z' | '0'...'9' | '_' => { + // so if we have a handle WIP, append to that string. + handle.push(c); + Some(handle) + }, + c => { + // we HAD a handle, this just terminated it. + // if it was empty string, it's not really a mention, we can + // discard it. + if handle.len() > 0 { + new_line.push_str(&format!("{}@{}{}{}", color_for(&handle), &handle, termion::style::Reset, c)); + } else { + new_line.push('@'); + new_line.push(c); + } + None + } + } + }, + None => { + if c == '@' { + Some(String::new()) + } else { + new_line.push(c); + None + } + } + } + } + if let Some(mut handle) = name { + new_line.push_str(&format!("{}@{}{}", color_for(&handle), &handle, termion::style::Reset)); + } + colorized_lines.push(new_line); + } + let mut urls_to_include: Vec<&str> = vec![]; - let urls_replaced = original_lines + let urls_replaced = colorized_lines .into_iter() .map(|line| { let mut result: String = line.to_owned(); diff --git a/todo b/todo index cf67e6d..2855d44 100644 --- a/todo +++ b/todo @@ -9,12 +9,8 @@ follow/new follower notifications display the wrong person up/down scroll -measure length of lines with ansi sequences properly - name/handle colored by user id instead of handle -color handles in text - recursively display quoted tweets? ratio -- cgit v1.1