Skip to content
Myr

main.rs

This file

Path

src/main.rs

165 lines · 5.1 KiB · Rust

Last changed by

Move 'win' crate back into local modulesharkdpbefore adoptionb3f1e18

History of this file

Take it

Relicense under MIT/Apache-2.0Antti Keränenbefore adoption701b8f2

lines 1–8why, one step back
1// Copyright (c) 2017 fd developers
2// 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.8

Restructure according to Cargo docsharkdpbefore adoption0f5ede4

line 9why, one step back
9extern crate ansi_term;

Use atty instead of isattysharkdpbefore adoption1adcf76

line 10why, one step back
10extern crate atty;

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 11–12why, one step back
11#[macro_use]
12extern crate clap;

Restructure according to Cargo docsharkdpbefore adoption0f5ede4

line 13why, one step back
13extern crate ignore;

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 14–17why, one step back
14#[macro_use]
15extern crate lazy_static;16#[cfg(all(unix, not(target_os = "redox")))]17extern crate libc;

Use N-1 search threads, where N = # coressharkdpbefore adoptionb4c8a8b

line 18why, one step back
18extern crate num_cpus;

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 19–20why, one step back
19extern crate regex;
20extern crate regex_syntax;

Fix --execJ.Wbefore adoption6b232a8

line 21why, one step back
21extern crate shell_escape;

Restructure according to Cargo docsharkdpbefore adoption0f5ede4

line 22why, one step back
22

Add optional second argument, closes #5sharkdpbefore adoption634016b

line 23why, one step back
23pub mod fshelper;

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

line 24why, one step back
24pub mod lscolors;

Use clap to generate shell completionsFlorian Hofmairbefore adoption396b9ba

line 25why, one step back
25mod app;

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

line 26why, one step back
26mod exec;

break main into separate modulesGarrett Squirebefore adoptionccb899a

lines 27–29why, one step back
27mod internal;
28mod output;29mod walk;

Restructure according to Cargo docsharkdpbefore adoption0f5ede4

line 30why, one step back
30

Move 'win' crate back into local modulesharkdpbefore adoptionb3f1e18

lines 31–33why, one step back
31#[cfg(windows)]
32mod windows;33

Restructure according to Cargo docsharkdpbefore adoption0f5ede4

lines 34–35why, one step back
34use std::env;
35use std::error::Error;

Use a uniform output format for searching ../J.Wbefore adoptione649c8f

line 36why, one step back
36use std::path::{Path, PathBuf};

Initial version of parallel directory traversalsharkdpbefore adoptione06189e

line 37why, one step back
37use std::sync::Arc;

Implement short-time buffering for sorted outputsharkdpbefore adoption550e3b3

line 38why, one step back
38use std::time;

Restructure according to Cargo docsharkdpbefore adoption0f5ede4

line 39why, one step back
39

Use atty instead of isattysharkdpbefore adoption1adcf76

line 40why, one step back
40use atty::Stream;

break main into separate modulesGarrett Squirebefore adoptionccb899a

line 41why, one step back
41use regex::RegexBuilder;

Restructure according to Cargo docsharkdpbefore adoption0f5ede4

line 42why, one step back
42

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

line 43why, one step back
43use exec::TokenizedCommand;

