diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-03-25 21:52:52 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-03-25 21:52:52 +0800 |
| commit | 4cb7c2e91d7dbde32de31e6ab48683d60212ec1d (patch) | |
| tree | 8d70aac6dd33599b278684c05388a319a978b825 | |
| parent | fece037f453006c83c45825e3649495180eb30c9 (diff) | |
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
| -rw-r--r-- | .cargo/config.toml | 8 | ||||
| -rw-r--r-- | resources/other/jvn_comp_help.txt | 13 | ||||
| -rw-r--r-- | scripts/deploy/completions/bash.sh | 1 | ||||
| -rw-r--r-- | scripts/deploy/completions/fish.fish | 3 | ||||
| -rw-r--r-- | scripts/deploy/completions/powershell.ps1 | 1 | ||||
| -rw-r--r-- | scripts/deploy/completions/zsh.zsh | 1 | ||||
| -rw-r--r-- | scripts/deploy/jvn.ps1 | 2 | ||||
| -rw-r--r-- | scripts/deploy/jvn_bash.sh | 4 | ||||
| -rw-r--r-- | scripts/deploy/jvn_fish.fish | 4 | ||||
| -rw-r--r-- | scripts/deploy/jvn_zsh.zsh | 4 | ||||
| -rw-r--r-- | src/bin/jvn_comp.rs | 57 | ||||
| -rw-r--r-- | src/cmds/comp/sheetdump.rs | 11 | ||||
| -rw-r--r-- | src/cmds/comp/sheetedit.rs | 15 | ||||
| -rw-r--r-- | src/cmds/comp/version.rs | 11 | ||||
| -rw-r--r-- | src/cmds/comp/workspace_alias.rs | 8 | ||||
| -rw-r--r-- | src/cmds/comp/workspace_sheet.rs | 11 | ||||
| -rw-r--r-- | src/systems/comp/context.rs | 14 |
17 files changed, 119 insertions, 49 deletions
diff --git a/.cargo/config.toml b/.cargo/config.toml index 48edb98..213cffe 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -25,17 +25,17 @@ binaries = [ # Completions [copies.completion_jvn_bash] from = "scripts/deploy/completions/bash.sh" -to = "comp/jvn_bash.sh" +to = "comp/comp.sh" platform = [ "linux", "macos" ] [copies.completion_jvn_zsh] from = "scripts/deploy/completions/zsh.zsh" -to = "comp/jvn_zsh.zsh" +to = "comp/comp.zsh" platform = [ "linux", "macos" ] [copies.completion_jvn_fish] from = "scripts/deploy/completions/fish.fish" -to = "comp/jvn_fish.fish" +to = "comp/comp.fish" platform = [ "linux", "macos" ] # Entries @@ -61,7 +61,7 @@ platform = [ "linux", "macos" ] # Completions [copies.completion_jvn_powershell] from = "scripts/deploy/completions/powershell.ps1" -to = "comp/jvn_pwsl.ps1" +to = "comp/comp.ps1" platform = [ "windows" ] # Entries diff --git a/resources/other/jvn_comp_help.txt b/resources/other/jvn_comp_help.txt new file mode 100644 index 0000000..4660b7f --- /dev/null +++ b/resources/other/jvn_comp_help.txt @@ -0,0 +1,13 @@ +Usage: +jvn_comp [-f | --command-line <content>] + [-C | --cursor-position <position>] + [-w | --current-word <word>] + [-p | --previous-word <word>] + [-c | --command-name <name>] + [-i | --word-index <index>] + [-a | --all-words <word: ?>] + [-F | --shell-flag <bash/fish/powershell/zsh>] + +This program generates completion suggestions + for the `jvn` command, +to be used with completion scripts. diff --git a/scripts/deploy/completions/bash.sh b/scripts/deploy/completions/bash.sh index a9a3263..647b5bb 100644 --- a/scripts/deploy/completions/bash.sh +++ b/scripts/deploy/completions/bash.sh @@ -13,6 +13,7 @@ _jvn_bash_completion() { args+=(-p="${prev//-/^}") args+=(-c="${COMP_WORDS[0]//-/^}") args+=(-i="$word_index") + args+=(-F="bash") for word in "${COMP_WORDS[@]}"; do args+=(-a="${word//-/^}") diff --git a/scripts/deploy/completions/fish.fish b/scripts/deploy/completions/fish.fish index eb2c2df..45205a3 100644 --- a/scripts/deploy/completions/fish.fish +++ b/scripts/deploy/completions/fish.fish @@ -67,6 +67,9 @@ function __jvn_fish_complete set -a args -a "" end + # Add shell type argument + set -a args -F "fish" + # Call jvn_comp and handle output set -l output if not jvn_comp $args 2>/dev/null | read -z output diff --git a/scripts/deploy/completions/powershell.ps1 b/scripts/deploy/completions/powershell.ps1 index ec91038..e393e52 100644 --- a/scripts/deploy/completions/powershell.ps1 +++ b/scripts/deploy/completions/powershell.ps1 @@ -25,6 +25,7 @@ Register-ArgumentCompleter -CommandName jvn -ScriptBlock { "-c", $commandName "-i", ($words.Count - 1).ToString() "-a", ($words | ForEach-Object { $_ -replace '-', '^' }) + "-F", "powershell" ) $suggestions = jvn_comp $args 2>$null diff --git a/scripts/deploy/completions/zsh.zsh b/scripts/deploy/completions/zsh.zsh index dd1ff38..2b9e7f9 100644 --- a/scripts/deploy/completions/zsh.zsh +++ b/scripts/deploy/completions/zsh.zsh @@ -22,6 +22,7 @@ _jvn_completion() { -c "$command_name" -i "$word_index" -a "${(@)words//-/^}" + -F "zsh" ) suggestions=$(jvn_comp "${args[@]}" 2>/dev/null) diff --git a/scripts/deploy/jvn.ps1 b/scripts/deploy/jvn.ps1 index 64959d0..495ed43 100644 --- a/scripts/deploy/jvn.ps1 +++ b/scripts/deploy/jvn.ps1 @@ -1,7 +1,7 @@ $SCRIPT_DIR = Split-Path -Parent $MyInvocation.MyCommand.Definition # Completion -$completionScript = Join-Path $SCRIPT_DIR "comp\jvn_pwsl.ps1" +$completionScript = Join-Path $SCRIPT_DIR "comp\comp.ps1" if (Test-Path $completionScript) { . $completionScript } diff --git a/scripts/deploy/jvn_bash.sh b/scripts/deploy/jvn_bash.sh index 0320127..6feafb0 100644 --- a/scripts/deploy/jvn_bash.sh +++ b/scripts/deploy/jvn_bash.sh @@ -9,8 +9,8 @@ fi # Completion script if [ -n "$BASH_VERSION" ]; then - if [ -f "$SCRIPT_DIR/comp/jvn_bash.sh" ]; then - source "$SCRIPT_DIR/comp/jvn_bash.sh" + if [ -f "$SCRIPT_DIR/comp/comp.sh" ]; then + source "$SCRIPT_DIR/comp/comp.sh" else echo "Error: Completion script not found at $SCRIPT_DIR/comp/jvn_bash.sh" >&2 fi diff --git a/scripts/deploy/jvn_fish.fish b/scripts/deploy/jvn_fish.fish index f9dd2a2..1a97984 100644 --- a/scripts/deploy/jvn_fish.fish +++ b/scripts/deploy/jvn_fish.fish @@ -2,8 +2,8 @@ set SCRIPT_DIR (dirname (status --current-filename)) # Completion script -if test -f "$SCRIPT_DIR/comp/jvn_fish.fish" - source "$SCRIPT_DIR/comp/jvn_fish.fish" +if test -f "$SCRIPT_DIR/comp/comp.fish" + source "$SCRIPT_DIR/comp/comp.fish" end # Environment diff --git a/scripts/deploy/jvn_zsh.zsh b/scripts/deploy/jvn_zsh.zsh index 25f8304..d86585a 100644 --- a/scripts/deploy/jvn_zsh.zsh +++ b/scripts/deploy/jvn_zsh.zsh @@ -2,8 +2,8 @@ SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" # Completion script -if [ -f "$SCRIPT_DIR/comp/jvn_zsh.zsh" ]; then - source "$SCRIPT_DIR/comp/jvn_zsh.zsh" +if [ -f "$SCRIPT_DIR/comp/comp.zsh" ]; then + source "$SCRIPT_DIR/comp/comp.zsh" fi # Environment 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<String> = 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<Vec<String>> { @@ -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<Vec<String>> { + 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<Vec<String>> { + 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<Vec<String>> { + 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<Vec<String>> { - 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<Vec<String>> { - 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<String>, + + /// Flag to indicate completion context + #[arg(short = 'F', long, value_enum)] + pub shell_flag: ShellFlag, } |
