diff --git a/src/app.rs b/src/app.rs index 586e55a..7dc5bc0 100644 --- a/src/app.rs +++ b/src/app.rs @@ -95,12 +95,7 @@ pub fn build_app() -> App<'static, 'static> { .takes_value(true) .hidden(true), ) - .arg( - arg("exec") - .long("exec") - .short("x") - .takes_value(true) - ) + .arg(arg("exec").long("exec").short("x").takes_value(true)) .arg(arg("pattern")) .arg(arg("path")) } @@ -151,8 +146,10 @@ fn usage() -> HashMap<&'static str, Help> { 'l' or 'symlink': symbolic links"); doc!(h, "exec" , "Execute each discovered path using the argument that follows as the command expression." - , "Execute each discovered path using the argument that follows as the command expression.\n \ - The following are valid tokens that can be used within the expression for generating commands:\n \ + , "Execute each discovered path using the argument that follows as the command \ + expression.\n \ + The following are valid tokens that can be used within the expression for generating \ + commands:\n \ '{}': places the input in the location of this token\n \ '{.}': removes the extension from the input\n \ '{/}': places the basename of the input\n \ @@ -169,7 +166,8 @@ fn usage() -> HashMap<&'static str, Help> { 'always': always use colorized output"); doc!(h, "threads" , "Set number of threads to use for searching & executing" - , "Set number of threads to use for searching & executing (default: number of available CPU cores)"); + , "Set number of threads to use for searching & executing (default: number of available \ + CPU cores)"); doc!(h, "max-buffer-time" , "the time (in ms) to buffer, before streaming to the console" , "Amount of time in milliseconds to buffer, before streaming the search results to\ diff --git a/src/exec/command.rs b/src/exec/command.rs index 6926118..9493e3f 100644 --- a/src/exec/command.rs +++ b/src/exec/command.rs @@ -36,7 +36,9 @@ impl<'a> CommandTicket<'a> { // Then wait for the command to exit, if it was spawned. match cmd { - Ok(mut child) => { let _ = child.wait(); }, + Ok(mut child) => { + let _ = child.wait(); + } Err(why) => eprintln!("fd: exec error: {}", why), } diff --git a/src/exec/job.rs b/src/exec/job.rs index 4d861ea..205567e 100644 --- a/src/exec/job.rs +++ b/src/exec/job.rs @@ -19,7 +19,7 @@ pub fn job(rx: Arc>>, cmd: Arc) { // has closed, exit from the loop let value = match lock.recv() { Ok(value) => value, - Err(_) => break + Err(_) => break, }; // Drop the lock so that other threads can read from the the receiver. @@ -29,4 +29,4 @@ pub fn job(rx: Arc>>, cmd: Arc) { // Note that the `then_execute()` method will clear the buffer for us. cmd.generate(buffer, &value).then_execute(); } -} \ No newline at end of file +} diff --git a/src/exec/mod.rs b/src/exec/mod.rs index eca8190..460ba9a 100644 --- a/src/exec/mod.rs +++ b/src/exec/mod.rs @@ -52,12 +52,14 @@ impl TokenizedCommand { match character { // Backslashes are useful in cases where we want to use the '{' character // without having all occurrences of it to collect placeholder tokens. - '\\' => if let Some((_, nchar)) = chars.next() { - if nchar != '{' { - text.push(character); + '\\' => { + if let Some((_, nchar)) = chars.next() { + if nchar != '{' { + text.push(character); + } + text.push(nchar); } - text.push(nchar); - }, + } // When a raw '{' is discovered, we will note it's position, and use that for a // later comparison against valid placeholder tokens. '{' if flags & OPEN == 0 => { @@ -67,34 +69,36 @@ impl TokenizedCommand { append(&mut tokens, &text); text.clear(); } - }, + } // If the `OPEN` bit is set, we will compare the contents between the discovered // '{' and '}' characters against a list of valid tokens, then pushing the // corresponding token onto the `tokens` vector. '}' if flags & OPEN != 0 => { flags ^= OPEN; - match &input[start+1..id] { - "" => tokens.push(Token::Placeholder), - "." => tokens.push(Token::NoExt), - "/" => tokens.push(Token::Basename), + match &input[start + 1..id] { + "" => tokens.push(Token::Placeholder), + "." => tokens.push(Token::NoExt), + "/" => tokens.push(Token::Basename), "//" => tokens.push(Token::Parent), "/." => tokens.push(Token::BasenameNoExt), _ => { - append(&mut tokens, &input[start..id+1]); - continue - }, + append(&mut tokens, &input[start..id + 1]); + continue; + } } flags |= PLACE; - }, + } // We aren't collecting characters for a text string if the `OPEN` bit is set. _ if flags & OPEN != 0 => (), // Push the character onto the text buffer - _ => text.push(character) + _ => text.push(character), } } // Take care of any stragglers left behind. - if !text.is_empty() { append(&mut tokens, &text); } + if !text.is_empty() { + append(&mut tokens, &text); + } // If a placeholder token was not supplied, append one at the end of the command. if flags & PLACE == 0 { @@ -115,7 +119,9 @@ impl TokenizedCommand { for token in &self.tokens { match *token { Token::Basename => *command += basename(&input.to_string_lossy()), - Token::BasenameNoExt => *command += remove_extension(basename(&input.to_string_lossy())), + Token::BasenameNoExt => { + *command += remove_extension(basename(&input.to_string_lossy())) + } Token::NoExt => *command += remove_extension(&input.to_string_lossy()), Token::Parent => *command += dirname(&input.to_string_lossy()), Token::Placeholder => *command += &input.to_string_lossy(), @@ -148,11 +154,11 @@ impl Display for TokenizedCommand { fn fmt(&self, f: &mut Formatter) -> fmt::Result { for token in &self.tokens { match *token { - Token::Placeholder => f.write_str("{}")?, - Token::Basename => f.write_str("{/}")?, - Token::Parent => f.write_str("{//}")?, - Token::NoExt => f.write_str("{.}")?, - Token::BasenameNoExt => f.write_str("{/.}")?, + Token::Placeholder => f.write_str("{}")?, + Token::Basename => f.write_str("{/}")?, + Token::Parent => f.write_str("{//}")?, + Token::NoExt => f.write_str("{.}")?, + Token::BasenameNoExt => f.write_str("{/.}")?, Token::Text(ref string) => f.write_str(string)?, } } @@ -167,19 +173,14 @@ mod tests { #[test] fn tokens() { let expected = TokenizedCommand { - tokens: vec![ - Token::Text("echo ${SHELL}: ".into()), - Token::Placeholder, - ], + tokens: vec![Token::Text("echo ${SHELL}: ".into()), Token::Placeholder], }; assert_eq!(TokenizedCommand::new("echo $\\{SHELL}: {}"), expected); assert_eq!(TokenizedCommand::new("echo ${SHELL}:"), expected); - assert_eq!(TokenizedCommand::new("echo {.}"), TokenizedCommand { - tokens: vec![ - Token::Text("echo ".into()), - Token::NoExt, - ], - }); + assert_eq!( + TokenizedCommand::new("echo {.}"), + TokenizedCommand { tokens: vec![Token::Text("echo ".into()), Token::NoExt] } + ); } -} \ No newline at end of file +} diff --git a/src/exec/paths.rs b/src/exec/paths.rs index 18a1359..11822f3 100644 --- a/src/exec/paths.rs +++ b/src/exec/paths.rs @@ -1,9 +1,15 @@ pub fn basename(input: &str) -> &str { let mut index = 0; for (id, character) in input.bytes().enumerate() { - if character == b'/' { index = id; } + if character == b'/' { + index = id; + } + } + if index == 0 { + input + } else { + &input[index + 1..] } - if index == 0 { input } else { &input[index+1..] } } /// Removes the extension of a given input @@ -12,18 +18,28 @@ pub fn remove_extension(input: &str) -> &str { let mut ext_index = 0; for (id, character) in input.bytes().enumerate() { - if character == b'/' { dir_index = id; } - if character == b'.' { ext_index = id; } + if character == b'/' { + dir_index = id; + } + if character == b'.' { + ext_index = id; + } } // Account for hidden files and directories - if ext_index == 0 || dir_index + 2 > ext_index { input } else { &input[0..ext_index] } + if ext_index == 0 || dir_index + 2 > ext_index { + input + } else { + &input[0..ext_index] + } } pub fn dirname(input: &str) -> &str { let mut index = 0; for (id, character) in input.bytes().enumerate() { - if character == b'/' { index = id; } + if character == b'/' { + index = id; + } } if index == 0 { "." } else { &input[0..index] } } diff --git a/src/internal.rs b/src/internal.rs index 871d1ce..3c73915 100644 --- a/src/internal.rs +++ b/src/internal.rs @@ -75,7 +75,7 @@ pub struct FdOptions { pub extension: Option, /// If a value is supplied, each item found will be used to generate and execute commands. - pub command: Option + pub command: Option, } /// Print error message to stderr and exit with status `1`. diff --git a/src/main.rs b/src/main.rs index e8f455b..d52b856 100644 --- a/src/main.rs +++ b/src/main.rs @@ -90,8 +90,7 @@ fn main() { None }; - let command = matches.value_of("exec") - .map(|x| TokenizedCommand::new(&x)); + let command = matches.value_of("exec").map(|x| TokenizedCommand::new(&x)); let config = FdOptions { case_sensitive, @@ -132,7 +131,7 @@ fn main() { extension: matches.value_of("extension").map(|e| { e.trim_left_matches('.').to_lowercase() }), - command + command, }; let root = Path::new(ROOT_DIR); diff --git a/src/walk.rs b/src/walk.rs index 358f868..9065e6a 100644 --- a/src/walk.rs +++ b/src/walk.rs @@ -70,7 +70,7 @@ pub fn scan(root: &Path, pattern: Arc, base: &Path, config: Arc, base: &Path, config: Arc