2020-08-27 18:15:09 +00:00
|
|
|
use crate::common::hash::fnv;
|
2020-04-19 18:51:04 +00:00
|
|
|
use crate::display::{self, Writer};
|
2020-03-20 01:01:50 +00:00
|
|
|
use crate::structures::cheat::VariableMap;
|
2020-03-27 14:36:37 +00:00
|
|
|
use crate::structures::finder::{Opts as FinderOpts, SuggestionType};
|
2020-08-27 18:15:09 +00:00
|
|
|
use crate::structures::item::Item;
|
2020-03-22 00:51:38 +00:00
|
|
|
use anyhow::{Context, Error};
|
2020-03-04 21:01:23 +00:00
|
|
|
use regex::Regex;
|
2020-03-18 15:29:29 +00:00
|
|
|
use std::collections::HashSet;
|
2020-03-04 21:01:23 +00:00
|
|
|
use std::io::Write;
|
|
|
|
|
2020-03-21 02:22:11 +00:00
|
|
|
lazy_static! {
|
2020-08-28 12:21:24 +00:00
|
|
|
pub static ref VAR_LINE_REGEX: Regex = Regex::new(r"^\$\s*([^:]+):(.*)").expect("Invalid regex");
|
2020-03-21 02:22:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 14:36:37 +00:00
|
|
|
fn parse_opts(text: &str) -> Result<FinderOpts, Error> {
|
2020-03-04 21:01:23 +00:00
|
|
|
let mut multi = false;
|
2020-03-17 15:39:38 +00:00
|
|
|
let mut prevent_extra = false;
|
2020-08-19 12:00:06 +00:00
|
|
|
let mut is_global = false;
|
2020-03-27 14:36:37 +00:00
|
|
|
let mut opts = FinderOpts::default();
|
2020-04-11 02:01:35 +00:00
|
|
|
|
2020-08-28 12:21:24 +00:00
|
|
|
let parts = shellwords::split(text).map_err(|_| anyhow!("Given options are missing a closing quote"))?;
|
2020-03-22 00:51:38 +00:00
|
|
|
|
|
|
|
parts
|
|
|
|
.into_iter()
|
|
|
|
.filter(|part| {
|
|
|
|
// We'll take parts in pairs of 2: (argument, value). Flags don't have a value tho, so we filter and handle them beforehand.
|
|
|
|
match part.as_str() {
|
|
|
|
"--multi" => {
|
|
|
|
multi = true;
|
|
|
|
false
|
|
|
|
}
|
|
|
|
"--prevent-extra" => {
|
|
|
|
prevent_extra = true;
|
|
|
|
false
|
|
|
|
}
|
2020-08-19 12:00:06 +00:00
|
|
|
"--global" => {
|
|
|
|
is_global = true;
|
|
|
|
false
|
|
|
|
}
|
2020-03-22 00:51:38 +00:00
|
|
|
_ => true,
|
2020-03-04 21:01:23 +00:00
|
|
|
}
|
2020-03-22 00:51:38 +00:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.chunks(2)
|
|
|
|
.map(|flag_and_value| {
|
|
|
|
if let [flag, value] = flag_and_value {
|
|
|
|
match flag.as_str() {
|
2020-11-27 12:35:15 +00:00
|
|
|
"--headers" | "--header-lines" => {
|
|
|
|
opts.header_lines = value
|
|
|
|
.parse::<u8>()
|
|
|
|
.context("Value for `--headers` is invalid u8")?
|
|
|
|
}
|
|
|
|
"--column" => {
|
|
|
|
opts.column = Some(
|
|
|
|
value
|
|
|
|
.parse::<u8>()
|
|
|
|
.context("Value for `--column` is invalid u8")?,
|
|
|
|
)
|
|
|
|
}
|
2020-04-11 02:01:35 +00:00
|
|
|
"--map" => opts.map = Some(value.to_string()),
|
2020-03-22 00:51:38 +00:00
|
|
|
"--delimiter" => opts.delimiter = Some(value.to_string()),
|
|
|
|
"--query" => opts.query = Some(value.to_string()),
|
|
|
|
"--filter" => opts.filter = Some(value.to_string()),
|
|
|
|
"--preview" => opts.preview = Some(value.to_string()),
|
|
|
|
"--preview-window" => opts.preview_window = Some(value.to_string()),
|
|
|
|
"--header" => opts.header = Some(value.to_string()),
|
|
|
|
"--overrides" => opts.overrides = Some(value.to_string()),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
} else if let [flag] = flag_and_value {
|
2020-03-22 15:31:49 +00:00
|
|
|
Err(anyhow!("No value provided for the flag `{}`", flag))
|
2020-03-22 00:51:38 +00:00
|
|
|
} else {
|
|
|
|
unreachable!() // Chunking by 2 allows only for tuples of 1 or 2 items...
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Result<_, _>>()
|
2020-03-27 14:36:37 +00:00
|
|
|
.context("Failed to parse finder options")?;
|
2020-03-04 21:01:23 +00:00
|
|
|
|
2020-03-20 01:01:50 +00:00
|
|
|
let suggestion_type = match (multi, prevent_extra) {
|
2020-03-24 12:42:54 +00:00
|
|
|
(true, _) => SuggestionType::MultipleSelections, // multi wins over prevent-extra
|
2020-03-20 01:01:50 +00:00
|
|
|
(false, false) => SuggestionType::SingleRecommendation,
|
|
|
|
(false, true) => SuggestionType::SingleSelection,
|
|
|
|
};
|
|
|
|
opts.suggestion_type = suggestion_type;
|
2020-08-19 12:00:06 +00:00
|
|
|
opts.global = is_global;
|
2020-03-20 01:01:50 +00:00
|
|
|
|
2020-03-22 00:51:38 +00:00
|
|
|
Ok(opts)
|
2020-03-04 21:01:23 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 14:36:37 +00:00
|
|
|
fn parse_variable_line(line: &str) -> Result<(&str, &str, Option<FinderOpts>), Error> {
|
2020-08-28 12:21:24 +00:00
|
|
|
let caps = VAR_LINE_REGEX
|
|
|
|
.captures(line)
|
|
|
|
.ok_or_else(|| anyhow!("No variables, command, and options found in the line `{}`", line))?;
|
2020-03-22 00:51:38 +00:00
|
|
|
let variable = caps
|
|
|
|
.get(1)
|
2020-03-22 15:31:49 +00:00
|
|
|
.ok_or_else(|| anyhow!("No variable captured in the line `{}`", line))?
|
2020-03-22 00:51:38 +00:00
|
|
|
.as_str()
|
|
|
|
.trim();
|
|
|
|
let mut command_plus_opts = caps
|
|
|
|
.get(2)
|
2020-03-22 15:31:49 +00:00
|
|
|
.ok_or_else(|| anyhow!("No command and options captured in the line `{}`", line))?
|
2020-03-22 00:51:38 +00:00
|
|
|
.as_str()
|
|
|
|
.split("---");
|
|
|
|
let command = command_plus_opts
|
|
|
|
.next()
|
2020-03-22 15:31:49 +00:00
|
|
|
.ok_or_else(|| anyhow!("No command captured in the line `{}`", line))?;
|
2020-03-22 00:51:38 +00:00
|
|
|
let command_options = command_plus_opts.next().map(parse_opts).transpose()?;
|
|
|
|
Ok((variable, command, command_options))
|
2020-03-04 21:01:23 +00:00
|
|
|
}
|
|
|
|
|
2020-11-21 18:42:35 +00:00
|
|
|
fn write_cmd(
|
|
|
|
tags: &str,
|
|
|
|
comment: &str,
|
|
|
|
snippet: &str,
|
2020-11-26 20:36:20 +00:00
|
|
|
file_index: &usize,
|
2020-11-21 18:42:35 +00:00
|
|
|
writer: &mut dyn Writer,
|
|
|
|
stdin: &mut std::process::ChildStdin,
|
|
|
|
) -> Result<(), Error> {
|
2020-03-26 17:14:56 +00:00
|
|
|
if snippet.len() <= 1 {
|
2020-03-22 00:51:38 +00:00
|
|
|
Ok(())
|
2020-03-14 21:08:57 +00:00
|
|
|
} else {
|
2020-04-19 18:51:04 +00:00
|
|
|
let item = Item {
|
|
|
|
tags: &tags,
|
|
|
|
comment: &comment,
|
|
|
|
snippet: &snippet,
|
2020-11-26 20:36:20 +00:00
|
|
|
file_index: &file_index,
|
2020-04-19 18:51:04 +00:00
|
|
|
};
|
2020-03-14 21:08:57 +00:00
|
|
|
stdin
|
2020-04-19 18:51:04 +00:00
|
|
|
.write_all(writer.write(item).as_bytes())
|
2020-03-27 14:36:37 +00:00
|
|
|
.context("Failed to write command to finder's stdin")
|
2020-03-14 21:08:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 20:45:01 +00:00
|
|
|
fn without_prefix(line: &str) -> String {
|
|
|
|
if line.len() > 2 {
|
|
|
|
String::from(line[2..].trim())
|
|
|
|
} else {
|
|
|
|
String::from("")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 18:15:09 +00:00
|
|
|
pub fn read_lines(
|
|
|
|
lines: impl Iterator<Item = Result<String, Error>>,
|
|
|
|
id: &str,
|
2020-11-26 20:36:20 +00:00
|
|
|
file_index: usize,
|
2020-03-18 11:38:13 +00:00
|
|
|
variables: &mut VariableMap,
|
2020-03-18 15:29:29 +00:00
|
|
|
visited_lines: &mut HashSet<u64>,
|
2020-04-25 18:30:05 +00:00
|
|
|
writer: &mut dyn Writer,
|
2020-03-04 21:01:23 +00:00
|
|
|
stdin: &mut std::process::ChildStdin,
|
2020-03-22 00:51:38 +00:00
|
|
|
) -> Result<(), Error> {
|
2020-03-04 21:01:23 +00:00
|
|
|
let mut tags = String::from("");
|
|
|
|
let mut comment = String::from("");
|
|
|
|
let mut snippet = String::from("");
|
2020-03-18 11:38:13 +00:00
|
|
|
let mut should_break = false;
|
2020-03-04 21:01:23 +00:00
|
|
|
|
2020-08-27 18:15:09 +00:00
|
|
|
for (line_nr, line_result) in lines.enumerate() {
|
2020-11-27 12:35:15 +00:00
|
|
|
let line = line_result
|
|
|
|
.with_context(|| format!("Failed to read line number {} in cheatsheet `{}`", line_nr, id))?;
|
2020-03-23 16:16:13 +00:00
|
|
|
|
2020-03-22 00:51:38 +00:00
|
|
|
if should_break {
|
|
|
|
break;
|
|
|
|
}
|
2020-03-19 12:19:50 +00:00
|
|
|
|
2020-03-22 00:51:38 +00:00
|
|
|
// duplicate
|
|
|
|
if !tags.is_empty() && !comment.is_empty() {}
|
|
|
|
// blank
|
|
|
|
if line.is_empty() {
|
|
|
|
}
|
|
|
|
// tag
|
|
|
|
else if line.starts_with('%') {
|
2020-11-26 20:36:20 +00:00
|
|
|
should_break = write_cmd(&tags, &comment, &snippet, &file_index, writer, stdin).is_err();
|
2020-03-22 00:51:38 +00:00
|
|
|
snippet = String::from("");
|
2020-10-08 20:45:01 +00:00
|
|
|
tags = without_prefix(&line);
|
2020-03-22 00:51:38 +00:00
|
|
|
}
|
2020-08-28 12:12:44 +00:00
|
|
|
// dependency
|
2020-08-23 23:09:27 +00:00
|
|
|
else if line.starts_with('@') {
|
2020-10-08 20:45:01 +00:00
|
|
|
let tags_dependency = without_prefix(&line);
|
2020-08-23 23:09:27 +00:00
|
|
|
variables.insert_dependency(&tags, &tags_dependency);
|
|
|
|
}
|
2020-03-22 00:51:38 +00:00
|
|
|
// metacomment
|
|
|
|
else if line.starts_with(';') {
|
|
|
|
}
|
|
|
|
// comment
|
|
|
|
else if line.starts_with('#') {
|
2020-11-26 20:36:20 +00:00
|
|
|
should_break = write_cmd(&tags, &comment, &snippet, &file_index, writer, stdin).is_err();
|
2020-03-22 00:51:38 +00:00
|
|
|
snippet = String::from("");
|
2020-10-08 20:45:01 +00:00
|
|
|
comment = without_prefix(&line);
|
2020-03-22 00:51:38 +00:00
|
|
|
}
|
|
|
|
// variable
|
|
|
|
else if line.starts_with('$') {
|
2020-11-26 20:36:20 +00:00
|
|
|
should_break = write_cmd(&tags, &comment, &snippet, &file_index, writer, stdin).is_err();
|
2020-03-22 00:51:38 +00:00
|
|
|
snippet = String::from("");
|
2020-11-27 12:35:15 +00:00
|
|
|
let (variable, command, opts) = parse_variable_line(&line).with_context(|| {
|
|
|
|
format!(
|
|
|
|
"Failed to parse variable line. See line number {} in cheatsheet `{}`",
|
|
|
|
line_nr + 1,
|
|
|
|
id
|
|
|
|
)
|
|
|
|
})?;
|
2020-08-23 23:09:27 +00:00
|
|
|
variables.insert_suggestion(&tags, &variable, (String::from(command), opts));
|
2020-03-22 00:51:38 +00:00
|
|
|
}
|
|
|
|
// snippet
|
|
|
|
else {
|
2020-08-27 18:15:09 +00:00
|
|
|
let hash = fnv(&format!("{}{}", &comment, &line));
|
2020-03-22 00:51:38 +00:00
|
|
|
if visited_lines.contains(&hash) {
|
|
|
|
continue;
|
2020-03-04 21:01:23 +00:00
|
|
|
}
|
2020-03-22 00:51:38 +00:00
|
|
|
visited_lines.insert(hash);
|
2020-03-19 12:19:50 +00:00
|
|
|
|
2020-03-22 00:51:38 +00:00
|
|
|
if !(&snippet).is_empty() {
|
|
|
|
snippet.push_str(display::LINE_SEPARATOR);
|
2020-03-04 21:01:23 +00:00
|
|
|
}
|
2020-03-22 00:51:38 +00:00
|
|
|
snippet.push_str(&line);
|
2020-03-04 21:01:23 +00:00
|
|
|
}
|
2020-03-22 00:51:38 +00:00
|
|
|
}
|
2020-03-15 16:46:58 +00:00
|
|
|
|
2020-03-22 00:51:38 +00:00
|
|
|
if !should_break {
|
2020-11-26 20:36:20 +00:00
|
|
|
let _ = write_cmd(&tags, &comment, &snippet, &file_index, writer, stdin);
|
2020-03-04 21:01:23 +00:00
|
|
|
}
|
2020-03-14 21:08:57 +00:00
|
|
|
|
2020-03-22 00:51:38 +00:00
|
|
|
Ok(())
|
2020-03-04 21:01:23 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 08:09:09 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_variable_line() {
|
2020-11-27 12:35:15 +00:00
|
|
|
let (variable, command, command_options) =
|
|
|
|
parse_variable_line("$ user : echo -e \"$(whoami)\\nroot\" --- --prevent-extra").unwrap();
|
2020-03-14 08:09:09 +00:00
|
|
|
assert_eq!(command, " echo -e \"$(whoami)\\nroot\" ");
|
|
|
|
assert_eq!(variable, "user");
|
2020-03-14 12:33:07 +00:00
|
|
|
assert_eq!(
|
|
|
|
command_options,
|
2020-03-27 14:36:37 +00:00
|
|
|
Some(FinderOpts {
|
2020-03-14 12:33:07 +00:00
|
|
|
header_lines: 0,
|
|
|
|
column: None,
|
|
|
|
delimiter: None,
|
2020-03-24 12:42:54 +00:00
|
|
|
suggestion_type: SuggestionType::SingleSelection,
|
2020-03-20 01:01:50 +00:00
|
|
|
..Default::default()
|
2020-03-14 12:33:07 +00:00
|
|
|
})
|
|
|
|
);
|
2020-03-14 08:09:09 +00:00
|
|
|
}
|
|
|
|
}
|