run cargo fmt

This commit is contained in:
Ingolf Wagner 2020-03-14 20:33:07 +08:00
parent 2d86b64683
commit af6fba3c31
No known key found for this signature in database
GPG key ID: 76BF5F1928B9618B
3 changed files with 68 additions and 58 deletions

View file

@ -12,10 +12,10 @@ pub struct SuggestionOpts {
pub header_lines: u8,
pub column: Option<u8>,
pub delimiter: Option<String>,
pub suggestion_type : SuggestionType,
pub suggestion_type: SuggestionType,
}
#[derive(Clone,Copy,Debug,PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum SuggestionType {
Disabled,
SingleSelection,
@ -65,13 +65,12 @@ fn parse_opts(text: &str) -> SuggestionOpts {
column,
delimiter,
suggestion_type: match (multi, allow_extra) {
(true,_) => SuggestionType::MultipleSelections , // multi wins over allow-extra
(false,true) => SuggestionType::SingleRecommendation,
(false,false) => SuggestionType::SingleSelection
(true, _) => SuggestionType::MultipleSelections, // multi wins over allow-extra
(false, true) => SuggestionType::SingleRecommendation,
(false, false) => SuggestionType::SingleSelection,
},
};
result
}
fn parse_variable_line(line: &str) -> (&str, &str, Option<SuggestionOpts>) {
@ -79,8 +78,8 @@ fn parse_variable_line(line: &str) -> (&str, &str, Option<SuggestionOpts>) {
let caps = re.captures(line).unwrap();
let variable = caps.get(1).unwrap().as_str().trim();
let mut command_plus_opts = caps.get(2).unwrap().as_str().split("---");
let command : &str = command_plus_opts.next().unwrap();
let command_option_string : Option<&str>= command_plus_opts.next();
let command: &str = command_plus_opts.next().unwrap();
let command_option_string: Option<&str> = command_plus_opts.next();
let command_options = command_option_string.map(parse_opts);
(variable, command, command_options)
}
@ -151,7 +150,10 @@ fn read_file(
}
}
pub fn read_all(config: &Config, stdin: &mut std::process::ChildStdin) -> HashMap<String, Suggestion> {
pub fn read_all(
config: &Config,
stdin: &mut std::process::ChildStdin,
) -> HashMap<String, Suggestion> {
let mut variables: HashMap<String, Suggestion> = HashMap::new();
let mut fallback: String = String::from("");
@ -184,39 +186,42 @@ mod tests {
#[test]
fn test_parse_variable_line() {
let( variable, command, command_options ) = parse_variable_line("$ user : echo -e \"$(whoami)\\nroot\" --- --allow-extra" );
let (variable, command, command_options) =
parse_variable_line("$ user : echo -e \"$(whoami)\\nroot\" --- --allow-extra");
assert_eq!(command, " echo -e \"$(whoami)\\nroot\" ");
assert_eq!(variable, "user");
assert_eq!(command_options, Some(SuggestionOpts{
assert_eq!(
command_options,
Some(SuggestionOpts {
header_lines: 0,
column: None,
delimiter: None,
suggestion_type: SuggestionType::SingleRecommendation
}));
})
);
}
use std::process::{Command, Stdio};
#[test]
fn test_read_file() {
let path = "tests/cheats/ssh.cheat";
let mut variables: HashMap<String, Suggestion> = HashMap::new();
let mut child = Command::new("cat")
.stdin(Stdio::piped())
.spawn().unwrap();
let mut child = Command::new("cat").stdin(Stdio::piped()).spawn().unwrap();
let child_stdin = child.stdin.as_mut().unwrap();
read_file(
path,
&mut variables,
child_stdin
);
let mut result : HashMap<String, (String, std::option::Option<_>)> = HashMap::new();
result.insert("ssh;user".to_string(),
(" echo -e \"$(whoami)\\nroot\" ".to_string(), Some(SuggestionOpts { header_lines: 0, column: None, delimiter: None, suggestion_type: SuggestionType::SingleRecommendation }))
read_file(path, &mut variables, child_stdin);
let mut result: HashMap<String, (String, std::option::Option<_>)> = HashMap::new();
result.insert(
"ssh;user".to_string(),
(
" echo -e \"$(whoami)\\nroot\" ".to_string(),
Some(SuggestionOpts {
header_lines: 0,
column: None,
delimiter: None,
suggestion_type: SuggestionType::SingleRecommendation,
}),
),
);
assert_eq!(variables, result);
}
}

View file

@ -4,13 +4,13 @@ use crate::display;
use crate::fzf;
use crate::option::Config;
use crate::cheat::SuggestionType;
use regex::Regex;
use std::collections::HashMap;
use std::error::Error;
use std::fs;
use std::io::Write;
use std::process::{Command, Stdio};
use crate::cheat::SuggestionType;
pub enum Variant {
Core,
@ -66,7 +66,7 @@ fn prompt_with_suggestions(
for (key, value) in values.iter() {
vars_cmd.push_str(format!("{}=\"{}\"; ", key, value).as_str());
}
let(suggestion_command, suggestion_options) = &suggestion;
let (suggestion_command, suggestion_options) = &suggestion;
let command = format!("{} {}", vars_cmd, suggestion_command);
let child = Command::new("bash")
@ -120,7 +120,7 @@ fn prompt_without_suggestions(variable_name: &str) -> String {
preview: false,
autoselect: false,
prompt: Some(display::variable_prompt(variable_name)),
suggestion_type : SuggestionType::Disabled,
suggestion_type: SuggestionType::Disabled,
..Default::default()
};
@ -155,14 +155,18 @@ fn replace_variables_from_snippet(
let key = format!("{};{}", tags, variable_name);
let value = match variables.get(&key[..]) {
Some(suggestion) => prompt_with_suggestions(variable_name, &config, suggestion, &values),
Some(suggestion) => {
prompt_with_suggestions(variable_name, &config, suggestion, &values)
}
None => prompt_without_suggestions(variable_name),
};
values.insert(variable_name.to_string(), value.clone());
interpolated_snippet = interpolated_snippet
.replace(bracketed_variable_name, gen_replacement(&value[..]).as_str());
interpolated_snippet = interpolated_snippet.replace(
bracketed_variable_name,
gen_replacement(&value[..]).as_str(),
);
}
}

View file

@ -2,11 +2,11 @@ use crate::cheat;
use crate::display;
use crate::filesystem;
use crate::cheat::SuggestionType;
use crate::cheat::SuggestionType::SingleSelection;
use std::collections::HashMap;
use std::process;
use std::process::{Command, Stdio};
use crate::cheat::SuggestionType;
use crate::cheat::SuggestionType::SingleSelection;
pub struct Opts<'a> {
pub query: Option<String>,
@ -16,10 +16,9 @@ pub struct Opts<'a> {
pub autoselect: bool,
pub overrides: Option<&'a String>, // TODO: remove &'a
pub header_lines: u8,
pub suggestion_type : SuggestionType,
pub suggestion_type: SuggestionType,
}
impl Default for Opts<'_> {
fn default() -> Self {
Self {
@ -59,14 +58,16 @@ where
}
match opts.suggestion_type {
SuggestionType::MultipleSelections => { fzf_command.arg("--multi"); },
SuggestionType::MultipleSelections => {
fzf_command.arg("--multi");
}
SuggestionType::Disabled => {
fzf_command.args(&["--print-query", "--no-select-1", "--height", "1"]);
},
SuggestionType::SnippetSelection =>{
}
SuggestionType::SnippetSelection => {
fzf_command.args(&["--expect", "ctrl-y,enter"]);
}
SuggestionType::SingleRecommendation =>{
SuggestionType::SingleRecommendation => {
fzf_command.args(&["--print-query", "--expect", "tab,enter"]);
}
_ => {}
@ -105,7 +106,6 @@ where
});
}
let child = fzf_command
.stdin(Stdio::piped())
.stdout(Stdio::piped())
@ -134,21 +134,21 @@ where
panic!("External command failed:\n {}", err)
}
};
(parse_output_single(text,opts.suggestion_type), result)
(parse_output_single(text, opts.suggestion_type), result)
}
fn parse_output_single(mut text: String, suggestion_type: SuggestionType) -> String {
match suggestion_type {
SuggestionType::SingleSelection => {
return text.lines().next().unwrap().to_string();
},
}
SuggestionType::MultipleSelections => {
return text.clone();
},
}
SuggestionType::Disabled => {
return "".to_string();
},
SuggestionType:: SnippetSelection => {
}
SuggestionType::SnippetSelection => {
text.truncate(text.len() - 1);
return text;
}
@ -156,14 +156,15 @@ fn parse_output_single(mut text: String, suggestion_type: SuggestionType) -> St
let lines: Vec<&str> = text.lines().collect();
match (lines.get(0), lines.get(1), lines.get(2)) {
(Some(one), Some(termination), Some(two)) if *termination == "enter" =>
(Some(one), Some(termination), Some(two)) if *termination == "enter" => {
if two.is_empty() {
one.to_string()
} else {
two.to_string()
},
(Some(one), Some(termination), None)if *termination == "enter" => one.to_string(),
(Some(one), Some(termination), _)if *termination == "tab" => one.to_string(),
}
}
(Some(one), Some(termination), None) if *termination == "enter" => one.to_string(),
(Some(one), Some(termination), _) if *termination == "tab" => one.to_string(),
_ => "".to_string(),
}
}