use crate::format::Format;
use ratatui::style::Color;
pub fn nerd_glyph(ext: &str, fmt: Format) -> &'static str {
if fmt == Format::Directory {
return "\u{f07b}"; }
match ext {
"rs" => "\u{e7a8}", "go" => "\u{e627}", "c" | "h" => "\u{e61e}", "cpp" | "hpp" => "\u{e61d}", "swift" => "\u{e755}", "java" => "\u{e738}", "kt" => "\u{e634}", "cs" => "\u{e648}", "py" => "\u{e606}", "rb" => "\u{e739}", "php" => "\u{e73d}", "lua" => "\u{e620}", "sh" | "bash" | "zsh" => "\u{e795}", "vim" => "\u{e7c5}", "js" => "\u{e74e}", "ts" => "\u{e628}", "jsx" | "tsx" => "\u{e7ba}", "html" => "\u{e736}", "css" => "\u{e749}", "scss" => "\u{e74b}", "json" => "\u{e60b}", "yaml" | "yml" => "\u{e615}", "toml" => "\u{e615}", "xml" => "\u{e619}", "sql" => "\u{e706}", "md" | "markdown" => "\u{e73e}", "txt" => "\u{f0f6}", "lock" => "\u{f023}", "pdf" => "\u{f1c1}", "png" | "jpg" | "jpeg" | "gif" | "webp" | "bmp" | "svg" => "\u{f1c5}", "mp4" | "mov" | "mkv" | "webm" => "\u{f1c8}", "mp3" | "wav" | "flac" => "\u{f1c7}", "zip" | "tar" | "gz" | "tgz" | "7z" | "rar" => "\u{f1c6}", "docx" | "doc" => "\u{f1c2}", "xlsx" | "xls" | "csv" => "\u{f1c3}", "pptx" | "ppt" | "key" => "\u{f1c4}", "epub" => "\u{f02d}", "ipynb" => "\u{e678}", _ => fallback_glyph(fmt),
}
}
fn fallback_glyph(fmt: Format) -> &'static str {
match fmt {
Format::Directory => "\u{f07b}", Format::Markdown => "\u{e73e}", Format::Html => "\u{f13b}", Format::Text => "\u{f1c9}", Format::Sheet => "\u{f1c3}", Format::Image | Format::Svg => "\u{f1c5}", Format::Pdf => "\u{f1c1}", Format::Video => "\u{f1c8}", Format::Audio => "\u{f1c7}", Format::Docx | Format::Doc => "\u{f1c2}", Format::Pptx | Format::Keynote => "\u{f1c4}", Format::Epub => "\u{f02d}", Format::Ipynb => "\u{e678}", Format::Archive => "\u{f1c6}", Format::Binary => "\u{f471}", }
}
pub fn nerd_color(ext: &str, fmt: Format) -> Color {
match ext {
"rs" => Color::Rgb(222, 165, 132), "py" => Color::Rgb(75, 139, 209), "go" => Color::Rgb(0, 173, 216), "c" | "h" => Color::Rgb(101, 154, 210), "cpp" | "hpp" => Color::Rgb(243, 101, 140), "swift" => Color::Rgb(240, 120, 96), "java" => Color::Rgb(215, 163, 74), "kt" => Color::Rgb(169, 123, 255), "cs" => Color::Rgb(106, 168, 79), "rb" => Color::Rgb(215, 72, 65), "php" => Color::Rgb(140, 150, 205), "lua" => Color::Rgb(81, 160, 207), "sh" | "bash" | "zsh" => Color::Rgb(137, 224, 81), "vim" => Color::Rgb(108, 181, 111), "js" => Color::Rgb(240, 219, 79), "ts" => Color::Rgb(49, 120, 198), "jsx" | "tsx" => Color::Rgb(97, 218, 251), "html" => Color::Rgb(227, 101, 66), "css" => Color::Rgb(100, 140, 220), "scss" => Color::Rgb(198, 83, 140), "json" => Color::Rgb(203, 203, 90), "yaml" | "yml" | "toml" => Color::Rgb(180, 160, 120), "xml" => Color::Rgb(227, 140, 66), "sql" => Color::Rgb(240, 180, 90), _ => fmt.color(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn known_extensions_get_nonempty_distinct_glyphs() {
let langs = ["rs", "py", "js", "ts", "go", "c", "rb", "html"];
let glyphs: Vec<&str> = langs.iter().map(|e| nerd_glyph(e, Format::Text)).collect();
for (e, g) in langs.iter().zip(&glyphs) {
assert!(!g.is_empty(), "{e} glyph is empty");
assert!(g.chars().all(|c| c as u32 > 0x7f), "{e} glyph is ASCII");
}
for i in 0..glyphs.len() {
for j in (i + 1)..glyphs.len() {
assert_ne!(
glyphs[i], glyphs[j],
"{} and {} collide",
langs[i], langs[j]
);
}
}
}
#[test]
fn unknown_extension_falls_back_to_format_glyph() {
assert_eq!(
nerd_glyph("wat", Format::Text),
fallback_glyph(Format::Text)
);
assert_eq!(
nerd_glyph("nope", Format::Binary),
fallback_glyph(Format::Binary)
);
assert_eq!(nerd_glyph("", Format::Binary), "\u{f471}");
}
#[test]
fn directory_always_gets_the_folder_glyph() {
assert_eq!(nerd_glyph("", Format::Directory), "\u{f07b}");
assert_eq!(nerd_glyph("rs", Format::Directory), "\u{f07b}");
}
#[test]
fn known_extensions_get_brand_colours() {
assert_eq!(nerd_color("rs", Format::Text), Color::Rgb(222, 165, 132));
assert_eq!(nerd_color("go", Format::Text), Color::Rgb(0, 173, 216));
assert_eq!(nerd_color("ts", Format::Text), Color::Rgb(49, 120, 198));
assert_eq!(nerd_color("html", Format::Text), Color::Rgb(227, 101, 66));
assert_ne!(
nerd_color("rs", Format::Text),
nerd_color("py", Format::Text)
);
}
#[test]
fn unknown_extension_falls_back_to_format_colour() {
assert_eq!(nerd_color("wat", Format::Binary), Format::Binary.color());
assert_eq!(nerd_color("", Format::Image), Format::Image.color());
assert_eq!(nerd_color("", Format::Directory), Format::Directory.color());
}
}