From e399398444cee54525f47ad45c514b7dcc559d2a Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Fri, 29 Nov 2019 12:43:29 +0100 Subject: [PATCH] remove shellwords crate --- Cargo.toml | 1 - src/cli/command.rs | 20 +---------------- src/cli/completion.rs | 52 ++++++++++++++++++++++++++++--------------- 3 files changed, 35 insertions(+), 38 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9ea95766..42c4bbcc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,7 +33,6 @@ regex = "1.0" rustyline = "5.0.4" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -shellwords = "1.0" siphasher = "0.3" syslog = "4.0" tokio = { version = "0.2.0-alpha.4" } diff --git a/src/cli/command.rs b/src/cli/command.rs index 2c611c97..ba4704be 100644 --- a/src/cli/command.rs +++ b/src/cli/command.rs @@ -314,25 +314,7 @@ pub fn print_bash_completion(def: &CommandLineInterface) { Err(_) => return, }; - let mut args = match shellwords::split(&cmdline) { - Ok(v) => v, - Err(_) => return, - }; - - if args.len() == 0 { return; } - - args.remove(0); //no need for program name - - if cmdline.ends_with(char::is_whitespace) { - //eprintln!("CMDLINE {:?}", cmdline); - args.push("".into()); - } - - let completions = if !args.is_empty() && args[0] == "help" { - get_help_completion(def, &help_command_def(), &args[1..]) - } else { - get_nested_completion(def, &args) - }; + let (_start, completions) = super::get_completions(def, &cmdline, true); for item in completions { println!("{}", item); diff --git a/src/cli/completion.rs b/src/cli/completion.rs index 59d171bf..78e38e58 100644 --- a/src/cli/completion.rs +++ b/src/cli/completion.rs @@ -172,6 +172,39 @@ fn test_shellword_split_unclosed() { ); } +pub fn get_completions( + cmd_def: &CommandLineInterface, + line: &str, + skip_first: bool, +) -> (usize, Vec) { + + let (mut args, start ) = match shellword_split_unclosed(line, false) { + (mut args, None) => { + args.push("".into()); + (args, line.len()) + } + (mut args, Some((start , arg, _quote))) => { + args.push(arg); + (args, start) + } + }; + + if skip_first { + + if args.len() == 0 { return (0, Vec::new()); } + + args.remove(0); // no need for program name + } + + let completions = if !args.is_empty() && args[0] == "help" { + get_help_completion(cmd_def, &help_command_def(), &args[1..]) + } else { + get_nested_completion(cmd_def, &args) + }; + + (start, completions) +} + pub struct CliHelper { cmd_def: CommandLineInterface, } @@ -199,24 +232,7 @@ impl rustyline::completion::Completer for CliHelper { let line = &line[..pos]; - let (args, start ) = match shellword_split_unclosed(line, false) { - (mut args, None) => { - args.push("".into()); - (args, pos) - } - (mut args, Some((start , arg, _quote))) => { - args.push(arg); - (args, start) - } - }; - - let def = &self.cmd_def; - - let completions = if !args.is_empty() && args[0] == "help" { - get_help_completion(&def, &help_command_def(), &args[1..]) - } else { - get_nested_completion(&def, &args) - }; + let (start, completions) = super::get_completions(&self.cmd_def, line, false); return Ok((start, completions)); }