Skip to content
Myr

Restructure according to Cargo doc

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by sharkdp · committed by sharkdp · parent 002983b · Browse files · 4 files+210 added−224 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+210 added−224 removed

  • other:+210 added−224 removed

4 files+210 added−224 removed

deleted:src/bin/main.rs-209 removed
1extern crate ansi_term;2extern crate getopts;3extern crate isatty;4extern crate regex;5extern crate ignore;6extern crate fd;78use std::env;9use std::error::Error;10use std::ffi::OsStr;11use std::io::Write;12use std::path::{Path, Component};13use std::process;1415use getopts::Options;16use isatty::stdout_isatty;17use regex::{Regex, RegexBuilder};18use ignore::WalkBuilder;1920use fd::lscolors::LsColors;2122/// Configuration options for *fd*.23struct FdOptions {24 case_sensitive: bool,25 search_full_path: bool,26 ignore_hidden: bool,27 read_ignore: bool,28 follow_links: bool,29 max_depth: Option<usize>,30 colored: bool,31 ls_colors: LsColors32}3334/// Print a search result to the console.35fn print_entry(path_root: &Path, path_entry: &Path, config: &FdOptions) {36 let path_full = path_root.join(path_entry);3738 let path_str = match path_entry.to_str() {39 Some(p) => p,40 None => return41 };4243 if !config.colored {44 println!("{}", path_str);45 } else {46 let mut component_path = path_root.to_path_buf();4748 // Traverse the path and colorize each component49 for component in path_entry.components() {50 let comp_str = match component {51 Component::Normal(p) => p,52 _ => error("Unexpected path component")53 };5455 component_path.push(Path::new(comp_str));5657 let style =58 if component_path.symlink_metadata()59 .map(|md| md.file_type().is_symlink())60 .unwrap_or(false) {61 config.ls_colors.symlink62 } else if component_path.is_dir() {63 config.ls_colors.directory64 } else {65 // Look up file name66 let o_style =67 component_path.file_name()68 .and_then(|n| n.to_str())69 .and_then(|n| config.ls_colors.filenames.get(n));7071 match o_style {72 Some(s) => *s,73 None =>74 // Look up file extension75 component_path.extension()76 .and_then(|e| e.to_str())77 .and_then(|e| config.ls_colors.extensions.get(e))78 .cloned()79 .unwrap_or_default()80 }81 };8283 print!("{}", style.paint(comp_str.to_str().unwrap()));8485 if component_path.is_dir() && component_path != path_full {86 let sep = std::path::MAIN_SEPARATOR.to_string();87 print!("{}", style.paint(sep));88 }89 }90 println!();91 }92}9394/// Recursively scan the given root path and search for files / pathnames95/// matching the pattern.96fn scan(root: &Path, pattern: &Regex, config: &FdOptions) {97 let walker = WalkBuilder::new(root)98 .hidden(config.ignore_hidden)99 .ignore(config.read_ignore)100 .git_ignore(config.read_ignore)101 .parents(config.read_ignore)102 .git_global(config.read_ignore)103 .git_exclude(config.read_ignore)104 .follow_links(config.follow_links)105 .max_depth(config.max_depth)106 .build()107 .into_iter()108 .filter_map(|e| e.ok())109 .filter(|e| e.path() != root);110111 for entry in walker {112 let path_rel = match entry.path().strip_prefix(root) {113 Ok(p) => p,114 Err(_) => continue115 };116117 let search_str =118 if config.search_full_path {119 path_rel.to_str()120 } else {121 path_rel.file_name()122 .and_then(OsStr::to_str)123 };124125 search_str.and_then(|s| pattern.find(s))126 .map(|_| print_entry(root, path_rel, config));127 }128}129130/// Print error message to stderr and exit with status `1`.131fn error(message: &str) -> ! {132 writeln!(&mut std::io::stderr(), "{}", message)133 .expect("Failed writing to stderr");134 process::exit(1);135}136137fn main() {138 let args: Vec<String> = env::args().collect();139140 let mut opts = Options::new();141 opts.optflag("h", "help",142 "print this help message");143 opts.optflag("s", "sensitive",144 "case-sensitive search (default: smart case)");145 opts.optflag("p", "full-path",146 "search full path (default: file-/dirname only)");147 opts.optflag("H", "hidden",148 "search hidden files/directories");149 opts.optflag("I", "no-ignore",150 "do not respect .(git)ignore files");151 opts.optflag("f", "follow",152 "follow symlinks");153 opts.optflag("n", "no-color",154 "do not colorize output");155 opts.optopt( "d", "max-depth",156 "maximum search depth (default: none)", "D");157158 let matches = match opts.parse(&args[1..]) {159 Ok(m) => m,160 Err(e) => error(e.description())161 };162163 if matches.opt_present("h") {164 let brief = "Usage: fd [options] [PATTERN]";165 print!("{}", opts.usage(brief));166 process::exit(1);167 }168169 let empty = String::new();170 let pattern = matches.free.get(0).unwrap_or(&empty);171172 let current_dir_buf = match env::current_dir() {173 Ok(cd) => cd,174 Err(_) => error("Could not get current directory!")175 };176 let current_dir = current_dir_buf.as_path();177178 let colored_output = !matches.opt_present("no-color") &&179 stdout_isatty();180181 let ls_colors =182 env::var("LS_COLORS")183 .ok()184 .map(|val| LsColors::from_string(&val))185 .unwrap_or_default();186187 let config = FdOptions {188 // The search will be case-sensitive if the command line flag is set or189 // if the pattern has an uppercase character (smart case).190 case_sensitive: matches.opt_present("sensitive") ||191 pattern.chars().any(char::is_uppercase),192 search_full_path: matches.opt_present("full-path"),193 ignore_hidden: !matches.opt_present("hidden"),194 read_ignore: !matches.opt_present("no-ignore"),195 follow_links: matches.opt_present("follow"),196 max_depth:197 matches.opt_str("max-depth")198 .and_then(|ds| usize::from_str_radix(&ds, 10).ok()),199 colored: colored_output,200 ls_colors: ls_colors201 };202203 match RegexBuilder::new(pattern)204 .case_insensitive(!config.case_sensitive)205 .build() {206 Ok(re) => scan(current_dir, &re, &config),207 Err(err) => error(err.description())208 }209}
deleted:src/fd.rs-7 removed
1extern crate ansi_term;2extern crate getopts;3extern crate isatty;4extern crate regex;5extern crate ignore;67pub mod lscolors;
added:src/main.rs+210 added
1extern crate ansi_term;2extern crate getopts;3extern crate isatty;4extern crate regex;5extern crate ignore;67pub mod lscolors;89use std::env;10use std::error::Error;11use std::ffi::OsStr;12use std::io::Write;13use std::path::{Path, Component};14use std::process;1516use getopts::Options;17use isatty::stdout_isatty;18use regex::{Regex, RegexBuilder};19use ignore::WalkBuilder;2021use lscolors::LsColors;2223/// Configuration options for *fd*.24struct FdOptions {25 case_sensitive: bool,26 search_full_path: bool,27 ignore_hidden: bool,28 read_ignore: bool,29 follow_links: bool,30 max_depth: Option<usize>,31 colored: bool,32 ls_colors: LsColors33}3435/// Print a search result to the console.36fn print_entry(path_root: &Path, path_entry: &Path, config: &FdOptions) {37 let path_full = path_root.join(path_entry);3839 let path_str = match path_entry.to_str() {40 Some(p) => p,41 None => return42 };4344 if !config.colored {45 println!("{}", path_str);46 } else {47 let mut component_path = path_root.to_path_buf();4849 // Traverse the path and colorize each component50 for component in path_entry.components() {51 let comp_str = match component {52 Component::Normal(p) => p,53 _ => error("Unexpected path component")54 };5556 component_path.push(Path::new(comp_str));5758 let style =59 if component_path.symlink_metadata()60 .map(|md| md.file_type().is_symlink())61 .unwrap_or(false) {62 config.ls_colors.symlink63 } else if component_path.is_dir() {64 config.ls_colors.directory65 } else {66 // Look up file name67 let o_style =68 component_path.file_name()69 .and_then(|n| n.to_str())70 .and_then(|n| config.ls_colors.filenames.get(n));7172 match o_style {73 Some(s) => *s,74 None =>75 // Look up file extension76 component_path.extension()77 .and_then(|e| e.to_str())78 .and_then(|e| config.ls_colors.extensions.get(e))79 .cloned()80 .unwrap_or_default()81 }82 };8384 print!("{}", style.paint(comp_str.to_str().unwrap()));8586 if component_path.is_dir() && component_path != path_full {87 let sep = std::path::MAIN_SEPARATOR.to_string();88 print!("{}", style.paint(sep));89 }90 }91 println!();92 }93}9495/// Recursively scan the given root path and search for files / pathnames96/// matching the pattern.97fn scan(root: &Path, pattern: &Regex, config: &FdOptions) {98 let walker = WalkBuilder::new(root)99 .hidden(config.ignore_hidden)100 .ignore(config.read_ignore)101 .git_ignore(config.read_ignore)102 .parents(config.read_ignore)103 .git_global(config.read_ignore)104 .git_exclude(config.read_ignore)105 .follow_links(config.follow_links)106 .max_depth(config.max_depth)107 .build()108 .into_iter()109 .filter_map(|e| e.ok())110 .filter(|e| e.path() != root);111112 for entry in walker {113 let path_rel = match entry.path().strip_prefix(root) {114 Ok(p) => p,115 Err(_) => continue116 };117118 let search_str =119 if config.search_full_path {120 path_rel.to_str()121 } else {122 path_rel.file_name()123 .and_then(OsStr::to_str)124 };125126 search_str.and_then(|s| pattern.find(s))127 .map(|_| print_entry(root, path_rel, config));128 }129}130131/// Print error message to stderr and exit with status `1`.132fn error(message: &str) -> ! {133 writeln!(&mut std::io::stderr(), "{}", message)134 .expect("Failed writing to stderr");135 process::exit(1);136}137138fn main() {139 let args: Vec<String> = env::args().collect();140141 let mut opts = Options::new();142 opts.optflag("h", "help",143 "print this help message");144 opts.optflag("s", "sensitive",145 "case-sensitive search (default: smart case)");146 opts.optflag("p", "full-path",147 "search full path (default: file-/dirname only)");148 opts.optflag("H", "hidden",149 "search hidden files/directories");150 opts.optflag("I", "no-ignore",151 "do not respect .(git)ignore files");152 opts.optflag("f", "follow",153 "follow symlinks");154 opts.optflag("n", "no-color",155 "do not colorize output");156 opts.optopt( "d", "max-depth",157 "maximum search depth (default: none)", "D");158159 let matches = match opts.parse(&args[1..]) {160 Ok(m) => m,161 Err(e) => error(e.description())162 };163164 if matches.opt_present("h") {165 let brief = "Usage: fd [options] [PATTERN]";166 print!("{}", opts.usage(brief));167 process::exit(1);168 }169170 let empty = String::new();171 let pattern = matches.free.get(0).unwrap_or(&empty);172173 let current_dir_buf = match env::current_dir() {174 Ok(cd) => cd,175 Err(_) => error("Could not get current directory!")176 };177 let current_dir = current_dir_buf.as_path();178179 let colored_output = !matches.opt_present("no-color") &&180 stdout_isatty();181182 let ls_colors =183 env::var("LS_COLORS")184 .ok()185 .map(|val| LsColors::from_string(&val))186 .unwrap_or_default();187188 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),193 search_full_path: matches.opt_present("full-path"),194 ignore_hidden: !matches.opt_present("hidden"),195 read_ignore: !matches.opt_present("no-ignore"),196 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()),200 colored: colored_output,201 ls_colors: ls_colors202 };203204 match RegexBuilder::new(pattern)205 .case_insensitive(!config.case_sensitive)206 .build() {207 Ok(re) => scan(current_dir, &re, &config),208 Err(err) => error(err.description())209 }210}
modified:Cargo.toml-8 removed