Allow color and width customization (#386)

Fixes #202 and allows a workaround for #249.
This commit is contained in:
Denis Isidoro 2020-08-28 08:57:03 -03:00 committed by GitHub
parent e49ff1a18d
commit abae4e022f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<u8> {
if let Ok(x) = env::var(varname) {
x.parse::<u8>().ok()
} else {
None
}
}
fn parse_env_var_u16(varname: &str) -> Option<u16> {
if let Ok(x) = env::var(varname) {
x.parse::<u16>().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,