internal.rs
This file
Path
src/internal.rs
114 lines · 3.9 KiB · Rust
Last changed by
Add support for exclude-patternssharkdpbefore adoption5ad69fb
History of this fileTake it
1// Copyright (c) 2017 fd developers2// Licensed under the Apache License, Version 2.03// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>4// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,5// at your option. All files in the project carrying such6// notice may not be copied, modified, or distributed except7// according to those terms.89use std::process;10use std::time;11use std::io::Write;1213use exec::TokenizedCommand;14use lscolors::LsColors;15use walk::FileType;16use regex_syntax::{Expr, ExprBuilder};1718/// Defines how to display search result paths.19#[derive(PartialEq)]20pub enum PathDisplay {21 /// As an absolute path22 Absolute,2324 /// As a relative path25 Relative,26}2728/// Configuration options for *fd*.29pub struct FdOptions {30 /// Whether the search is case-sensitive or case-insensitive.31 pub case_sensitive: bool,3233 /// Whether to search within the full file path or just the base name (filename or directory34 /// name).35 pub search_full_path: bool,3637 /// Whether to ignore hidden files and directories (or not).38 pub ignore_hidden: bool,3940 /// Whether to respect VCS ignore files (`.gitignore`, `.ignore`, ..) or not.41 pub read_ignore: bool,4243 /// Whether to follow symlinks or not.44 pub follow_links: bool,4546 /// Whether elements of output should be separated by a null character47 pub null_separator: bool,4849 /// The maximum search depth, or `None` if no maximum search depth should be set.50 ///51 /// A depth of `1` includes all files under the current directory, a depth of `2` also includes52 /// all files under subdirectories of the current directory, etc.53 pub max_depth: Option<usize>,5455 /// The number of threads to use.56 pub threads: usize,5758 /// Time to buffer results internally before streaming to the console. This is useful to59 /// provide a sorted output, in case the total execution time is shorter than60 /// `max_buffer_time`.61 pub max_buffer_time: Option<time::Duration>,6263 /// Display results as relative or absolute path.64 pub path_display: PathDisplay,6566 /// `None` if the output should not be colorized. Otherwise, a `LsColors` instance that defines67 /// how to style different filetypes.68 pub ls_colors: Option<LsColors>,6970 /// The type of file to search for. All files other than the specified type will be ignored.71 pub file_type: FileType,7273 /// The extension to search for. Only entries matching the extension will be included.74 ///75 /// The value (if present) will be a lowercase string without leading dots.76 pub extension: Option<String>,7778 /// If a value is supplied, each item found will be used to generate and execute commands.79 pub command: Option<TokenizedCommand>,8081 /// A list of glob patterns that should be excluded from the search.82 pub exclude_patterns: Vec<String>,83}8485/// Print error message to stderr and exit with status `1`.86pub fn error(message: &str) -> ! {87 writeln!(&mut ::std::io::stderr(), "{}", message).expect("Failed writing to stderr");88 process::exit(1);89}9091/// Determine if a regex pattern contains a literal uppercase character.92pub fn pattern_has_uppercase_char(pattern: &str) -> bool {93 ExprBuilder::new()94 .parse(pattern)95 .map(|expr| expr_has_uppercase_char(&expr))96 .unwrap_or(false)97}9899/// Determine if a regex expression contains a literal uppercase character.100fn expr_has_uppercase_char(expr: &Expr) -> bool {101 match *expr {102 Expr::Literal { ref chars, .. } => chars.iter().any(|c| c.is_uppercase()),103 Expr::Class(ref ranges) => {104 ranges.iter().any(|r| {105 r.start.is_uppercase() || r.end.is_uppercase()106 })107 }108 Expr::Group { ref e, .. } |109 Expr::Repeat { ref e, .. } => expr_has_uppercase_char(e),110 Expr::Concat(ref es) => es.iter().any(expr_has_uppercase_char),111 Expr::Alternate(ref es) => es.iter().any(expr_has_uppercase_char),112 _ => false,113 }114}