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

@ -67,11 +67,10 @@ fn parse_opts(text: &str) -> SuggestionOpts {
suggestion_type: match (multi, allow_extra) {
(true, _) => SuggestionType::MultipleSelections, // multi wins over allow-extra
(false, true) => SuggestionType::SingleRecommendation,
(false,false) => SuggestionType::SingleSelection
(false, false) => SuggestionType::SingleSelection,
},
};
result
}
fn parse_variable_line(line: &str) -> (&str, &str, Option<SuggestionOpts>) {
@ -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
);
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 }))
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,
@ -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>,
@ -19,7 +19,6 @@ pub struct Opts<'a> {
pub suggestion_type: SuggestionType,
}
impl Default for Opts<'_> {
fn default() -> Self {
Self {
@ -59,10 +58,12 @@ 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 => {
fzf_command.args(&["--expect", "ctrl-y,enter"]);
}
@ -105,7 +106,6 @@ where
});
}
let child = fzf_command
.stdin(Stdio::piped())
.stdout(Stdio::piped())
@ -141,13 +141,13 @@ fn parse_output_single(mut text: String, suggestion_type: SuggestionType) -> St
match suggestion_type {
SuggestionType::SingleSelection => {
return text.lines().next().unwrap().to_string();
},
}
SuggestionType::MultipleSelections => {
return text.clone();
},
}
SuggestionType::Disabled => {
return "".to_string();
},
}
SuggestionType::SnippetSelection => {
text.truncate(text.len() - 1);
return text;
@ -156,12 +156,13 @@ 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(),
_ => "".to_string(),