Skip to content
Myr

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

4 files+61 added−66 removed

modified:src/exec/job.rs+2 added-6 removed
8 unmodified lines
9use std::path::PathBuf;10use std::sync::{Arc, Mutex};11use std::sync::mpsc::Receiver;1213use super::TokenizedCommand;13use super::CommandTemplate;1415/// 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.18pub fn job(19 rx: Arc<Mutex<Receiver<PathBuf>>>,20 cmd: Arc<TokenizedCommand>,21 out_perm: Arc<Mutex<()>>,22) {18pub 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 lines
modified:src/exec/mod.rs+55 added-56 removed
22 unmodified lines
23use self::command::execute_command;24use self::token::Token;25pub use self::job::job;2627/// 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)]33pub struct TokenizedCommand {34 args: Vec<TokenizedArgument>,32pub struct CommandTemplate {33 args: Vec<ArgumentTemplate>,34}3537/// 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)]42enum TokenizedArgument {43 Tokens(Vec<Token>),44 Text(String),45}4647impl TokenizedArgument {48 pub fn generate<'a>(&'a self, path: &str) -> Cow<'a, str> {49 use self::Token::*;5051 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}7071impl TokenizedCommand {72 pub fn new<I, S>(input: I) -> TokenizedCommand36impl CommandTemplate {37 pub fn new<I, S>(input: I) -> CommandTemplate38 where39 I: IntoIterator<Item = S>,40 S: AsRef<str>,41 {
31 unmodified lines
73 }7475 // 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 }8081 if start < arg.len() {82 // Trailing text after last placeholder.83 tokens.push(Token::Text(arg[start..].to_owned()));84 }85121 args.push(TokenizedArgument::Tokens(tokens));86 args.push(ArgumentTemplate::Tokens(tokens));87 }8889 // 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 }93129 TokenizedCommand { args: args }94 CommandTemplate { args: args }95 }9697 /// Generates and executes a command.98 ///
14 unmodified lines
113 execute_command(cmd, out_perm)114 }115}116117/// 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)]122enum ArgumentTemplate {123 Tokens(Vec<Token>),124 Text(String),125}126127impl ArgumentTemplate {128 pub fn generate<'a>(&'a self, path: &str) -> Cow<'a, str> {129 use self::Token::*;130131 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}150151#[cfg(test)]152mod tests {154 use super::{TokenizedCommand, TokenizedArgument, Token};153 use super::{CommandTemplate, ArgumentTemplate, Token};154155 #[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 };164166 assert_eq!(TokenizedCommand::new(&[&"echo", &"${SHELL}:"]), expected);165 assert_eq!(CommandTemplate::new(&[&"echo", &"${SHELL}:"]), expected);166167 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 line
modified:src/internal.rs+2 added-2 removed
8 unmodified lines
9use std::process;10use std::time;11use std::io::Write;1213use exec::TokenizedCommand;13use exec::CommandTemplate;14use lscolors::LsColors;15use walk::FileType;16use regex_syntax::{Expr, ExprBuilder};17
57 unmodified lines
75 /// The value (if present) will be a lowercase string without leading dots.76 pub extension: Option<String>,7778 /// 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>,8081 /// A list of glob patterns that should be excluded from the search.82 pub exclude_patterns: Vec<String>,83}
31 unmodified lines
modified:src/main.rs+2 added-2 removed
37 unmodified lines
3839use atty::Stream;40use regex::RegexBuilder;4142use exec::TokenizedCommand;42use exec::CommandTemplate;43use internal::{error, pattern_has_uppercase_char, FdOptions, PathDisplay};44use lscolors::LsColors;45use walk::FileType;46
57 unmodified lines
104 } else {105 None106 };107108 let command = matches.values_of("exec").map(TokenizedCommand::new);108 let command = matches.values_of("exec").map(CommandTemplate::new);109110 let config = FdOptions {111 case_sensitive,112 search_full_path: matches.is_present("full-path"),
47 unmodified lines