diff --git a/Cargo.lock b/Cargo.lock index 11e7f6e..72205a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -144,6 +144,8 @@ dependencies = [ "rand", "regex", "shellwords", + "strum", + "strum_macros", ] [[package]] @@ -283,6 +285,12 @@ version = "0.6.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" +[[package]] +name = "rustversion" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0a5f7c728f5d284929a1cccb5bc19884422bfe6ef4d6c409da2c41838983fcf" + [[package]] name = "shellwords" version = "1.1.0" @@ -299,6 +307,28 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +[[package]] +name = "strum" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f" +dependencies = [ + "strum_macros", +] + +[[package]] +name = "strum_macros" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4faebde00e8ff94316c01800f9054fd2ba77d30d9e922541913051d1d978918b" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "rustversion", + "syn", +] + [[package]] name = "syn" version = "1.0.86" diff --git a/Cargo.toml b/Cargo.toml index 06529ec..c54a7b3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,6 +41,8 @@ evalexpr = "7" kakplugin = {path = "./kakplugin/"} linked-hash-map = "0.5.4" linked_hash_set = "0.1.4" +strum_macros = "0.24" +strum = { version = "0.24", features = ["derive"] } [profile.release] lto = true diff --git a/kakplugin/src/lib.rs b/kakplugin/src/lib.rs index 82f189f..e505db9 100644 --- a/kakplugin/src/lib.rs +++ b/kakplugin/src/lib.rs @@ -212,3 +212,24 @@ pub fn get_var(var_name: &str) -> Result { } }) } + +/// Prints a list of shell script candidates for kakoune to ingest +pub fn generate_shell_script_candidates(variants: &[S]) -> Result<(), KakError> +where + S: AsRef, +{ + let token_to_complete = get_var("kak_token_to_complete")?.parse::()?; + + match token_to_complete { + 0 => { + for v in variants { + println!("{}", v.as_ref()); + } + } + 1_u8..=u8::MAX => { + // We can't see which command was selected, so none of these will do anything + } + } + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index e2d1333..2ad47a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,8 @@ mod utils; // mod xargs; use clap::{Parser, Subcommand}; use kakplugin::{display_message, get_var, KakError}; +use std::env; +use strum::VariantNames; #[derive(Parser, Debug)] #[clap(about, version, author)] @@ -34,7 +36,8 @@ struct Cli { // kak_response_fifo_name: PathBuf, } -#[derive(Subcommand, Debug)] +#[derive(Subcommand, Debug, strum::EnumVariantNames)] +#[strum(serialize_all = "kebab_case")] enum Commands { #[clap(about = "Sorts selections based on content or content regex match")] Sort(sort::Options), @@ -57,6 +60,16 @@ enum Commands { } fn main() { + // First, check if we are just getting candidates to run the program. kak_command_fifo is not needed for this + let args = env::args().collect::>(); + if args.len() == 2 && args[1] == "shell-script-candidates" { + match kakplugin::generate_shell_script_candidates(Commands::VARIANTS) { + Err(e) => eprintln!("{e:?}"), + Ok(()) => {} + } + return; + } + if get_var("kak_command_fifo") .and(get_var("kak_response_fifo")) .is_err()