use ansi_term::{ANSIString, Colour, Style}; use lscolors::{Indicator, LsColors}; use std::collections::HashMap; #[allow(dead_code)] #[derive(Hash, Debug, Eq, PartialEq, Clone)] pub enum Elem { /// Node type File { exec: bool, uid: bool, }, SymLink, BrokenSymLink, Dir { uid: bool, }, Pipe, BlockDevice, CharDevice, Socket, Special, /// Permissions Read, Write, Exec, ExecSticky, NoAccess, /// Last Time Modified DayOld, HourOld, Older, /// User / Group Name User, Group, /// File Size NonFile, FileLarge, FileMedium, FileSmall, } impl Elem { pub fn has_suid(&self) -> bool { match self { Elem::Dir { uid: true } | Elem::File { uid: true, .. } => true, _ => false, } } } pub type ColoredString<'a> = ANSIString<'a>; #[allow(dead_code)] #[derive(Debug, Copy, Clone)] pub enum Theme { NoColor, Default, NoLscolors, } pub struct Colors { colors: Option>, lscolors: Option, } impl Colors { pub fn new(theme: Theme) -> Self { let colors = match theme { Theme::NoColor => None, Theme::Default => Some(Self::get_light_theme_colour_map()), Theme::NoLscolors => Some(Self::get_light_theme_colour_map()), }; let lscolors = match theme { Theme::NoColor => None, Theme::Default => Some(LsColors::from_env().unwrap_or_default()), Theme::NoLscolors => None, }; Self { colors, lscolors } } pub fn colorize<'a>(&self, input: String, elem: &Elem) -> ColoredString<'a> { self.style(elem).paint(input) } pub fn colorize_using_path<'a>( &self, input: String, path: &str, elem: &Elem, ) -> ColoredString<'a> { let style_from_path = self.style_from_path(path); match style_from_path { Some(style_from_path) => style_from_path.paint(input), None => self.colorize(input, elem), } } fn style_from_path(&self, path: &str) -> Option