From abae4e022f39cf779237bf99b74e7651e31315fd Mon Sep 17 00:00:00 2001 From: Denis Isidoro Date: Fri, 28 Aug 2020 08:57:03 -0300 Subject: [PATCH] Allow color and width customization (#386) Fixes #202 and allows a workaround for #249. --- src/display/terminal.rs | 47 +++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/src/display/terminal.rs b/src/display/terminal.rs index e97092f..6fcc09e 100644 --- a/src/display/terminal.rs +++ b/src/display/terminal.rs @@ -2,11 +2,36 @@ use crate::common::terminal_width; use crate::display; use crate::structures::item::Item; use std::cmp::max; +use std::env; use termion::color; -const COMMENT_COLOR: color::LightCyan = color::LightCyan; -const TAG_COLOR: color::Blue = color::Blue; -const SNIPPET_COLOR: color::White = color::White; +fn parse_env_var_u8(varname: &str) -> Option { + if let Ok(x) = env::var(varname) { + x.parse::().ok() + } else { + None + } +} + +fn parse_env_var_u16(varname: &str) -> Option { + if let Ok(x) = env::var(varname) { + x.parse::().ok() + } else { + None + } +} + +lazy_static! { + pub static ref TAG_COLOR: color::AnsiValue = + color::AnsiValue(parse_env_var_u8("NAVI_TAG_COLOR").unwrap_or(14)); + pub static ref COMMENT_COLOR: color::AnsiValue = + color::AnsiValue(parse_env_var_u8("NAVI_COMMENT_COLOR").unwrap_or(4)); + pub static ref SNIPPET_COLOR: color::AnsiValue = + color::AnsiValue(parse_env_var_u8("NAVI_SNIPPET_COLOR").unwrap_or(7)); + pub static ref TAG_WIDTH_PERCENTAGE: u16 = parse_env_var_u16("NAVI_TAG_WIDTH").unwrap_or(20); + pub static ref COMMENT_WIDTH_PERCENTAGE: u16 = + parse_env_var_u16("NAVI_COMMENT_WIDTH").unwrap_or(40); +} pub fn variable_prompt(varname: &str) -> String { format!("{}: ", varname) @@ -18,9 +43,9 @@ pub fn preview(comment: &str, tags: &str, snippet: &str) { comment = format!("# {}", comment), tags = format!("[{}]", tags), snippet = display::fix_newlines(snippet), - comment_color = color::Fg(COMMENT_COLOR), - tag_color = color::Fg(TAG_COLOR), - snippet_color = color::Fg(SNIPPET_COLOR), + comment_color = color::Fg(*COMMENT_COLOR), + tag_color = color::Fg(*TAG_COLOR), + snippet_color = color::Fg(*SNIPPET_COLOR), ); } @@ -34,8 +59,8 @@ fn limit_str(text: &str, length: usize) -> String { fn get_widths() -> (usize, usize) { let width = terminal_width::get(); - let tag_width = max(4, width * 20 / 100); - let comment_width = max(4, width * 40 / 100); + let tag_width = max(4, width * *TAG_WIDTH_PERCENTAGE / 100); + let comment_width = max(4, width * *COMMENT_WIDTH_PERCENTAGE / 100); (usize::from(tag_width), usize::from(comment_width)) } @@ -61,9 +86,9 @@ impl display::Writer for Writer { tags_short = limit_str(item.tags, self.tag_width), comment_short = limit_str(item.comment, self.comment_width), snippet_short = display::fix_newlines(item.snippet), - comment_color = color::Fg(COMMENT_COLOR), - tag_color = color::Fg(TAG_COLOR), - snippet_color = color::Fg(SNIPPET_COLOR), + comment_color = color::Fg(*COMMENT_COLOR), + tag_color = color::Fg(*TAG_COLOR), + snippet_color = color::Fg(*SNIPPET_COLOR), tags = item.tags, comment = item.comment, delimiter = display::DELIMITER,