navi/src/display.rs
2020-03-24 10:53:45 -03:00

79 lines
2.4 KiB
Rust
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use crate::terminal;
use regex::Regex;
use std::cmp::max;
use termion::color;
const COMMENT_COLOR: color::LightCyan = color::LightCyan;
const TAG_COLOR: color::Blue = color::Blue;
const SNIPPET_COLOR: color::White = color::White;
const NEWLINE_ESCAPE_CHAR: char = '\x15';
pub const LINE_SEPARATOR: &str = " \x15 ";
pub const DELIMITER: &str = r" ";
lazy_static! {
pub static ref WIDTHS: (usize, usize) = get_widths();
pub static ref NEWLINE_REGEX: Regex = Regex::new(r"\\\s+").expect("Invalid regex");
}
fn get_widths() -> (usize, usize) {
let width = terminal::width();
let tag_width = max(4, width * 20 / 100);
let comment_width = max(4, width * 40 / 100);
(usize::from(tag_width), usize::from(comment_width))
}
pub fn variable_prompt(varname: &str) -> String {
format!("{}: ", varname)
}
fn fix_newlines(txt: &str) -> String {
if txt.contains(NEWLINE_ESCAPE_CHAR) {
(*NEWLINE_REGEX)
.replace_all(txt.replace(LINE_SEPARATOR, " ").as_str(), "")
.to_string()
} else {
txt.to_string()
}
}
pub fn preview(comment: &str, tags: &str, snippet: &str) {
println!(
"{comment_color}{comment} {tag_color}{tags} \n{snippet_color}{snippet}",
comment = format!("# {}", comment),
tags = format!("[{}]", tags),
snippet = fix_newlines(snippet),
comment_color = color::Fg(COMMENT_COLOR),
tag_color = color::Fg(TAG_COLOR),
snippet_color = color::Fg(SNIPPET_COLOR),
);
}
fn limit_str(text: &str, length: usize) -> String {
if text.len() > length {
format!("{}", text.chars().take(length).collect::<String>())
} else {
format!("{:width$}", text, width = length)
}
}
pub fn format_line(
tags: &str,
comment: &str,
snippet: &str,
tag_width: usize,
comment_width: usize,
) -> String {
format!(
"{tag_color}{tags_short}{delimiter}{comment_color}{comment_short}{delimiter}{snippet_color}{snippet_short}{delimiter}{tags}{delimiter}{comment}{delimiter}{snippet}{delimiter}\n",
tags_short = limit_str(tags, tag_width),
comment_short = limit_str(comment, comment_width),
snippet_short = fix_newlines(snippet),
comment_color = color::Fg(COMMENT_COLOR),
tag_color = color::Fg(TAG_COLOR),
snippet_color = color::Fg(SNIPPET_COLOR),
tags = tags,
comment = comment,
delimiter = DELIMITER,
snippet = &snippet)
}