Refactor: Rename `Options` to `Config`
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Niklas Mohrin · committed by David Peter · parent b8c575c · Browse files · 5 files·+129 added−129 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+129 added−129 removed
- other:+129 added−129 removed
Provenance
- Commit
02e9850- Tree
c48c4af- Author
Niklas Mohrin <dev@niklasmohrin.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
b8c575c
5 files changed+129 added−129 removed
added:src/config.rs+112 added
1+use std::{path::PathBuf, sync::Arc, time::Duration};2+3+use lscolors::LsColors;4+use regex::bytes::RegexSet;5+6+use crate::exec::CommandTemplate;7+use crate::filetypes::FileTypes;8+#[cfg(unix)]9+use crate::filter::OwnerFilter;10+use crate::filter::{SizeFilter, TimeFilter};11+12+/// Configuration options for *fd*.13+pub struct Config {14+ /// Whether the search is case-sensitive or case-insensitive.15+ pub case_sensitive: bool,16+17+ /// Whether to search within the full file path or just the base name (filename or directory18+ /// name).19+ pub search_full_path: bool,20+21+ /// Whether to ignore hidden files and directories (or not).22+ pub ignore_hidden: bool,23+24+ /// Whether to respect `.fdignore` files or not.25+ pub read_fdignore: bool,26+27+ /// Whether to respect ignore files in parent directories or not.28+ pub read_parent_ignore: bool,29+30+ /// Whether to respect VCS ignore files (`.gitignore`, ..) or not.31+ pub read_vcsignore: bool,32+33+ /// Whether to respect the global ignore file or not.34+ pub read_global_ignore: bool,35+36+ /// Whether to follow symlinks or not.37+ pub follow_links: bool,38+39+ /// Whether to limit the search to starting file system or not.40+ pub one_file_system: bool,41+42+ /// Whether elements of output should be separated by a null character43+ pub null_separator: bool,44+45+ /// The maximum search depth, or `None` if no maximum search depth should be set.46+ ///47+ /// A depth of `1` includes all files under the current directory, a depth of `2` also includes48+ /// all files under subdirectories of the current directory, etc.49+ pub max_depth: Option<usize>,50+51+ /// The minimum depth for reported entries, or `None`.52+ pub min_depth: Option<usize>,53+54+ /// Whether to stop traversing into matching directories.55+ pub prune: bool,56+57+ /// The number of threads to use.58+ pub threads: usize,59+60+ /// If true, the program doesn't print anything and will instead return an exit code of 061+ /// if there's at least one match. Otherwise, the exit code will be 1.62+ pub quiet: bool,63+64+ /// Time to buffer results internally before streaming to the console. This is useful to65+ /// provide a sorted output, in case the total execution time is shorter than66+ /// `max_buffer_time`.67+ pub max_buffer_time: Option<Duration>,68+69+ /// `None` if the output should not be colorized. Otherwise, a `LsColors` instance that defines70+ /// how to style different filetypes.71+ pub ls_colors: Option<LsColors>,72+73+ /// Whether or not we are writing to an interactive terminal74+ pub interactive_terminal: bool,75+76+ /// The type of file to search for. If set to `None`, all file types are displayed. If77+ /// set to `Some(..)`, only the types that are specified are shown.78+ pub file_types: Option<FileTypes>,79+80+ /// The extension to search for. Only entries matching the extension will be included.81+ ///82+ /// The value (if present) will be a lowercase string without leading dots.83+ pub extensions: Option<RegexSet>,84+85+ /// If a value is supplied, each item found will be used to generate and execute commands.86+ pub command: Option<Arc<CommandTemplate>>,87+88+ /// A list of glob patterns that should be excluded from the search.89+ pub exclude_patterns: Vec<String>,90+91+ /// A list of custom ignore files.92+ pub ignore_files: Vec<PathBuf>,93+94+ /// The given constraints on the size of returned files95+ pub size_constraints: Vec<SizeFilter>,96+97+ /// Constraints on last modification time of files98+ pub time_constraints: Vec<TimeFilter>,99+100+ #[cfg(unix)]101+ /// User/group ownership constraint102+ pub owner_constraint: Option<OwnerFilter>,103+104+ /// Whether or not to display filesystem errors105+ pub show_filesystem_errors: bool,106+107+ /// The separator used to print file paths.108+ pub path_separator: Option<String>,109+110+ /// The maximum number of search results111+ pub max_results: Option<usize>,112+}modified:src/main.rs+7 added-7 removed
1 mod app;2+mod config;3 mod error;4 mod exec;5 mod exit_codes;6 mod filesystem;7 mod filetypes;8 mod filter;8-mod options;9 mod output;10 mod regex_helper;11 mod walk;12 9 unmodified lines22 use lscolors::LsColors;23 use normpath::PathExt;24 use regex::bytes::{RegexBuilder, RegexSetBuilder};25 26+use crate::config::Config;27 use crate::error::print_error;28 use crate::exec::CommandTemplate;29 use crate::exit_codes::ExitCode;30 use crate::filetypes::FileTypes;31 #[cfg(unix)]32 use crate::filter::OwnerFilter;33 use crate::filter::{SizeFilter, TimeFilter};33-use crate::options::Options;34 use crate::regex_helper::{pattern_has_uppercase_char, pattern_matches_strings_with_leading_dot};35 36 // We use jemalloc for performance reasons, see https://github.com/sharkdp/fd/pull/48137 // FIXME: re-enable jemalloc on macOS, see comment in Cargo.toml file for more infos36 unmodified lines74 let pattern = extract_search_pattern(&matches)?;75 ensure_search_pattern_is_not_a_path(&matches, pattern)?;76 let pattern_regex = build_pattern_regex(&matches, pattern)?;77 78- let config = construct_options(matches, &pattern_regex)?;78+ let config = construct_config(matches, &pattern_regex)?;79 ensure_use_hidden_option_for_leading_dot_pattern(&config, &pattern_regex)?;80 let re = build_regex(pattern_regex, &config)?;81 walk::scan(&search_paths, Arc::new(re), Arc::new(config))82 }126 unmodified lines209 _ => Ok(()),210 }211 }212 213-fn construct_options(matches: clap::ArgMatches, pattern_regex: &str) -> Result<Options> {213+fn construct_config(matches: clap::ArgMatches, pattern_regex: &str) -> Result<Config> {214 // The search will be case-sensitive if the command line flag is set or215 // if the pattern has an uppercase character (smart case).216 let case_sensitive = !matches.is_present("ignore-case")217 && (matches.is_present("case-sensitive") || pattern_has_uppercase_char(pattern_regex));31 unmodified lines249 None250 };251 let command = extract_command(&matches, path_separator.as_deref(), colored_output)?;252 253- Ok(Options {253+ Ok(Config {254 case_sensitive,255 search_full_path: matches.is_present("full-path"),256 ignore_hidden: !(matches.is_present("hidden")257 || matches.occurrences_of("rg-alias-hidden-ignore") >= 2),270 unmodified lines528 Ok(time_constraints)529 }530 531 fn ensure_use_hidden_option_for_leading_dot_pattern(532- config: &Options,532+ config: &Config,533 pattern_regex: &str,534 ) -> Result<()> {535 if cfg!(unix) && config.ignore_hidden && pattern_matches_strings_with_leading_dot(pattern_regex)536 {6 unmodified lines543 Ok(())544 }545 }546 547-fn build_regex(pattern_regex: String, config: &Options) -> Result<regex::bytes::Regex> {547+fn build_regex(pattern_regex: String, config: &Config) -> Result<regex::bytes::Regex> {548 RegexBuilder::new(&pattern_regex)549 .case_insensitive(!config.case_sensitive)550 .dot_matches_new_line(true)551 .build()9 unmodified linesdeleted:src/options.rs-112 removed
1-use std::{path::PathBuf, sync::Arc, time::Duration};2-3-use lscolors::LsColors;4-use regex::bytes::RegexSet;5-6-use crate::exec::CommandTemplate;7-use crate::filetypes::FileTypes;8-#[cfg(unix)]9-use crate::filter::OwnerFilter;10-use crate::filter::{SizeFilter, TimeFilter};11-12-/// Configuration options for *fd*.13-pub struct Options {14- /// Whether the search is case-sensitive or case-insensitive.15- pub case_sensitive: bool,16-17- /// Whether to search within the full file path or just the base name (filename or directory18- /// name).19- pub search_full_path: bool,20-21- /// Whether to ignore hidden files and directories (or not).22- pub ignore_hidden: bool,23-24- /// Whether to respect `.fdignore` files or not.25- pub read_fdignore: bool,26-27- /// Whether to respect ignore files in parent directories or not.28- pub read_parent_ignore: bool,29-30- /// Whether to respect VCS ignore files (`.gitignore`, ..) or not.31- pub read_vcsignore: bool,32-33- /// Whether to respect the global ignore file or not.34- pub read_global_ignore: bool,35-36- /// Whether to follow symlinks or not.37- pub follow_links: bool,38-39- /// Whether to limit the search to starting file system or not.40- pub one_file_system: bool,41-42- /// Whether elements of output should be separated by a null character43- pub null_separator: bool,44-45- /// The maximum search depth, or `None` if no maximum search depth should be set.46- ///47- /// A depth of `1` includes all files under the current directory, a depth of `2` also includes48- /// all files under subdirectories of the current directory, etc.49- pub max_depth: Option<usize>,50-51- /// The minimum depth for reported entries, or `None`.52- pub min_depth: Option<usize>,53-54- /// Whether to stop traversing into matching directories.55- pub prune: bool,56-57- /// The number of threads to use.58- pub threads: usize,59-60- /// If true, the program doesn't print anything and will instead return an exit code of 061- /// if there's at least one match. Otherwise, the exit code will be 1.62- pub quiet: bool,63-64- /// Time to buffer results internally before streaming to the console. This is useful to65- /// provide a sorted output, in case the total execution time is shorter than66- /// `max_buffer_time`.67- pub max_buffer_time: Option<Duration>,68-69- /// `None` if the output should not be colorized. Otherwise, a `LsColors` instance that defines70- /// how to style different filetypes.71- pub ls_colors: Option<LsColors>,72-73- /// Whether or not we are writing to an interactive terminal74- pub interactive_terminal: bool,75-76- /// The type of file to search for. If set to `None`, all file types are displayed. If77- /// set to `Some(..)`, only the types that are specified are shown.78- pub file_types: Option<FileTypes>,79-80- /// The extension to search for. Only entries matching the extension will be included.81- ///82- /// The value (if present) will be a lowercase string without leading dots.83- pub extensions: Option<RegexSet>,84-85- /// If a value is supplied, each item found will be used to generate and execute commands.86- pub command: Option<Arc<CommandTemplate>>,87-88- /// A list of glob patterns that should be excluded from the search.89- pub exclude_patterns: Vec<String>,90-91- /// A list of custom ignore files.92- pub ignore_files: Vec<PathBuf>,93-94- /// The given constraints on the size of returned files95- pub size_constraints: Vec<SizeFilter>,96-97- /// Constraints on last modification time of files98- pub time_constraints: Vec<TimeFilter>,99-100- #[cfg(unix)]101- /// User/group ownership constraint102- pub owner_constraint: Option<OwnerFilter>,103-104- /// Whether or not to display filesystem errors105- pub show_filesystem_errors: bool,106-107- /// The separator used to print file paths.108- pub path_separator: Option<String>,109-110- /// The maximum number of search results111- pub max_results: Option<usize>,112-}modified:src/output.rs+6 added-6 removed
4 unmodified lines5 use std::sync::Arc;6 7 use lscolors::{LsColors, Style};8 9+use crate::config::Config;10 use crate::error::print_error;11 use crate::exit_codes::ExitCode;12 use crate::filesystem::strip_current_dir;12-use crate::options::Options;13 14 fn replace_path_separator(path: &str, new_path_separator: &str) -> String {15 path.replace(std::path::MAIN_SEPARATOR, new_path_separator)16 }1 unmodified line18 // TODO: this function is performance critical and can probably be optimized19 pub fn print_entry(20 stdout: &mut StdoutLock,21 entry: &Path,22- config: &Options,22+ config: &Config,23 wants_to_quit: &Arc<AtomicBool>,24 ) {25 let path = if entry.is_absolute() {26 entry21 unmodified lines48 // TODO: this function is performance critical and can probably be optimized49 fn print_entry_colorized(50 stdout: &mut StdoutLock,51 path: &Path,52- config: &Options,52+ config: &Config,53 ls_colors: &LsColors,54 wants_to_quit: &Arc<AtomicBool>,55 ) -> io::Result<()> {56 let default_style = ansi_term::Style::default();27 unmodified lines84 // TODO: this function is performance critical and can probably be optimized85 fn print_entry_uncolorized_base(86 stdout: &mut StdoutLock,87 path: &Path,88- config: &Options,88+ config: &Config,89 ) -> io::Result<()> {90 let separator = if config.null_separator { "\0" } else { "\n" };91 92 let mut path_string = path.to_string_lossy();6 unmodified lines99 #[cfg(not(unix))]100 fn print_entry_uncolorized(101 stdout: &mut StdoutLock,102 path: &Path,103- config: &Options,103+ config: &Config,104 ) -> io::Result<()> {105 print_entry_uncolorized_base(stdout, path, config)106 }107 108 #[cfg(unix)]109 fn print_entry_uncolorized(110 stdout: &mut StdoutLock,111 path: &Path,112- config: &Options,112+ config: &Config,113 ) -> io::Result<()> {114 use std::os::unix::ffi::OsStrExt;115 116 if config.interactive_terminal || config.path_separator.is_some() {9 unmodified linesmodified:src/walk.rs+4 added-4 removed
13 unmodified lines14 use ignore::overrides::OverrideBuilder;15 use ignore::{self, WalkBuilder};16 use regex::bytes::Regex;17 18+use crate::config::Config;19 use crate::error::print_error;20 use crate::exec;21 use crate::exit_codes::{merge_exitcodes, ExitCode};22 use crate::filesystem;22-use crate::options::Options;23 use crate::output;24 25 /// The receiver thread can either be buffering results or directly streaming to the console.26 enum ReceiverMode {20 unmodified lines47 ///48 /// If the `--exec` argument was supplied, this will create a thread pool for executing49 /// jobs in parallel from a given command line and the discovered paths. Otherwise, each50 /// path will simply be written to standard output.51-pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<Options>) -> Result<ExitCode> {51+pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<Config>) -> Result<ExitCode> {52 let mut path_iter = path_vec.iter();53 let first_path_buf = path_iter54 .next()55 .expect("Error: Path vector can not be empty");106 unmodified lines162 }163 }164 165 fn spawn_receiver(166- config: &Arc<Options>,166+ config: &Arc<Config>,167 wants_to_quit: &Arc<AtomicBool>,168 rx: Receiver<WorkerResult>,169 ) -> thread::JoinHandle<ExitCode> {170 let config = Arc::clone(config);161 unmodified lines332 }333 }334 335 fn spawn_senders(336- config: &Arc<Options>,336+ config: &Arc<Config>,337 wants_to_quit: &Arc<AtomicBool>,338 pattern: Arc<Regex>,339 parallel_walker: ignore::WalkParallel,340 tx: Sender<WorkerResult>,159 unmodified lines