Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211use std::ffi::{OsStr, OsString};
use bpaf::{any, OptionParser, Parser};
#[derive(Debug, PartialEq, Eq)]pub struct CliArgs { pub kakoune_args: Vec<OsString>, pub bufferline: bool, pub print_bufferline_kakrc: bool, pub picker: bool, pub print_picker_kakrc: bool,}
fn is_kak_rs_help_or_version(argument: &OsString) -> bool { matches!( argument.to_str(), Some("-h" | "--help" | "-V" | "--version") )}
fn parser() -> OptionParser<Vec<OsString>> { any::<OsString, _, _>("KAK_ARG", |argument| { (!is_kak_rs_help_or_version(&argument)).then_some(argument) }) .help("Kakoune arguments to forward to Kakoune as-is") .many() .to_options() .descr("A terminal user interface for Kakoune") .version(env!("CARGO_PKG_VERSION"))}
pub(crate) fn help_text() -> String { concat!( "A terminal user interface for Kakoune\n", "\n", "Usage: kak-rs [KAK_ARG]...\n", "\n", "Available positional items:\n", " KAK_ARG Kakoune arguments to forward to Kakoune as-is\n", "\n", "Available options:\n", " -h, --help Prints help information\n", " -V, --version Prints version information\n", " --bufferline Enable the bufferline\n", " --print-bufferline-kakrc Print the bufferline kakscript and exit\n", " --picker Enable the fuzzy-finder overlay\n", " --print-picker-kakrc Print the picker kakscript and exit\n", ) .to_string()}
pub fn parse() -> CliArgs { let raw: Vec<OsString> = std::env::args_os().skip(1).collect(); if raw .iter() .any(|argument| matches!(argument.to_str(), Some("-h" | "--help"))) { print!("{}", help_text()); std::process::exit(0); } split_arguments(parser().run())}
fn split_arguments(arguments: Vec<OsString>) -> CliArgs { let mut kakoune_args = Vec::with_capacity(arguments.len()); let mut bufferline = false; let mut print_bufferline_kakrc = false; let mut picker = false; let mut print_picker_kakrc = false; let mut consumes_next = false; let mut after_separator = false;
for argument in arguments { if after_separator || consumes_next { kakoune_args.push(argument); consumes_next = false; } else if argument == OsStr::new("--") { after_separator = true; kakoune_args.push(argument); } else if argument == OsStr::new("--bufferline") { bufferline = true; } else if argument == OsStr::new("--print-bufferline-kakrc") { print_bufferline_kakrc = true; } else if argument == OsStr::new("--picker") { picker = true; } else if argument == OsStr::new("--print-picker-kakrc") { print_picker_kakrc = true; } else { consumes_next = kakoune_switch_takes_value(&argument); kakoune_args.push(argument); } }
CliArgs { kakoune_args, bufferline, print_bufferline_kakrc, picker, print_picker_kakrc, }}
pub(crate) fn kakoune_switch_takes_value(argument: &OsStr) -> bool { matches!( argument.to_str(), Some("-c" | "-C" | "-e" | "-E" | "-s" | "-p" | "-f" | "-i" | "-ui" | "-debug") )}
#[cfg(test)]mod tests { use super::*;
#[test] fn forwards_kakoune_arguments_in_order_without_parsing_them() { let args = [ OsString::from("-c"), OsString::from("work"), OsString::from("-C"), OsString::from("other-work"), OsString::from("-e"), OsString::from("set-option window tabstop 4"), OsString::from("-E"), OsString::from("set-option global tabstop 2"), OsString::from("-s"), OsString::from("kak-session"), OsString::from("+12:3"), OsString::from("file.txt"), ];
assert_eq!(parser().run_inner(&args[..]).unwrap(), args); }
#[test] fn bufferline_flags_are_removed_without_stealing_kakoune_values() { let options = split_arguments(vec![ OsString::from("--bufferline"), OsString::from("-e"), OsString::from("--print-bufferline-kakrc"), OsString::from("--print-bufferline-kakrc"), OsString::from("--"), OsString::from("--bufferline"), ]);
assert!(options.bufferline); assert!(options.print_bufferline_kakrc); assert_eq!( options.kakoune_args, [ OsString::from("-e"), OsString::from("--print-bufferline-kakrc"), OsString::from("--"), OsString::from("--bufferline"), ] ); }
#[test] fn picker_flags_are_removed_without_stealing_kakoune_values() { let options = split_arguments(vec![ OsString::from("--picker"), OsString::from("-e"), OsString::from("--print-picker-kakrc"), OsString::from("--print-picker-kakrc"), OsString::from("--"), OsString::from("--picker"), ]);
assert!(options.picker); assert!(options.print_picker_kakrc); assert_eq!( options.kakoune_args, [ OsString::from("-e"), OsString::from("--print-picker-kakrc"), OsString::from("--"), OsString::from("--picker"), ] ); }
#[test] fn version_is_handled_by_bpaf() { for argument in ["-V", "--version"] { let version = parser() .run_inner([argument].as_slice()) .unwrap_err() .unwrap_stdout(); assert!(version.contains(env!("CARGO_PKG_VERSION"))); } }
#[test] fn help_text_lists_every_flag_on_its_own_line() { let help = help_text(); for flag in [ "-h, --help", "-V, --version", "--bufferline", "--print-bufferline-kakrc", "--picker", "--print-picker-kakrc", ] { assert!( help.lines().any(|line| line.trim_start().starts_with(flag)), "expected a line starting with {flag:?} in:\n{help}" ); } }}