Add kak responses and enable skipping whitespace

This commit is contained in:
Austen Adler 2022-01-23 21:30:00 -05:00
parent a68e987ab1
commit b127ffe59d

View File

@ -3,13 +3,14 @@
use clap::Parser; use clap::Parser;
use regex::Regex; use regex::Regex;
type KakMessage = (String, Option<String>);
#[derive(Parser)] #[derive(Parser)]
#[clap(about, version, author)] #[clap(about, version, author)]
struct Options { struct Options {
#[clap(short = 'S', long)] #[clap(short = 'S', long)]
// TODO: Can we invert a boolean? This name is terrible
no_skip_whitespace: bool, no_skip_whitespace: bool,
// #[clap(short, long)]
// debug: bool,
#[clap(short, long, required = true)] #[clap(short, long, required = true)]
regex: String, regex: String,
#[clap(multiple_occurrences = true, required = true)] #[clap(multiple_occurrences = true, required = true)]
@ -17,38 +18,61 @@ struct Options {
} }
fn main() { fn main() {
if let Err(msg) = run() { match run() {
output_message(&msg, false); Ok(()) => send_message(&("Replaced successfully".to_string(), None)),
Err(msg) => send_message(&msg),
} }
} }
fn output_message(msg: &str, debug: bool) { fn send_message(msg: &KakMessage) {
println!( let msg_str = msg.0.replace('\'', "''");
"echo{}'{}';", print!("echo '{}';", msg_str);
if debug { " -debug" } else { " " },
msg.replace("'", "''") if let Some(debug_info) = &msg.1 {
); print!("echo -debug '{}';", msg_str);
print!("echo -debug '{}';", debug_info.replace('\'', "''"));
}
} }
fn run() -> Result<(), String> { fn run() -> Result<(), KakMessage> {
let options = Options::try_parse().map_err(|e| format!("Error: {:?}", e))?; let options = Options::try_parse().map_err(|e| {
(
"Error parsing arguments".to_string(),
Some(format!("Could not parse: {:?}", e)),
)
})?;
let replacement_re = options.regex; let replacement_re = options.regex;
let re = Regex::new(&replacement_re) let re = Regex::new(&replacement_re).map_err(|_| {
.map_err(|_| format!("Invalid regular expression: {}", replacement_re))?; (
format!("Invalid regular expression: {}", replacement_re),
None,
)
})?;
let mut zipped = options let mut zipped = options
.selections .selections
.iter() .iter()
.skip(2) .zip(
.zip(options.selections.iter().skip(2).map(|a| { options
let captures = re.captures(a)?; .selections
captures .iter()
.get(1) .map(|a| {
.or_else(|| captures.get(0)) if options.no_skip_whitespace {
.map(|m| m.as_str()) a
})) } else {
a.trim()
}
})
.map(|a| {
let captures = re.captures(a)?;
captures
.get(1)
.or_else(|| captures.get(0))
.map(|m| m.as_str())
}),
)
.collect::<Vec<(&String, Option<&str>)>>(); .collect::<Vec<(&String, Option<&str>)>>();
zipped.sort_by(|(a, a_key), (b, b_key)| { zipped.sort_by(|(a, a_key), (b, b_key)| {
@ -59,11 +83,8 @@ fn run() -> Result<(), String> {
print!("reg '\"'"); print!("reg '\"'");
for i in &zipped { for i in &zipped {
let new_selection = i.0.replace("'", "''"); let new_selection = i.0.replace('\'', "''");
print!(" '{}'", new_selection); print!(" '{}'", new_selection);
// print!("{}\0", new_selection);
// TODO: Allow debugging with -d
// println!("\n\tSort key: {:?}", i.1);
} }
print!(" ;"); print!(" ;");
Ok(()) Ok(())