// Copyright (c) 2017 fd developers // Licensed under the Apache License, Version 2.0 // // or the MIT license , // at your option. All files in the project carrying such // notice may not be copied, modified, or distributed except // according to those terms. /// A parser for the `LS_COLORS` environment variable. use std::collections::HashMap; use ansi_term::{Colour, Style}; /// Maps file extensions to ANSI colors / styles. pub type ExtensionStyles = HashMap; /// Maps filenames to ANSI colors / styles. pub type FilenameStyles = HashMap; const LS_CODES: &'static [&'static str] = &[ "no", "no", "fi", "rs", "di", "ln", "ln", "ln", "or", "mi", "pi", "pi", "so", "bd", "bd", "cd", "cd", "do", "ex", "lc", "lc", "rc", "rc", "ec", "ec", "su", "su", "sg", "sg", "st", "ow", "ow", "tw", "tw", "ca", "mh", "cl", ]; /// Defines how different file system entries should be colorized / styled. #[derive(Debug, PartialEq)] pub struct LsColors { /// ANSI Style for directories. pub directory: Style, /// ANSI style for symbolic links. pub symlink: Style, /// ANSI style for executable files. pub executable: Style, /// A map that defines ANSI styles for different file extensions. pub extensions: ExtensionStyles, /// A map that defines ANSI styles for different specific filenames. pub filenames: FilenameStyles, } impl Default for LsColors { /// Get a default LsColors structure. fn default() -> LsColors { LsColors { directory: Colour::Blue.bold(), symlink: Colour::Cyan.normal(), executable: Colour::Red.bold(), extensions: HashMap::new(), filenames: HashMap::new(), } } } impl LsColors { /// Parse a single text-decoration code (normal, bold, italic, ...). fn parse_decoration(code: &str) -> Option Style> { match code { "0" | "00" => Some(Colour::normal), "1" | "01" => Some(Colour::bold), "3" | "03" => Some(Colour::italic), "4" | "04" => Some(Colour::underline), _ => None, } } /// Parse ANSI escape sequences like `38;5;10;1`. fn parse_style(code: &str) -> Option