Code clean-ups, documentation
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by sharkdp · parent 0f5ede4 · Browse files · 2 files·+58 added−25 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+58 added−25 removed
- other:+58 added−25 removed
Provenance
- Commit
bf013da- Tree
c398c81- Author
sharkdp <davidpeter@web.de>- Committer
sharkdp <davidpeter@web.de>- Parent
0f5ede4
2 files changed+58 added−25 removed
modified:src/lscolors/mod.rs+12 added-2 removed
1-use std::collections::HashMap;1+/// A parser for the `LS_COLORS` environment variable.2 3+use std::collections::HashMap;4 use ansi_term::{Style, Colour};5 6 /// Maps file extensions to ANSI colors / styles.7 pub type ExtensionStyles = HashMap<String, Style>;6 unmodified lines14 "so", "bd", "bd", "cd", "cd", "do", "ex", "lc", "lc", "rc", "rc", "ec",15 "ec", "su", "su", "sg", "sg", "st", "ow", "ow", "tw", "tw", "ca", "mh",16 "cl"];17 18+/// Defines how different file system entries should be colorized / styled.19 #[derive(Debug, PartialEq)]20 pub struct LsColors {21+ /// ANSI Style for directories.22 pub directory: Style,23+24+ /// ANSI style for symbolic links.25 pub symlink: Style,26+27+ /// A map that defines ANSI styles for different file extensions.28 pub extensions: ExtensionStyles,29+30+ /// A map that defines ANSI styles for different specific filenames.31 pub filenames: FilenameStyles,32 }33 34 impl Default for LsColors {8 unmodified lines43 }44 }45 46 impl LsColors {47+ /// Parse a single text-decoration code (normal, bold, italic, ...).48 fn parse_decoration(code: &str) -> Option<fn(Colour) -> Style> {49 match code {50 "0" | "00" => Some(Colour::normal),51 "1" | "01" => Some(Colour::bold),54 unmodified lines106 None107 }108 }109 100- /// Add a new LS_COLORS entry110+ /// Add a new `LS_COLORS` entry.111 fn add_entry(&mut self, input: &str) {112 let mut parts = input.trim().split('=');113 if let Some(pattern) = parts.next() {114 if let Some(style_code) = parts.next() {100 unmodified linesmodified:src/main.rs+46 added-23 removed
20 unmodified lines21 use lscolors::LsColors;22 23 /// Configuration options for *fd*.24 struct FdOptions {25+ /// Determines whether the regex search is case-sensitive or case-insensitive.26 case_sensitive: bool,27+28+ /// Whether to search within the full file path or just the base name (filename or directory29+ /// name).30 search_full_path: bool,31+32+ /// Whether to ignore hidden files and directories (or not).33 ignore_hidden: bool,34+35+ /// Whether to respect VCS ignore files (`.gitignore`, `.ignore`, ..) or not.36 read_ignore: bool,37+38+ /// Whether to follow symlinks or not.39 follow_links: bool,40+41+ /// The maximum search depth, or `None` if no maximum search depth should be set.42+ ///43+ /// A depth of `1` includes all files under the current directory, a depth of `2` also includes44+ /// all files under subdirectories of the current directory, etc.45 max_depth: Option<usize>,31- colored: bool,32- ls_colors: LsColors46+47+ /// `None` if the output should not be colorized. Otherwise, a `LsColors` instance that defines48+ /// how to style different filetypes.49+ ls_colors: Option<LsColors>50 }51 52 /// Print a search result to the console.53 fn print_entry(path_root: &Path, path_entry: &Path, config: &FdOptions) {3 unmodified lines57 Some(p) => p,58 None => return59 };60 44- if !config.colored {45- println!("{}", path_str);46- } else {61+ if let Some(ref ls_colors) = config.ls_colors {62 let mut component_path = path_root.to_path_buf();63 64 // Traverse the path and colorize each component65 for component in path_entry.components() {7 unmodified lines73 let style =74 if component_path.symlink_metadata()75 .map(|md| md.file_type().is_symlink())76 .unwrap_or(false) {62- config.ls_colors.symlink77+ ls_colors.symlink78 } else if component_path.is_dir() {64- config.ls_colors.directory79+ ls_colors.directory80 } else {81 // Look up file name82 let o_style =83 component_path.file_name()84 .and_then(|n| n.to_str())70- .and_then(|n| config.ls_colors.filenames.get(n));85+ .and_then(|n| ls_colors.filenames.get(n));86 87 match o_style {88 Some(s) => *s,89 None =>90 // Look up file extension91 component_path.extension()92 .and_then(|e| e.to_str())78- .and_then(|e| config.ls_colors.extensions.get(e))93+ .and_then(|e| ls_colors.extensions.get(e))94 .cloned()95 .unwrap_or_default()96 }97 };5 unmodified lines103 print!("{}", style.paint(sep));104 }105 }106 println!();107+ } else {108+ // Uncolored output:109+ println!("{}", path_str);110 }111 }112 95-/// Recursively scan the given root path and search for files / pathnames96-/// matching the pattern.113+/// Recursively scan the given root path and search for files / pathnames matching the pattern.114 fn scan(root: &Path, pattern: &Regex, config: &FdOptions) {115 let walker = WalkBuilder::new(root)116 .hidden(config.ignore_hidden)117 .ignore(config.read_ignore)74 unmodified lines192 Err(_) => error("Could not get current directory!")193 };194 let current_dir = current_dir_buf.as_path();195 196+ // The search will be case-sensitive if the command line flag is set or197+ // if the pattern has an uppercase character (smart case).198+ let case_sensitive = matches.opt_present("sensitive") ||199+ pattern.chars().any(char::is_uppercase);200+201 let colored_output = !matches.opt_present("no-color") &&202 stdout_isatty();203 204 let ls_colors =205+ if colored_output {206+ Some(183- env::var("LS_COLORS")184- .ok()185- .map(|val| LsColors::from_string(&val))186- .unwrap_or_default();207+ env::var("LS_COLORS")208+ .ok()209+ .map(|val| LsColors::from_string(&val))210+ .unwrap_or_default()211+ )212+ } else {213+ None214+ };215 216 let config = FdOptions {189- // The search will be case-sensitive if the command line flag is set or190- // if the pattern has an uppercase character (smart case).191- case_sensitive: matches.opt_present("sensitive") ||192- pattern.chars().any(char::is_uppercase),217+ case_sensitive: case_sensitive,218 search_full_path: matches.opt_present("full-path"),219 ignore_hidden: !matches.opt_present("hidden"),220 read_ignore: !matches.opt_present("no-ignore"),221 follow_links: matches.opt_present("follow"),197- max_depth:198- matches.opt_str("max-depth")199- .and_then(|ds| usize::from_str_radix(&ds, 10).ok()),222+ max_depth: matches.opt_str("max-depth")223+ .and_then(|ds| usize::from_str_radix(&ds, 10).ok()),200- colored: colored_output,224 ls_colors: ls_colors225 };226 227 match RegexBuilder::new(pattern)6 unmodified lines