From 4cb7c2e91d7dbde32de31e6ab48683d60212ec1d Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Wed, 25 Mar 2026 21:52:52 +0800 Subject: Add shell flag to completion scripts and rename output files - Add -F flag to all completion scripts to specify shell type - Rename completion output files to generic names (comp.sh, comp.zsh, etc.) - Add help text for jvn_comp program - Combine specific and default completion results - Add completion handlers for sheetdump, sheetedit, and version commands - Remove word count limits from workspace_alias and workspace_sheet completions --- src/bin/jvn_comp.rs | 57 +++++++++++++++++++++++++++------------- src/cmds/comp/sheetdump.rs | 11 ++++++++ src/cmds/comp/sheetedit.rs | 15 +++++++++++ src/cmds/comp/version.rs | 11 ++++++++ src/cmds/comp/workspace_alias.rs | 8 ------ src/cmds/comp/workspace_sheet.rs | 11 -------- src/systems/comp/context.rs | 14 +++++++++- 7 files changed, 89 insertions(+), 38 deletions(-) create mode 100644 src/cmds/comp/sheetdump.rs create mode 100644 src/cmds/comp/sheetedit.rs create mode 100644 src/cmds/comp/version.rs (limited to 'src') diff --git a/src/bin/jvn_comp.rs b/src/bin/jvn_comp.rs index 572164c..de52253 100644 --- a/src/bin/jvn_comp.rs +++ b/src/bin/jvn_comp.rs @@ -41,18 +41,31 @@ fn main() { #[cfg(debug_assertions)] init_env_logger(); + // Check if help flag is present in arguments + let args: Vec = std::env::args().collect(); + if args.iter().any(|arg| arg == "-h" || arg == "--help") { + println!( + "{}", + include_str!("../../resources/other/jvn_comp_help.txt").trim() + ); + std::process::exit(0); + } + // Get context parameters from clap let ctx = match CompletionContext::try_parse() { - Ok(args) => CompletionContext { - // In completion scripts, "-" is replaced with "^", need to convert back here - command_line: args.command_line.replace('^', "-"), - cursor_position: args.cursor_position, - current_word: args.current_word.replace('^', "-"), - previous_word: args.previous_word.replace('^', "-"), - command_name: args.command_name.replace('^', "-"), - word_index: args.word_index, - all_words: args.all_words.iter().map(|w| w.replace('^', "-")).collect(), - }, + Ok(args) => { + CompletionContext { + // In completion scripts, "-" is replaced with "^", need to convert back here + command_line: args.command_line.replace('^', "-"), + cursor_position: args.cursor_position, + current_word: args.current_word.replace('^', "-"), + previous_word: args.previous_word.replace('^', "-"), + command_name: args.command_name.replace('^', "-"), + word_index: args.word_index, + all_words: args.all_words.iter().map(|w| w.replace('^', "-")).collect(), + shell_flag: args.shell_flag, + } + } Err(e) => { // An error occurred, collecting information for output error!( @@ -69,14 +82,21 @@ fn main() { trace_ctx(&ctx); trace!("Try using specific completion"); - let result = comp(&ctx); - if let Some(suggestions) = result { - handle_comp_result(&Some(suggestions)); - } else { - trace!("Using default completion"); - let result = default_comp(&ctx); - handle_comp_result(&result); - } + let specific_result = comp(&ctx); + trace!("Using default completion"); + let default_result = default_comp(&ctx); + + let combined_result = match (specific_result, default_result) { + (None, None) => None, + (Some(s), None) => Some(s), + (None, Some(d)) => Some(d), + (Some(mut s), Some(d)) => { + s.extend(d); + Some(s) + } + }; + + handle_comp_result(&combined_result); } fn default_comp(ctx: &CompletionContext) -> Option> { @@ -290,6 +310,7 @@ fn trace_ctx(ctx: &CompletionContext) { log::trace!("command_name={}", ctx.command_name); log::trace!("word_index={}", ctx.word_index); log::trace!("all_words={:?}", ctx.all_words); + log::trace!("shell_flag={:?}", ctx.shell_flag); } #[cfg(debug_assertions)] diff --git a/src/cmds/comp/sheetdump.rs b/src/cmds/comp/sheetdump.rs new file mode 100644 index 0000000..3528cf3 --- /dev/null +++ b/src/cmds/comp/sheetdump.rs @@ -0,0 +1,11 @@ +use cli_utils::string_vec; + +use crate::systems::comp::context::CompletionContext; + +pub fn comp(ctx: CompletionContext) -> Option> { + if ctx.current_word.starts_with('-') { + return Some(string_vec!["--no-sort", "--no-pretty"]); + } + + None +} diff --git a/src/cmds/comp/sheetedit.rs b/src/cmds/comp/sheetedit.rs new file mode 100644 index 0000000..d210028 --- /dev/null +++ b/src/cmds/comp/sheetedit.rs @@ -0,0 +1,15 @@ +use cli_utils::string_vec; + +use crate::systems::comp::context::CompletionContext; + +pub fn comp(ctx: CompletionContext) -> Option> { + if ctx.current_word.starts_with('-') { + return Some(string_vec!["-e", "--editor"]); + } + + if ctx.previous_word == "-e" || ctx.previous_word == "--editor" { + return Some(vec![]); + } + + None +} diff --git a/src/cmds/comp/version.rs b/src/cmds/comp/version.rs new file mode 100644 index 0000000..1460214 --- /dev/null +++ b/src/cmds/comp/version.rs @@ -0,0 +1,11 @@ +use cli_utils::string_vec; + +use crate::systems::comp::context::CompletionContext; + +pub fn comp(ctx: CompletionContext) -> Option> { + if ctx.current_word.starts_with('-') { + return Some(string_vec!["-c", "--with-compile-info", "--no-banner"]); + } + + None +} diff --git a/src/cmds/comp/workspace_alias.rs b/src/cmds/comp/workspace_alias.rs index 5efabfa..a8ac495 100644 --- a/src/cmds/comp/workspace_alias.rs +++ b/src/cmds/comp/workspace_alias.rs @@ -3,14 +3,6 @@ use cli_utils::string_vec; use crate::systems::comp::context::CompletionContext; pub fn comp(ctx: CompletionContext) -> Option> { - if ctx.all_words.contains(&"--insert".to_string()) { - if ctx.all_words.len() > 7 { - return None; - } - } else if ctx.all_words.len() > 5 { - return None; - } - if ctx.current_word.starts_with('-') { return Some(string_vec![ "-i", "--insert", "-Q", "--query", "-e", "--erase", "--to", diff --git a/src/cmds/comp/workspace_sheet.rs b/src/cmds/comp/workspace_sheet.rs index 89cd259..3162442 100644 --- a/src/cmds/comp/workspace_sheet.rs +++ b/src/cmds/comp/workspace_sheet.rs @@ -3,17 +3,6 @@ use cli_utils::string_vec; use just_enough_vcs::system::workspace::workspace::manager::WorkspaceManager; pub fn comp(ctx: CompletionContext) -> Option> { - if ctx.all_words.len() > 5 { - return None; - } - - if (ctx.all_words.contains(&"--list-all".to_string()) - || ctx.all_words.contains(&"-A".to_string())) - && ctx.all_words.len() > 4 - { - return None; - } - if ctx.current_word.starts_with('-') { return Some(string_vec![ "-A", diff --git a/src/systems/comp/context.rs b/src/systems/comp/context.rs index 7fb8f43..8d0adc2 100644 --- a/src/systems/comp/context.rs +++ b/src/systems/comp/context.rs @@ -1,4 +1,12 @@ -use clap::Parser; +use clap::{Parser, ValueEnum}; + +#[derive(ValueEnum, Debug, Clone, Copy)] +pub enum ShellFlag { + Zsh, + Bash, + Fish, + Powershell, +} #[derive(Parser, Debug, Clone)] #[command(author, version, about, long_about = None)] @@ -30,4 +38,8 @@ pub struct CompletionContext { /// All words #[arg(short = 'a', long, num_args = 1..)] pub all_words: Vec, + + /// Flag to indicate completion context + #[arg(short = 'F', long, value_enum)] + pub shell_flag: ShellFlag, } -- cgit