#![deny(unsafe_code)]
use crate::term::Style;
pub const ACCENT: u8 = 45;
pub const LINK: u8 = 39;
pub const LINK_EXTERNAL: u8 = 141;
pub const H1_FG: u8 = ACCENT;
pub const H2_FG: u8 = 231;
pub const H3_FG: u8 = 51;
pub const H4_FG: u8 = 44;
pub const H5_FG: u8 = 37;
pub const H6_FG: u8 = 245;
pub const RULE_FG: u8 = 240;
pub const CODE_FG: u8 = 252;
pub const CODE_BG: u8 = 236;
pub const CODE_SPAN_FG: u8 = 216;
pub const CODE_SPAN_BG: u8 = 236;
pub const QUOTE_BAR_FG: u8 = 244;
pub const QUOTE_FG: u8 = 250;
pub const TABLE_BORDER_FG: u8 = 240;
pub const TABLE_HEAD_FG: u8 = 231;
pub const MUTED_FG: u8 = 245;
pub const SYNTAX_KEYWORD: u8 = 176;
pub const SYNTAX_STRING: u8 = 114;
pub const SYNTAX_NUMBER: u8 = 216;
pub const SEARCH_BG: u8 = 226;
pub const SEARCH_FG: u8 = 16;
pub const SEARCH_CURRENT_BG: u8 = 208;
pub const SELECTION_BG: u8 = 238;
pub const STATUS_BG: u8 = 238;
pub const STATUS_FG: u8 = 252;
pub const GUTTER_FG: u8 = 244;
pub const TASK_DONE_FG: u8 = 114;
pub const MORE_FG: u8 = 214;
pub const COLUMN_BG: u8 = 237;
pub const STATUS_LIVE: u8 = 114;
pub const STATUS_OPEN: u8 = 214;
pub const STATUS_BLOCKED: u8 = 203;
pub const fn text() -> Style {
Style::new()
}
pub const fn muted() -> Style {
Style::new().fg(MUTED_FG)
}
pub const fn rule() -> Style {
Style::new().fg(RULE_FG)
}
pub const fn gutter() -> Style {
Style::new().fg(GUTTER_FG)
}
pub const fn banner_style() -> Style {
Style::new().fg(H1_FG).bold()
}
pub const fn code() -> Style {
Style::new().fg(CODE_FG).bg(CODE_BG)
}
pub const fn code_label() -> Style {
Style::new().fg(MUTED_FG).bg(CODE_BG).italic()
}
pub const fn quote_bar() -> Style {
Style::new().fg(QUOTE_BAR_FG).dim()
}
pub const fn table_border() -> Style {
Style::new().fg(TABLE_BORDER_FG)
}
pub fn status_of(value: &str) -> Style {
let first = value
.split(|c: char| c.is_whitespace() || c == '|' || c == '\u{2014}')
.find(|w| !w.is_empty())
.unwrap_or("")
.to_ascii_lowercase();
match first.as_str() {
"in" | "partially" => Style::new().fg(STATUS_OPEN).bold(),
w => status_word(w).unwrap_or_default(),
}
}
pub fn tag_style(keyword: &str) -> Option<Style> {
status_word(&keyword.to_ascii_lowercase())
}
fn status_word(w: &str) -> Option<Style> {
match w {
"active" | "accepted" | "implemented" | "done" => Some(Style::new().fg(STATUS_LIVE).bold()),
"draft" | "proposed" | "executing" | "wip" => Some(Style::new().fg(STATUS_OPEN).bold()),
"blocked" => Some(Style::new().fg(STATUS_BLOCKED).bold()),
"superseded" | "archived" | "cancelled" | "rejected" => {
Some(Style::new().fg(MUTED_FG).dim())
}
_ => None,
}
}
pub const fn link_fg(external: bool) -> u8 {
match external {
true => LINK_EXTERNAL,
false => LINK,
}
}
pub const fn more() -> Style {
Style::new().fg(MORE_FG).bold()
}
pub const fn table_head() -> Style {
Style::new().fg(TABLE_HEAD_FG).bold()
}
pub const fn bullet_style() -> Style {
Style::new().fg(ACCENT)
}
pub const fn task_done() -> Style {
Style::new().fg(TASK_DONE_FG)
}
pub const fn search() -> Style {
Style::new().fg(SEARCH_FG).bg(SEARCH_BG)
}
pub const fn search_current() -> Style {
Style::new().fg(SEARCH_FG).bg(SEARCH_CURRENT_BG)
}
pub const fn selection() -> Style {
Style::new().bg(SELECTION_BG)
}
pub const fn status() -> Style {
Style::new().fg(STATUS_FG).bg(STATUS_BG)
}
pub const fn heading(level: u8) -> Style {
match level {
1 => Style::new().fg(H1_FG).bold(),
2 => Style::new().fg(H2_FG).bold(),
3 => Style::new().fg(H3_FG).bold(),
4 => Style::new().fg(H4_FG),
5 => Style::new().fg(H5_FG).dim(),
_ => Style::new().fg(H6_FG).dim().italic(),
}
}
pub const fn heading_indent(level: u8) -> usize {
match level {
1 | 2 => 0,
3 => 2,
4 => 4,
5 => 6,
_ => 8,
}
}
pub const MARKER_OPEN: char = '\u{25be}'; pub const MARKER_CLOSED: char = '\u{25b8}';
pub const JSON_KEY_FG: u8 = 81;
pub const JSON_STR_FG: u8 = 150;
pub const JSON_NUM_FG: u8 = 215;
pub const JSON_BOOL_FG: u8 = 141;
pub const JSON_NULL_FG: u8 = MUTED_FG;
pub const JSON_PUNCT_FG: u8 = RULE_FG;
pub const ERROR_FG: u8 = 203;
pub const fn json_key() -> Style {
Style::new().fg(JSON_KEY_FG)
}
pub const fn json_string() -> Style {
Style::new().fg(JSON_STR_FG)
}
pub const fn json_number() -> Style {
Style::new().fg(JSON_NUM_FG)
}
pub const fn json_bool() -> Style {
Style::new().fg(JSON_BOOL_FG).bold()
}
pub const fn json_null() -> Style {
Style::new().fg(JSON_NULL_FG).italic()
}
pub const fn json_punct() -> Style {
Style::new().fg(JSON_PUNCT_FG)
}
pub const fn json_marker() -> Style {
Style::new().fg(GUTTER_FG)
}
pub const fn error() -> Style {
Style::new().fg(ERROR_FG)
}
pub const fn lens_user() -> Style {
Style::new().fg(JSON_KEY_FG).bold()
}
pub const fn lens_assistant() -> Style {
Style::new().fg(STATUS_LIVE).bold()
}
pub const fn lens_tool() -> Style {
Style::new().fg(JSON_NUM_FG)
}
pub const fn lens_system() -> Style {
Style::new().fg(MUTED_FG)
}
pub const fn lens_time() -> Style {
Style::new().fg(GUTTER_FG)
}
pub const fn lens_group() -> Style {
Style::new().fg(MUTED_FG).italic()
}
pub const fn lens_body() -> Style {
Style::new()
}
pub const fn lens_more() -> Style {
Style::new().fg(MUTED_FG).italic()
}
pub const MARKER_MORE: char = '+';
pub const fn bullet(depth: usize) -> char {
match depth % 3 {
0 => '\u{2022}', 1 => '\u{25e6}', _ => '\u{25aa}', }
}
pub const fn checkbox(checked: bool) -> char {
if checked {
'\u{2611}' } else {
'\u{2610}' }
}
pub const BANNER_ROWS: usize = 5;
const GLYPH_W: usize = 3;
const GLYPH_GAP: usize = 1;
#[rustfmt::skip]
const GLYPHS: &[(char, [&str; BANNER_ROWS])] = &[
(' ', ["...", "...", "...", "...", "..."]),
('A', [".#.", "#.#", "###", "#.#", "#.#"]),
('B', ["##.", "#.#", "##.", "#.#", "##."]),
('C', [".##", "#..", "#..", "#..", ".##"]),
('D', ["##.", "#.#", "#.#", "#.#", "##."]),
('E', ["###", "#..", "##.", "#..", "###"]),
('F', ["###", "#..", "##.", "#..", "#.."]),
('G', [".##", "#..", "#.#", "#.#", ".##"]),
('H', ["#.#", "#.#", "###", "#.#", "#.#"]),
('I', ["###", ".#.", ".#.", ".#.", "###"]),
('J', ["..#", "..#", "..#", "#.#", ".#."]),
('K', ["#.#", "##.", "#..", "##.", "#.#"]),
('L', ["#..", "#..", "#..", "#..", "###"]),
('M', ["#.#", "###", "###", "#.#", "#.#"]),
('N', ["#.#", "###", "###", "###", "#.#"]),
('O', ["###", "#.#", "#.#", "#.#", "###"]),
('P', ["##.", "#.#", "##.", "#..", "#.."]),
('Q', ["###", "#.#", "#.#", "##.", ".##"]),
('R', ["##.", "#.#", "##.", "#.#", "#.#"]),
('S', [".##", "#..", ".#.", "..#", "##."]),
('T', ["###", ".#.", ".#.", ".#.", ".#."]),
('U', ["#.#", "#.#", "#.#", "#.#", "###"]),
('V', ["#.#", "#.#", "#.#", "#.#", ".#."]),
('W', ["#.#", "#.#", "###", "###", "#.#"]),
('X', ["#.#", "#.#", ".#.", "#.#", "#.#"]),
('Y', ["#.#", "#.#", ".#.", ".#.", ".#."]),
('Z', ["###", "..#", ".#.", "#..", "###"]),
('0', ["###", "#.#", "#.#", "#.#", "###"]),
('1', [".#.", "##.", ".#.", ".#.", "###"]),
('2', ["##.", "..#", ".#.", "#..", "###"]),
('3', ["###", "..#", ".##", "..#", "###"]),
('4', ["#.#", "#.#", "###", "..#", "..#"]),
('5', ["###", "#..", "##.", "..#", "##."]),
('6', [".##", "#..", "###", "#.#", "###"]),
('7', ["###", "..#", "..#", ".#.", ".#."]),
('8', ["###", "#.#", "###", "#.#", "###"]),
('9', ["###", "#.#", "###", "..#", "##."]),
('-', ["...", "...", "###", "...", "..."]),
('.', ["...", "...", "...", "...", ".#."]),
(',', ["...", "...", "...", ".#.", "#.."]),
(':', ["...", ".#.", "...", ".#.", "..."]),
('!', [".#.", ".#.", ".#.", "...", ".#."]),
('?', ["##.", "..#", ".#.", "...", ".#."]),
('\'', [".#.", ".#.", "...", "...", "..."]),
('/', ["..#", "..#", ".#.", "#..", "#.."]),
('(', ["..#", ".#.", ".#.", ".#.", "..#"]),
(')', ["#..", ".#.", ".#.", ".#.", "#.."]),
];
const INK: char = '\u{2588}';
fn glyph(c: char) -> Option<&'static [&'static str; BANNER_ROWS]> {
let up = c.to_uppercase().next().unwrap_or(c);
GLYPHS.iter().find(|(g, _)| *g == up).map(|(_, rows)| rows)
}
pub fn banner_width(text: &str) -> Option<usize> {
let n = text.chars().count();
if n == 0 {
return None;
}
for c in text.chars() {
glyph(c)?;
}
Some(n * GLYPH_W + (n - 1) * GLYPH_GAP)
}
pub fn banner(text: &str, max_width: usize) -> Option<Vec<String>> {
let trimmed = text.trim();
let width = banner_width(trimmed)?;
if width > max_width {
return None;
}
let glyphs: Vec<&[&str; BANNER_ROWS]> = trimmed.chars().filter_map(glyph).collect();
let mut out = Vec::with_capacity(BANNER_ROWS);
for row in 0..BANNER_ROWS {
let mut line = String::with_capacity(width);
for (i, g) in glyphs.iter().enumerate() {
if i > 0 {
line.push(' ');
}
for cell in g[row].chars() {
line.push(if cell == '#' { INK } else { ' ' });
}
}
out.push(line);
}
Some(out)
}
#[cfg(test)]
#[path = "theme_tests.rs"]
mod tests;