Refactor path handling (fixes #113)J.Wbefore adoption8d85deb

line 44why, one step back
44use internal::{error, pattern_has_uppercase_char, FdOptions, PathDisplay};

Restructure according to Cargo docsharkdpbefore adoption0f5ede4

line 45why, one step back
45use lscolors::LsColors;

break main into separate modulesGarrett Squirebefore adoptionccb899a

line 46why, one step back
46use walk::FileType;

Restructure according to Cargo docsharkdpbefore adoption0f5ede4

lines 47–48why, one step back
47
48fn main() {

Use clap to generate shell completionsFlorian Hofmairbefore adoption396b9ba

line 49why, one step back
49 let matches = app::build_app().get_matches();

Restructure according to Cargo docsharkdpbefore adoption0f5ede4

line 50why, one step back
50

Add optional second argument, closes #5sharkdpbefore adoption634016b

lines 51–52why, one step back
51 // Get the search pattern
52 let empty_pattern = String::new();

Use clap, change --sensitive to --case-sensitivesharkdpbefore adoption087e709

line 53why, one step back
53 let pattern = matches.value_of("pattern").unwrap_or(&empty_pattern);

Restructure according to Cargo docsharkdpbefore adoption0f5ede4

line 54why, one step back
54

Add optional second argument, closes #5sharkdpbefore adoption634016b

line 55why, one step back
55 // Get the current working directory

Use a uniform output format for searching ../J.Wbefore adoptione649c8f

line 56why, one step back
56 let current_dir = Path::new(".");

Updates and preparations for v5.0sharkdpbefore adoptione9cf8af

line 57why, one step back
57 if !fshelper::is_dir(current_dir) {

Use a uniform output format for searching ../J.Wbefore adoptione649c8f

lines 58–59why, one step back
58 error("Error: could not get current directory.");
59 }

Restructure according to Cargo docsharkdpbefore adoption0f5ede4

line 60why, one step back
60

Add optional second argument, closes #5sharkdpbefore adoption634016b

line 61why, one step back
61 // Get the root directory for the search

Use a uniform output format for searching ../J.Wbefore adoptione649c8f

line 62why, one step back
62 let mut root_dir_buf = match matches.value_of("path") {

Fix root directory for WindowsJ.Wbefore adoption87ba4e3

lines 63–68why, one step back
63 Some(path) => {
64 #[cfg(windows)]65 let path = path.replace('/', "\\");6667 PathBuf::from(path)68 }

Use a uniform output format for searching ../J.Wbefore adoptione649c8f

line 69why, one step back
69 None => current_dir.to_path_buf(),

Throw an error if root directory is non-existent, fixes #39sharkdpbefore adoption1b1f644

line 70why, one step back
70 };

Refactor path handling (fixes #113)J.Wbefore adoption8d85deb

line 71why, one step back
71 if !fshelper::is_dir(&root_dir_buf) {

Format the source code using `rustfmt`Antti Keränenbefore adoption049b9ec

lines 72–75why, one step back
72 error(&format!(
73 "Error: '{}' is not a directory.",74 root_dir_buf.to_string_lossy()75 ));

Throw an error if root directory is non-existent, fixes #39sharkdpbefore adoption1b1f644

lines 76–77why, one step back
76 }
77

Use a uniform output format for searching ../J.Wbefore adoptione649c8f

lines 78–86why, one step back
78 let path_display = if matches.is_present("absolute-path") || root_dir_buf.is_absolute() {
79 PathDisplay::Absolute80 } else {81 PathDisplay::Relative82 };8384 if path_display == PathDisplay::Absolute && root_dir_buf.is_relative() {85 root_dir_buf = fshelper::absolute_path(root_dir_buf.as_path()).unwrap();86 }

Add optional second argument, closes #5sharkdpbefore adoption634016b

lines 87–88why, one step back
87 let root_dir = root_dir_buf.as_path();
88

Code clean-ups, documentationsharkdpbefore adoptionbf013da

lines 89–90why, one step back
89 // The search will be case-sensitive if the command line flag is set or
90 // if the pattern has an uppercase character (smart case).

Improve smart case to only consider literal uppercase chars (#103)Matthias Reitingerbefore adoptionb441528

lines 91–92why, one step back
91 let case_sensitive = !matches.is_present("ignore-case") &&
92 (matches.is_present("case-sensitive") || pattern_has_uppercase_char(pattern));

Code clean-ups, documentationsharkdpbefore adoptionbf013da

line 93why, one step back
93

Add flag to force colorAntti Keränenbefore adoption4b61bea

lines 94–96why, one step back
94 let colored_output = match matches.value_of("color") {
95 Some("always") => true,96 Some("never") => false,

Format the source code using `rustfmt`Antti Keränenbefore adoption049b9ec

line 97why, one step back
97 _ => atty::is(Stream::Stdout),

Add flag to force colorAntti Keränenbefore adoption4b61bea

line 98why, one step back
98 };

Move 'win' crate back into local modulesharkdpbefore adoptionb3f1e18

line 99why, one step back
99

Auto disable colored output for unsupported Windows shellsJ.Wbefore adoptionce73c08

line 100why, one step back
100 #[cfg(windows)]

jakwings/fd@a44e677 -> subdirectory "win"J.Wbefore adoption570ea78

line 101why, one step back
101 let colored_output = colored_output && windows::enable_colored_output();

Restructure according to Cargo docsharkdpbefore adoption0f5ede4

line 102why, one step back
102

Format the source code using `rustfmt`Antti Keränenbefore adoption049b9ec

lines 103–112why, one step back
103 let ls_colors = if colored_output {
104 Some(105 env::var("LS_COLORS")106 .ok()107 .map(|val| LsColors::from_string(&val))108 .unwrap_or_default(),109 )110 } else {111 None112 };

Restructure according to Cargo docsharkdpbefore adoption0f5ede4

line 113why, one step back
113

Updates and preparations for v5.0sharkdpbefore adoptione9cf8af

line 114why, one step back
114 let command = matches.value_of("exec").map(|x| TokenizedCommand::new(x));

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

line 115why, one step back
115

Restructure according to Cargo docsharkdpbefore adoption0f5ede4

line 116why, one step back
116 let config = FdOptions {

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

line 117why, one step back
117 case_sensitive,

Format the source code using `rustfmt`Antti Keränenbefore adoption049b9ec

lines 118–138why, one step back
118 search_full_path: matches.is_present("full-path"),
119 ignore_hidden: !(matches.is_present("hidden") ||120 matches.occurrences_of("rg-alias-hidden-ignore") >= 2),121 read_ignore: !(matches.is_present("no-ignore") ||122 matches.is_present("rg-alias-hidden-ignore")),123 follow_links: matches.is_present("follow"),124 null_separator: matches.is_present("null_separator"),125 max_depth: matches.value_of("depth").and_then(|n| {126 usize::from_str_radix(n, 10).ok()127 }),128 threads: std::cmp::max(129 matches130 .value_of("threads")131 .and_then(|n| usize::from_str_radix(n, 10).ok())132 .unwrap_or_else(num_cpus::get),133 1,134 ),135 max_buffer_time: matches136 .value_of("max-buffer-time")137 .and_then(|n| u64::from_str_radix(n, 10).ok())138 .map(time::Duration::from_millis),

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 139–140why, one step back
139 path_display,
140 ls_colors,

Format the source code using `rustfmt`Antti Keränenbefore adoption049b9ec

lines 141–150why, one step back
141 file_type: match matches.value_of("file-type") {
142 Some("f") | Some("file") => FileType::RegularFile,143 Some("d") |144 Some("directory") => FileType::Directory,145 Some("l") | Some("symlink") => FileType::SymLink,146 _ => FileType::Any,147 },148 extension: matches.value_of("extension").map(|e| {149 e.trim_left_matches('.').to_lowercase()150 }),

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

line 151why, one step back
151 command,

Add support for exclude-patternssharkdpbefore adoption5ad69fb

lines 152–154why, one step back
152 exclude_patterns: matches
153 .values_of("exclude")154 .map(|v| v.map(|p| String::from("!") + p).collect())

Fix some clippy warningssharkdpbefore adoptionbba5c4f

line 155why, one step back
155 .unwrap_or_else(|| vec![]),

Restructure according to Cargo docsharkdpbefore adoption0f5ede4

lines 156–158why, one step back
156 };
157158 match RegexBuilder::new(pattern)

Format the source code using `rustfmt`Antti Keränenbefore adoption049b9ec

line 159why, one step back
159 .case_insensitive(!config.case_sensitive)

Dot "." also matches newline by default (closes #111)J.Wbefore adoptionf2632d5

line 160why, one step back
160 .dot_matches_new_line(true)

Format the source code using `rustfmt`Antti Keränenbefore adoption049b9ec

line 161why, one step back
161 .build() {

Refactor path handling (fixes #113)J.Wbefore adoption8d85deb

line 162why, one step back
162 Ok(re) => walk::scan(root_dir, Arc::new(re), Arc::new(config)),

Format the source code using `rustfmt`Antti Keränenbefore adoption049b9ec

line 163why, one step back
163 Err(err) => error(err.description()),

Restructure according to Cargo docsharkdpbefore adoption0f5ede4

lines 164–165why, one step back
164 }
165}