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
Provenance
- Commit
0f5ede4- Tree
01a0867- Author
sharkdp <davidpeter@web.de>- Committer
sharkdp <davidpeter@web.de>- Parent
002983b
4 files changed+210 added−224 removed
deleted:src/bin/main.rs-209 removed
1-extern crate ansi_term;2-extern crate getopts;3-extern crate isatty;4-extern crate regex;5-extern crate ignore;6-extern crate fd;7-8-use std::env;9-use std::error::Error;10-use std::ffi::OsStr;11-use std::io::Write;12-use std::path::{Path, Component};13-use std::process;14-15-use getopts::Options;16-use isatty::stdout_isatty;17-use regex::{Regex, RegexBuilder};18-use ignore::WalkBuilder;19-20-use fd::lscolors::LsColors;21-22-/// Configuration options for *fd*.23-struct 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-}33-34-/// Print a search result to the console.35-fn print_entry(path_root: &Path, path_entry: &Path, config: &FdOptions) {36- let path_full = path_root.join(path_entry);37-38- let path_str = match path_entry.to_str() {39- Some(p) => p,40- None => return41- };42-43- if !config.colored {44- println!("{}", path_str);45- } else {46- let mut component_path = path_root.to_path_buf();47-48- // 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- };54-55- component_path.push(Path::new(comp_str));56-57- 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));70-71- 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- };82-83- print!("{}", style.paint(comp_str.to_str().unwrap()));84-85- 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-}93-94-/// Recursively scan the given root path and search for files / pathnames95-/// matching the pattern.96-fn 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);110-111- for entry in walker {112- let path_rel = match entry.path().strip_prefix(root) {113- Ok(p) => p,114- Err(_) => continue115- };116-117- 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- };124-125- search_str.and_then(|s| pattern.find(s))126- .map(|_| print_entry(root, path_rel, config));127- }128-}129-130-/// Print error message to stderr and exit with status `1`.131-fn error(message: &str) -> ! {132- writeln!(&mut std::io::stderr(), "{}", message)133- .expect("Failed writing to stderr");134- process::exit(1);135-}136-137-fn main() {138- let args: Vec<String> = env::args().collect();139-140- 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");157-158- let matches = match opts.parse(&args[1..]) {159- Ok(m) => m,160- Err(e) => error(e.description())161- };162-163- if matches.opt_present("h") {164- let brief = "Usage: fd [options] [PATTERN]";165- print!("{}", opts.usage(brief));166- process::exit(1);167- }168-169- let empty = String::new();170- let pattern = matches.free.get(0).unwrap_or(&empty);171-172- 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();177-178- let colored_output = !matches.opt_present("no-color") &&179- stdout_isatty();180-181- let ls_colors =182- env::var("LS_COLORS")183- .ok()184- .map(|val| LsColors::from_string(&val))185- .unwrap_or_default();186-187- 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- };202-203- 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
1-extern crate ansi_term;2-extern crate getopts;3-extern crate isatty;4-extern crate regex;5-extern crate ignore;6-7-pub mod lscolors;added:src/main.rs+210 added
1+extern crate ansi_term;2+extern crate getopts;3+extern crate isatty;4+extern crate regex;5+extern crate ignore;6+7+pub mod lscolors;8+9+use std::env;10+use std::error::Error;11+use std::ffi::OsStr;12+use std::io::Write;13+use std::path::{Path, Component};14+use std::process;15+16+use getopts::Options;17+use isatty::stdout_isatty;18+use regex::{Regex, RegexBuilder};19+use ignore::WalkBuilder;20+21+use lscolors::LsColors;22+23+/// Configuration options for *fd*.24+struct 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+}34+35+/// Print a search result to the console.36+fn print_entry(path_root: &Path, path_entry: &Path, config: &FdOptions) {37+ let path_full = path_root.join(path_entry);38+39+ let path_str = match path_entry.to_str() {40+ Some(p) => p,41+ None => return42+ };43+44+ if !config.colored {45+ println!("{}", path_str);46+ } else {47+ let mut component_path = path_root.to_path_buf();48+49+ // 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+ };55+56+ component_path.push(Path::new(comp_str));57+58+ 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));71+72+ 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+ };83+84+ print!("{}", style.paint(comp_str.to_str().unwrap()));85+86+ 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+}94+95+/// Recursively scan the given root path and search for files / pathnames96+/// matching the pattern.97+fn 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);111+112+ for entry in walker {113+ let path_rel = match entry.path().strip_prefix(root) {114+ Ok(p) => p,115+ Err(_) => continue116+ };117+118+ 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+ };125+126+ search_str.and_then(|s| pattern.find(s))127+ .map(|_| print_entry(root, path_rel, config));128+ }129+}130+131+/// Print error message to stderr and exit with status `1`.132+fn error(message: &str) -> ! {133+ writeln!(&mut std::io::stderr(), "{}", message)134+ .expect("Failed writing to stderr");135+ process::exit(1);136+}137+138+fn main() {139+ let args: Vec<String> = env::args().collect();140+141+ 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");158+159+ let matches = match opts.parse(&args[1..]) {160+ Ok(m) => m,161+ Err(e) => error(e.description())162+ };163+164+ if matches.opt_present("h") {165+ let brief = "Usage: fd [options] [PATTERN]";166+ print!("{}", opts.usage(brief));167+ process::exit(1);168+ }169+170+ let empty = String::new();171+ let pattern = matches.free.get(0).unwrap_or(&empty);172+173+ 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();178+179+ let colored_output = !matches.opt_present("no-color") &&180+ stdout_isatty();181+182+ let ls_colors =183+ env::var("LS_COLORS")184+ .ok()185+ .map(|val| LsColors::from_string(&val))186+ .unwrap_or_default();187+188+ 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+ };203+204+ 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
1234567891011121367892 unmodified lines[package]name = "fd"version = "0.3.0"authors = ["David Peter <mail@david-peter.de>"]
[[bin]]name = "fd"path = "src/bin/main.rs"
[lib]name = "fd"path = "src/fd.rs"
[dependencies]ansi_term = "0.9"getopts = "0.2"isatty = "0.1"2 unmodified lines