Rename TokenizedCommand to CommandTemplate
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Matthias Reitinger · committed by David Peter · parent c18caaa · Browse files · 4 files·+61 added−66 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+61 added−66 removed
- other:+61 added−66 removed
Provenance
- Commit
0f82fe7- Tree
34346d9- Author
Matthias Reitinger <reima@edenprime.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
c18caaa
4 files changed+61 added−66 removed
modified:src/exec/job.rs+2 added-6 removed
8 unmodified lines9 use std::path::PathBuf;10 use std::sync::{Arc, Mutex};11 use std::sync::mpsc::Receiver;12 13-use super::TokenizedCommand;13+use super::CommandTemplate;14 15 /// An event loop that listens for inputs from the `rx` receiver. Each received input will16 /// generate a command with the supplied command template. The generated command will then17 /// be executed, and this process will continue until the receiver's sender has closed.18-pub fn job(19- rx: Arc<Mutex<Receiver<PathBuf>>>,20- cmd: Arc<TokenizedCommand>,21- out_perm: Arc<Mutex<()>>,22-) {18+pub fn job(rx: Arc<Mutex<Receiver<PathBuf>>>, cmd: Arc<CommandTemplate>, out_perm: Arc<Mutex<()>>) {19 loop {20 // Create a lock on the shared receiver for this thread.21 let lock = rx.lock().unwrap();22 13 unmodified linesmodified:src/exec/mod.rs+55 added-56 removed
22 unmodified lines23 use self::command::execute_command;24 use self::token::Token;25 pub use self::job::job;26 27-/// Contains a collection of `TokenizedArgument`s that are utilized to generate command strings.27+/// Represents a template that is utilized to generate command strings.28 ///29-/// The arguments are a representation of the supplied command template, and are meant to be coupled30-/// with an input in order to generate a command. The `generate_and_execute()` method will be used31-/// to generate a command and execute it.29+/// The template is meant to be coupled with an input in order to generate a command. The30+/// `generate_and_execute()` method will be used to generate a command and execute it.31 #[derive(Debug, Clone, PartialEq)]33-pub struct TokenizedCommand {34- args: Vec<TokenizedArgument>,32+pub struct CommandTemplate {33+ args: Vec<ArgumentTemplate>,34 }35 37-/// Represents a single command argument.38-///39-/// The argument is either a collection of `Token`s including at least one placeholder variant,40-/// or a fixed text.41-#[derive(Clone, Debug, PartialEq)]42-enum TokenizedArgument {43- Tokens(Vec<Token>),44- Text(String),45-}46-47-impl TokenizedArgument {48- pub fn generate<'a>(&'a self, path: &str) -> Cow<'a, str> {49- use self::Token::*;50-51- match *self {52- TokenizedArgument::Tokens(ref tokens) => {53- let mut s = String::new();54- for token in tokens {55- match *token {56- Basename => s += basename(path),57- BasenameNoExt => s += remove_extension(basename(path)),58- NoExt => s += remove_extension(path),59- Parent => s += dirname(path),60- Placeholder => s += path,61- Text(ref string) => s += string,62- }63- }64- Cow::Owned(s)65- }66- TokenizedArgument::Text(ref text) => Cow::Borrowed(text),67- }68- }69-}70-71-impl TokenizedCommand {72- pub fn new<I, S>(input: I) -> TokenizedCommand36+impl CommandTemplate {37+ pub fn new<I, S>(input: I) -> CommandTemplate38 where39 I: IntoIterator<Item = S>,40 S: AsRef<str>,41 {31 unmodified lines73 }74 75 // Without a placeholder, the argument is just fixed text.76 if tokens.is_empty() {112- args.push(TokenizedArgument::Text(arg.to_owned()));77+ args.push(ArgumentTemplate::Text(arg.to_owned()));78 continue;79 }80 81 if start < arg.len() {82 // Trailing text after last placeholder.83 tokens.push(Token::Text(arg[start..].to_owned()));84 }85 121- args.push(TokenizedArgument::Tokens(tokens));86+ args.push(ArgumentTemplate::Tokens(tokens));87 }88 89 // If a placeholder token was not supplied, append one at the end of the command.90 if !has_placeholder {126- args.push(TokenizedArgument::Tokens(vec![Token::Placeholder]));91+ args.push(ArgumentTemplate::Tokens(vec![Token::Placeholder]));92 }93 129- TokenizedCommand { args: args }94+ CommandTemplate { args: args }95 }96 97 /// Generates and executes a command.98 ///14 unmodified lines113 execute_command(cmd, out_perm)114 }115 }116 117+/// Represents a template for a single command argument.118+///119+/// The argument is either a collection of `Token`s including at least one placeholder variant, or120+/// a fixed text.121+#[derive(Clone, Debug, PartialEq)]122+enum ArgumentTemplate {123+ Tokens(Vec<Token>),124+ Text(String),125+}126+127+impl ArgumentTemplate {128+ pub fn generate<'a>(&'a self, path: &str) -> Cow<'a, str> {129+ use self::Token::*;130+131+ match *self {132+ ArgumentTemplate::Tokens(ref tokens) => {133+ let mut s = String::new();134+ for token in tokens {135+ match *token {136+ Basename => s += basename(path),137+ BasenameNoExt => s += remove_extension(basename(path)),138+ NoExt => s += remove_extension(path),139+ Parent => s += dirname(path),140+ Placeholder => s += path,141+ Text(ref string) => s += string,142+ }143+ }144+ Cow::Owned(s)145+ }146+ ArgumentTemplate::Text(ref text) => Cow::Borrowed(text),147+ }148+ }149+}150+151 #[cfg(test)]152 mod tests {154- use super::{TokenizedCommand, TokenizedArgument, Token};153+ use super::{CommandTemplate, ArgumentTemplate, Token};154 155 #[test]156 fn tokens() {158- let expected = TokenizedCommand {157+ let expected = CommandTemplate {158 args: vec![160- TokenizedArgument::Text("echo".into()),161- TokenizedArgument::Text("${SHELL}:".into()),162- TokenizedArgument::Tokens(vec![Token::Placeholder]),159+ ArgumentTemplate::Text("echo".into()),160+ ArgumentTemplate::Text("${SHELL}:".into()),161+ ArgumentTemplate::Tokens(vec![Token::Placeholder]),162 ],163 };164 166- assert_eq!(TokenizedCommand::new(&[&"echo", &"${SHELL}:"]), expected);165+ assert_eq!(CommandTemplate::new(&[&"echo", &"${SHELL}:"]), expected);166 167 assert_eq!(169- TokenizedCommand::new(&["echo", "{.}"]),170- TokenizedCommand {168+ CommandTemplate::new(&["echo", "{.}"]),169+ CommandTemplate {170 args: vec![172- TokenizedArgument::Text("echo".into()),173- TokenizedArgument::Tokens(vec![Token::NoExt]),171+ ArgumentTemplate::Text("echo".into()),172+ ArgumentTemplate::Tokens(vec![Token::NoExt]),173 ],174 }175 );176 }1 unmodified linemodified:src/internal.rs+2 added-2 removed
8 unmodified lines9 use std::process;10 use std::time;11 use std::io::Write;12 13-use exec::TokenizedCommand;13+use exec::CommandTemplate;14 use lscolors::LsColors;15 use walk::FileType;16 use regex_syntax::{Expr, ExprBuilder};17 57 unmodified lines75 /// The value (if present) will be a lowercase string without leading dots.76 pub extension: Option<String>,77 78 /// If a value is supplied, each item found will be used to generate and execute commands.79- pub command: Option<TokenizedCommand>,79+ pub command: Option<CommandTemplate>,80 81 /// A list of glob patterns that should be excluded from the search.82 pub exclude_patterns: Vec<String>,83 }31 unmodified linesmodified:src/main.rs+2 added-2 removed
37 unmodified lines38 39 use atty::Stream;40 use regex::RegexBuilder;41 42-use exec::TokenizedCommand;42+use exec::CommandTemplate;43 use internal::{error, pattern_has_uppercase_char, FdOptions, PathDisplay};44 use lscolors::LsColors;45 use walk::FileType;46 57 unmodified lines104 } else {105 None106 };107 108- let command = matches.values_of("exec").map(TokenizedCommand::new);108+ let command = matches.values_of("exec").map(CommandTemplate::new);109 110 let config = FdOptions {111 case_sensitive,112 search_full_path: matches.is_present("full-path"),47 unmodified lines