mirror of https://github.com/sharkdp/fd.git
Add print_error macros to replace functions
This patch replaces the `print_error` and `print_error_and_exit` functions with equivalent macros.
This commit is contained in:
parent
abe8aa55c0
commit
984f04f142
|
@ -30,10 +30,10 @@ pub fn execute_command(mut cmd: Command, out_perm: Arc<Mutex<()>>) {
|
||||||
let _ = stderr.lock().write_all(&output.stderr);
|
let _ = stderr.lock().write_all(&output.stderr);
|
||||||
}
|
}
|
||||||
Err(ref why) if why.kind() == io::ErrorKind::NotFound => {
|
Err(ref why) if why.kind() == io::ErrorKind::NotFound => {
|
||||||
eprintln!("fd: execution error: command not found");
|
print_error!("fd: execution error: command not found");
|
||||||
}
|
}
|
||||||
Err(why) => {
|
Err(why) => {
|
||||||
eprintln!("fd: execution error: {}", why);
|
print_error!("fd: execution error: {}", why);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,6 @@
|
||||||
// according to those terms.
|
// according to those terms.
|
||||||
|
|
||||||
use super::CommandTemplate;
|
use super::CommandTemplate;
|
||||||
use internal::print_error;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::sync::mpsc::Receiver;
|
use std::sync::mpsc::Receiver;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
@ -30,7 +29,7 @@ pub fn job(
|
||||||
let value: PathBuf = match lock.recv() {
|
let value: PathBuf = match lock.recv() {
|
||||||
Ok(WorkerResult::Entry(val)) => val,
|
Ok(WorkerResult::Entry(val)) => val,
|
||||||
Ok(WorkerResult::Error(err)) => {
|
Ok(WorkerResult::Error(err)) => {
|
||||||
print_error(&format!("{}", err));
|
print_error!("{}", err);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Err(_) => break,
|
Err(_) => break,
|
||||||
|
|
|
@ -8,7 +8,6 @@
|
||||||
|
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process;
|
|
||||||
use std::time::{self, SystemTime};
|
use std::time::{self, SystemTime};
|
||||||
|
|
||||||
use exec::CommandTemplate;
|
use exec::CommandTemplate;
|
||||||
|
@ -199,15 +198,15 @@ pub struct FdOptions {
|
||||||
pub time_constraints: Vec<TimeFilter>,
|
pub time_constraints: Vec<TimeFilter>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Print error message to stderr.
|
macro_rules! print_error {
|
||||||
pub fn print_error(message: &str) {
|
($($arg:tt)*) => (eprintln!($($arg)*))
|
||||||
eprintln!("{}", message);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Print error message to stderr and exit with status `1`.
|
macro_rules! print_error_and_exit {
|
||||||
pub fn print_error_and_exit(message: &str) -> ! {
|
($($arg:tt)*) => {
|
||||||
print_error(message);
|
print_error!($($arg)*);
|
||||||
process::exit(1);
|
::std::process::exit(1);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Determine if a regex pattern contains a literal uppercase character.
|
/// Determine if a regex pattern contains a literal uppercase character.
|
||||||
|
|
37
src/main.rs
37
src/main.rs
|
@ -20,11 +20,13 @@ extern crate num_cpus;
|
||||||
extern crate regex;
|
extern crate regex;
|
||||||
extern crate regex_syntax;
|
extern crate regex_syntax;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
mod internal;
|
||||||
|
|
||||||
mod app;
|
mod app;
|
||||||
mod exec;
|
mod exec;
|
||||||
mod exit_codes;
|
mod exit_codes;
|
||||||
pub mod fshelper;
|
pub mod fshelper;
|
||||||
mod internal;
|
|
||||||
pub mod lscolors;
|
pub mod lscolors;
|
||||||
mod output;
|
mod output;
|
||||||
mod walk;
|
mod walk;
|
||||||
|
@ -40,8 +42,8 @@ use regex::{RegexBuilder, RegexSetBuilder};
|
||||||
|
|
||||||
use exec::CommandTemplate;
|
use exec::CommandTemplate;
|
||||||
use internal::{
|
use internal::{
|
||||||
pattern_has_uppercase_char, print_error_and_exit, transform_args_with_exec, FdOptions,
|
pattern_has_uppercase_char, transform_args_with_exec, FdOptions, FileTypes, SizeFilter,
|
||||||
FileTypes, SizeFilter, TimeFilter,
|
TimeFilter,
|
||||||
};
|
};
|
||||||
use lscolors::LsColors;
|
use lscolors::LsColors;
|
||||||
|
|
||||||
|
@ -55,7 +57,7 @@ fn main() {
|
||||||
// Get the current working directory
|
// Get the current working directory
|
||||||
let current_dir = Path::new(".");
|
let current_dir = Path::new(".");
|
||||||
if !fshelper::is_dir(current_dir) {
|
if !fshelper::is_dir(current_dir) {
|
||||||
print_error_and_exit("Error: could not get current directory.");
|
print_error_and_exit!("Error: could not get current directory.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get one or more root directories to search.
|
// Get one or more root directories to search.
|
||||||
|
@ -64,10 +66,10 @@ fn main() {
|
||||||
.map(|path| {
|
.map(|path| {
|
||||||
let path_buffer = PathBuf::from(path);
|
let path_buffer = PathBuf::from(path);
|
||||||
if !fshelper::is_dir(&path_buffer) {
|
if !fshelper::is_dir(&path_buffer) {
|
||||||
print_error_and_exit(&format!(
|
print_error_and_exit!(
|
||||||
"Error: '{}' is not a directory.",
|
"Error: '{}' is not a directory.",
|
||||||
path_buffer.to_string_lossy()
|
path_buffer.to_string_lossy()
|
||||||
));
|
);
|
||||||
}
|
}
|
||||||
path_buffer
|
path_buffer
|
||||||
})
|
})
|
||||||
|
@ -92,7 +94,7 @@ fn main() {
|
||||||
&& pattern.contains(std::path::MAIN_SEPARATOR)
|
&& pattern.contains(std::path::MAIN_SEPARATOR)
|
||||||
&& fshelper::is_dir(Path::new(pattern))
|
&& fshelper::is_dir(Path::new(pattern))
|
||||||
{
|
{
|
||||||
print_error_and_exit(&format!(
|
print_error_and_exit!(
|
||||||
"Error: The search pattern '{pattern}' contains a path-separation character ('{sep}') \
|
"Error: The search pattern '{pattern}' contains a path-separation character ('{sep}') \
|
||||||
and will not lead to any search results.\n\n\
|
and will not lead to any search results.\n\n\
|
||||||
If you want to search for all files inside the '{pattern}' directory, use a match-all pattern:\n\n \
|
If you want to search for all files inside the '{pattern}' directory, use a match-all pattern:\n\n \
|
||||||
|
@ -101,7 +103,7 @@ fn main() {
|
||||||
fd --full-path '{pattern}'",
|
fd --full-path '{pattern}'",
|
||||||
pattern = pattern,
|
pattern = pattern,
|
||||||
sep = std::path::MAIN_SEPARATOR,
|
sep = std::path::MAIN_SEPARATOR,
|
||||||
));
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Treat pattern as literal string if '--fixed-strings' is used
|
// Treat pattern as literal string if '--fixed-strings' is used
|
||||||
|
@ -145,7 +147,7 @@ fn main() {
|
||||||
if let Some(f) = SizeFilter::from_string(sf) {
|
if let Some(f) = SizeFilter::from_string(sf) {
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
print_error_and_exit(&format!("Error: {} is not a valid size constraint.", sf));
|
print_error_and_exit!("Error: {} is not a valid size constraint.", sf);
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
})
|
})
|
||||||
|
@ -157,14 +159,14 @@ fn main() {
|
||||||
if let Some(f) = TimeFilter::after(&now, t) {
|
if let Some(f) = TimeFilter::after(&now, t) {
|
||||||
time_constraints.push(f);
|
time_constraints.push(f);
|
||||||
} else {
|
} else {
|
||||||
print_error_and_exit(&format!("Error: {} is not a valid time.", t));
|
print_error_and_exit!("Error: {} is not a valid time.", t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(t) = matches.value_of("changed-before") {
|
if let Some(t) = matches.value_of("changed-before") {
|
||||||
if let Some(f) = TimeFilter::before(&now, t) {
|
if let Some(f) = TimeFilter::before(&now, t) {
|
||||||
time_constraints.push(f);
|
time_constraints.push(f);
|
||||||
} else {
|
} else {
|
||||||
print_error_and_exit(&format!("Error: {} is not a valid time.", t));
|
print_error_and_exit!("Error: {} is not a valid time.", t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -231,7 +233,9 @@ fn main() {
|
||||||
.build()
|
.build()
|
||||||
{
|
{
|
||||||
Ok(re) => re,
|
Ok(re) => re,
|
||||||
Err(err) => print_error_and_exit(err.description()),
|
Err(err) => {
|
||||||
|
print_error_and_exit!("{}", err.description());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
command,
|
command,
|
||||||
|
@ -253,13 +257,12 @@ fn main() {
|
||||||
.build()
|
.build()
|
||||||
{
|
{
|
||||||
Ok(re) => walk::scan(&dir_vec, Arc::new(re), Arc::new(config)),
|
Ok(re) => walk::scan(&dir_vec, Arc::new(re), Arc::new(config)),
|
||||||
Err(err) => print_error_and_exit(
|
Err(err) => {
|
||||||
format!(
|
print_error_and_exit!(
|
||||||
"{}\nHint: You can use the '--fixed-strings' option to search for a \
|
"{}\nHint: You can use the '--fixed-strings' option to search for a \
|
||||||
literal string instead of a regular expression",
|
literal string instead of a regular expression",
|
||||||
err.description()
|
err.description()
|
||||||
)
|
);
|
||||||
.as_str(),
|
}
|
||||||
),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
25
src/walk.rs
25
src/walk.rs
|
@ -11,7 +11,7 @@ extern crate ctrlc;
|
||||||
use exec;
|
use exec;
|
||||||
use exit_codes::ExitCode;
|
use exit_codes::ExitCode;
|
||||||
use fshelper;
|
use fshelper;
|
||||||
use internal::{print_error, print_error_and_exit, FdOptions, MAX_BUFFER_LENGTH};
|
use internal::{FdOptions, MAX_BUFFER_LENGTH};
|
||||||
use output;
|
use output;
|
||||||
|
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
@ -61,11 +61,11 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
|
||||||
for pattern in &config.exclude_patterns {
|
for pattern in &config.exclude_patterns {
|
||||||
let res = override_builder.add(pattern);
|
let res = override_builder.add(pattern);
|
||||||
if res.is_err() {
|
if res.is_err() {
|
||||||
print_error_and_exit(&format!("Error: malformed exclude pattern '{}'", pattern));
|
print_error_and_exit!("Error: malformed exclude pattern '{}'", pattern);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let overrides = override_builder.build().unwrap_or_else(|_| {
|
let overrides = override_builder.build().unwrap_or_else(|_| {
|
||||||
print_error_and_exit("Mismatch in exclude patterns");
|
print_error_and_exit!("Mismatch in exclude patterns");
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut walker = WalkBuilder::new(first_path_buf.as_path());
|
let mut walker = WalkBuilder::new(first_path_buf.as_path());
|
||||||
|
@ -90,11 +90,14 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
|
||||||
match err {
|
match err {
|
||||||
ignore::Error::Partial(_) => (),
|
ignore::Error::Partial(_) => (),
|
||||||
_ => {
|
_ => {
|
||||||
print_error(&format!(
|
print_error!(
|
||||||
"Error while parsing custom ignore file '{}': {}.",
|
"{}",
|
||||||
ignore_file.to_string_lossy(),
|
format!(
|
||||||
err.description()
|
"Error while parsing custom ignore file '{}': {}.",
|
||||||
));
|
ignore_file.to_string_lossy(),
|
||||||
|
err.description()
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -190,7 +193,7 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
WorkerResult::Error(err) => {
|
WorkerResult::Error(err) => {
|
||||||
print_error(&format!("{}", err));
|
print_error!("{}", err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -310,7 +313,9 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
|
||||||
let search_str_o = if config.search_full_path {
|
let search_str_o = if config.search_full_path {
|
||||||
match fshelper::path_absolute_form(entry_path) {
|
match fshelper::path_absolute_form(entry_path) {
|
||||||
Ok(path_abs_buf) => Some(path_abs_buf.to_string_lossy().into_owned().into()),
|
Ok(path_abs_buf) => Some(path_abs_buf.to_string_lossy().into_owned().into()),
|
||||||
Err(_) => print_error_and_exit("Error: unable to get full path."),
|
Err(_) => {
|
||||||
|
print_error_and_exit!("Error: unable to get full path.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
entry_path.file_name().map(|f| f.to_string_lossy())
|
entry_path.file_name().map(|f| f.to_string_lossy())
|
||||||
|
|
Loading…
Reference in New Issue