Skip to content
Myr

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

5 files+129 added−129 removed

added:src/config.rs+112 added
1use std::{path::PathBuf, sync::Arc, time::Duration};23use lscolors::LsColors;4use regex::bytes::RegexSet;56use crate::exec::CommandTemplate;7use crate::filetypes::FileTypes;8#[cfg(unix)]9use crate::filter::OwnerFilter;10use crate::filter::{SizeFilter, TimeFilter};1112/// Configuration options for *fd*.13pub struct Config {14 /// Whether the search is case-sensitive or case-insensitive.15 pub case_sensitive: bool,1617 /// Whether to search within the full file path or just the base name (filename or directory18 /// name).19 pub search_full_path: bool,2021 /// Whether to ignore hidden files and directories (or not).22 pub ignore_hidden: bool,2324 /// Whether to respect `.fdignore` files or not.25 pub read_fdignore: bool,2627 /// Whether to respect ignore files in parent directories or not.28 pub read_parent_ignore: bool,2930 /// Whether to respect VCS ignore files (`.gitignore`, ..) or not.31 pub read_vcsignore: bool,3233 /// Whether to respect the global ignore file or not.34 pub read_global_ignore: bool,3536 /// Whether to follow symlinks or not.37 pub follow_links: bool,3839 /// Whether to limit the search to starting file system or not.40 pub one_file_system: bool,4142 /// Whether elements of output should be separated by a null character43 pub null_separator: bool,4445 /// 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>,5051 /// The minimum depth for reported entries, or `None`.52 pub min_depth: Option<usize>,5354 /// Whether to stop traversing into matching directories.55 pub prune: bool,5657 /// The number of threads to use.58 pub threads: usize,5960 /// 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,6364 /// 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>,6869 /// `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>,7273 /// Whether or not we are writing to an interactive terminal74 pub interactive_terminal: bool,7576 /// 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>,7980 /// 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>,8485 /// If a value is supplied, each item found will be used to generate and execute commands.86 pub command: Option<Arc<CommandTemplate>>,8788 /// A list of glob patterns that should be excluded from the search.89 pub exclude_patterns: Vec<String>,9091 /// A list of custom ignore files.92 pub ignore_files: Vec<PathBuf>,9394 /// The given constraints on the size of returned files95 pub size_constraints: Vec<SizeFilter>,9697 /// Constraints on last modification time of files98 pub time_constraints: Vec<TimeFilter>,99100 #[cfg(unix)]101 /// User/group ownership constraint102 pub owner_constraint: Option<OwnerFilter>,103104 /// Whether or not to display filesystem errors105 pub show_filesystem_errors: bool,106107 /// The separator used to print file paths.108 pub path_separator: Option<String>,109110 /// The maximum number of search results111 pub max_results: Option<usize>,112}
modified:src/main.rs+7 added-7 removed
1mod app;2mod config;3mod error;4mod exec;5mod exit_codes;6mod filesystem;7mod filetypes;8mod filter;8mod options;9mod output;10mod regex_helper;11mod walk;12
9 unmodified lines
22use lscolors::LsColors;23use normpath::PathExt;24use regex::bytes::{RegexBuilder, RegexSetBuilder};2526use crate::config::Config;27use crate::error::print_error;28use crate::exec::CommandTemplate;29use crate::exit_codes::ExitCode;30use crate::filetypes::FileTypes;31#[cfg(unix)]32use crate::filter::OwnerFilter;33use crate::filter::{SizeFilter, TimeFilter};33use crate::options::Options;34use crate::regex_helper::{pattern_has_uppercase_char, pattern_matches_strings_with_leading_dot};3536// 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 infos
36 unmodified lines
74 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)?;7778 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 lines
209 _ => Ok(()),210 }211}212213fn construct_options(matches: clap::ArgMatches, pattern_regex: &str) -> Result<Options> {213fn 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 lines
249 None250 };251 let command = extract_command(&matches, path_separator.as_deref(), colored_output)?;252253 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 lines
528 Ok(time_constraints)529}530531fn 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 lines
543 Ok(())544 }545}546547fn build_regex(pattern_regex: String, config: &Options) -> Result<regex::bytes::Regex> {547fn 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 lines
deleted:src/options.rs-112 removed
1use std::{path::PathBuf, sync::Arc, time::Duration};23use lscolors::LsColors;4use regex::bytes::RegexSet;56use crate::exec::CommandTemplate;7use crate::filetypes::FileTypes;8#[cfg(unix)]9use crate::filter::OwnerFilter;10use crate::filter::{SizeFilter, TimeFilter};1112/// Configuration options for *fd*.13pub struct Options {14 /// Whether the search is case-sensitive or case-insensitive.15 pub case_sensitive: bool,1617 /// Whether to search within the full file path or just the base name (filename or directory18 /// name).19 pub search_full_path: bool,2021 /// Whether to ignore hidden files and directories (or not).22 pub ignore_hidden: bool,2324 /// Whether to respect `.fdignore` files or not.25 pub read_fdignore: bool,2627 /// Whether to respect ignore files in parent directories or not.28 pub read_parent_ignore: bool,2930 /// Whether to respect VCS ignore files (`.gitignore`, ..) or not.31 pub read_vcsignore: bool,3233 /// Whether to respect the global ignore file or not.34 pub read_global_ignore: bool,3536 /// Whether to follow symlinks or not.37 pub follow_links: bool,3839 /// Whether to limit the search to starting file system or not.40 pub one_file_system: bool,4142 /// Whether elements of output should be separated by a null character43 pub null_separator: bool,4445 /// 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>,5051 /// The minimum depth for reported entries, or `None`.52 pub min_depth: Option<usize>,5354 /// Whether to stop traversing into matching directories.55 pub prune: bool,5657 /// The number of threads to use.58 pub threads: usize,5960 /// 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,6364 /// 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>,6869 /// `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>,7273 /// Whether or not we are writing to an interactive terminal74 pub interactive_terminal: bool,7576 /// 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>,7980 /// 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>,8485 /// If a value is supplied, each item found will be used to generate and execute commands.86 pub command: Option<Arc<CommandTemplate>>,8788 /// A list of glob patterns that should be excluded from the search.89 pub exclude_patterns: Vec<String>,9091 /// A list of custom ignore files.92 pub ignore_files: Vec<PathBuf>,9394 /// The given constraints on the size of returned files95 pub size_constraints: Vec<SizeFilter>,9697 /// Constraints on last modification time of files98 pub time_constraints: Vec<TimeFilter>,99100 #[cfg(unix)]101 /// User/group ownership constraint102 pub owner_constraint: Option<OwnerFilter>,103104 /// Whether or not to display filesystem errors105 pub show_filesystem_errors: bool,106107 /// The separator used to print file paths.108 pub path_separator: Option<String>,109110 /// The maximum number of search results111 pub max_results: Option<usize>,112}
modified:src/output.rs+6 added-6 removed
4 unmodified lines
5use std::sync::Arc;67use lscolors::{LsColors, Style};89use crate::config::Config;10use crate::error::print_error;11use crate::exit_codes::ExitCode;12use crate::filesystem::strip_current_dir;12use crate::options::Options;1314fn replace_path_separator(path: &str, new_path_separator: &str) -> String {15 path.replace(std::path::MAIN_SEPARATOR, new_path_separator)16}
1 unmodified line
18// TODO: this function is performance critical and can probably be optimized19pub 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 entry
21 unmodified lines
48// TODO: this function is performance critical and can probably be optimized49fn 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 lines
84// TODO: this function is performance critical and can probably be optimized85fn 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" };9192 let mut path_string = path.to_string_lossy();
6 unmodified lines
99#[cfg(not(unix))]100fn 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}107108#[cfg(unix)]109fn 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;115116 if config.interactive_terminal || config.path_separator.is_some() {
9 unmodified lines
modified:src/walk.rs+4 added-4 removed
13 unmodified lines
14use ignore::overrides::OverrideBuilder;15use ignore::{self, WalkBuilder};16use regex::bytes::Regex;1718use crate::config::Config;19use crate::error::print_error;20use crate::exec;21use crate::exit_codes::{merge_exitcodes, ExitCode};22use crate::filesystem;22use crate::options::Options;23use crate::output;2425/// The receiver thread can either be buffering results or directly streaming to the console.26enum ReceiverMode {
20 unmodified lines
47///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.51pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<Options>) -> Result<ExitCode> {51pub 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 lines
162 }163}164165fn 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 lines
332 }333}334335fn 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