diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-03-14 22:12:30 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-03-14 22:40:12 +0800 |
| commit | 72f80ea51f25256d0c463c2f3dc3d8670cfc4634 (patch) | |
| tree | b200a3ab1a4c718034458863570a064b52bafdcd | |
| parent | 54b5567d6f1b1adaa6ada6a26faba0c5c492b7f3 (diff) | |
Add shell completions for new jvn CLI
| -rw-r--r-- | .cargo/config.toml | 83 | ||||
| -rw-r--r-- | scripts/deploy/completions/bash.sh | 65 | ||||
| -rw-r--r-- | scripts/deploy/completions/fish.fish | 43 | ||||
| -rw-r--r-- | scripts/deploy/completions/powershell.ps1 | 42 | ||||
| -rw-r--r-- | scripts/deploy/completions/zsh.sh | 48 | ||||
| -rw-r--r-- | scripts/deploy/jv_cli.ps1 | 4 | ||||
| -rwxr-xr-x | scripts/deploy/jv_cli.sh | 8 | ||||
| -rw-r--r-- | scripts/deploy/jvn.ps1 | 13 | ||||
| -rw-r--r-- | scripts/deploy/jvn.sh | 25 | ||||
| -rwxr-xr-x | scripts/deploy/legacy_completions/bash/completion_jv.sh (renamed from scripts/deploy/completions/bash/completion_jv.sh) | 0 | ||||
| -rwxr-xr-x | scripts/deploy/legacy_completions/bash/completion_jvv.sh (renamed from scripts/deploy/completions/bash/completion_jvv.sh) | 0 | ||||
| -rw-r--r-- | scripts/deploy/legacy_completions/powershell/completion_jv.ps1 (renamed from scripts/deploy/completions/powershell/completion_jv.ps1) | 0 | ||||
| -rw-r--r-- | scripts/deploy/legacy_completions/powershell/completion_jvv.ps1 (renamed from scripts/deploy/completions/powershell/completion_jvv.ps1) | 0 | ||||
| -rw-r--r-- | scripts/deploy/legacy_zsh_support/how_to_install.md (renamed from scripts/deploy/zsh_support/how_to_install.md) | 0 | ||||
| -rwxr-xr-x | scripts/deploy/legacy_zsh_support/install.sh (renamed from scripts/deploy/zsh_support/install.sh) | 0 | ||||
| -rw-r--r-- | scripts/deploy/legacy_zsh_support/jvcs.plugin.zsh (renamed from scripts/deploy/zsh_support/jvcs.plugin.zsh) | 0 | ||||
| -rw-r--r-- | src/bin/jvn_comp.rs | 54 |
17 files changed, 355 insertions, 30 deletions
diff --git a/.cargo/config.toml b/.cargo/config.toml index f79321e..40260ca 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -4,73 +4,108 @@ target-dir = "./.temp/target/" [publish] target-dir = "./.temp/deploy/" binaries = [ + "jvii", + "jvii.exe", + + "jvn", + "jvn.exe", + + "jvn_comp", + "jvn_comp.exe", + # The legacy command line "jv", "jv.exe", "jvv", "jvv.exe", - - "jvii", - "jvii.exe", - - # The new command line - "jvn", - "jvn.exe" ] -############ -### Bash ### -############ +##################### +### Bash/Zsh/Fish ### +##################### # Completions +[copies.completion_jvn_bash] +from = "scripts/deploy/completions/bash.sh" +to = "comp/jvn_bash.sh" +platform = [ "linux", "macos" ] + +[copies.completion_jvn_zsh] +from = "scripts/deploy/completions/zsh.sh" +to = "comp/jvn_zsh.sh" +platform = [ "linux", "macos" ] + +[copies.completion_jvn_fish] +from = "scripts/deploy/completions/fish.fish" +to = "comp/jvn_fish.fish" +platform = [ "linux", "macos" ] + +# Legacy Completions [copies.completion_jv_bash] -from = "scripts/deploy/completions/bash/completion_jv.sh" -to = "comp/jv.sh" +from = "scripts/deploy/legacy_completions/bash/completion_jv.sh" +to = "_legacy/comp/jv.sh" platform = [ "linux", "macos" ] [copies.completion_jvv_bash] -from = "scripts/deploy/completions/bash/completion_jvv.sh" -to = "comp/jvv.sh" +from = "scripts/deploy/legacy_completions/bash/completion_jvv.sh" +to = "_legacy/comp/jvv.sh" platform = [ "linux", "macos" ] # Entries -[copies.entry_bash] +[copies.entry_legacy_bash] from = "scripts/deploy/jv_cli.sh" to = "jv_cli.sh" platform = [ "linux", "macos" ] +[copies.entry_bash] +from = "scripts/deploy/jvn.sh" +to = "jvn.sh" +platform = [ "linux", "macos" ] + ################## ### Powershell ### ################## # Completions +[copies.completion_jvn_powershell] +from = "scripts/deploy/completions/powershell.ps1" +to = "comp/jvn_pwsl.ps1" +platform = [ "windows" ] + +# Legacy Completions [copies.completion_jv_powershell] -from = "scripts/deploy/completions/powershell/completion_jv.ps1" -to = "comp/jv.ps1" +from = "scripts/deploy/legacy_completions/powershell/completion_jv.ps1" +to = "_legacy/comp/jv.ps1" platform = [ "windows" ] [copies.completion_jvv_powershell] -from = "scripts/deploy/completions/powershell/completion_jvv.ps1" -to = "comp/jvv.ps1" +from = "scripts/deploy/legacy_completions/powershell/completion_jvv.ps1" +to = "_legacy/comp/jvv.ps1" platform = [ "windows" ] + # Entries -[copies.entry_powershell] +[copies.entry_legacy_powershell] from = "scripts/deploy/jv_cli.ps1" to = "jv_cli.ps1" platform = [ "windows" ] +[copies.entry_powershell] +from = "scripts/deploy/jvn.ps1" +to = "jvn.ps1" +platform = [ "windows" ] + ################## ### Zsh Plugin ### ################## [copies.zsh_plugin] -from = "scripts/deploy/zsh_support/jvcs.plugin.zsh" -to = "zsh_support/jvcs.plugin.zsh" +from = "scripts/deploy/legacy_zsh_support/jvcs.plugin.zsh" +to = "_legacy/zsh_support/jvcs.plugin.zsh" platform = [ "linux", "macos" ] [copies.zsh_plugin_installation] -from = "scripts/deploy/zsh_support/how_to_install.md" -to = "zsh_support/how_to_install.md" +from = "scripts/deploy/legacy_zsh_support/how_to_install.md" +to = "_legacy/zsh_support/how_to_install.md" platform = [ "linux", "macos" ] diff --git a/scripts/deploy/completions/bash.sh b/scripts/deploy/completions/bash.sh new file mode 100644 index 0000000..418105d --- /dev/null +++ b/scripts/deploy/completions/bash.sh @@ -0,0 +1,65 @@ +#!/usr/bin/env bash +_jvn_bash_completion() { + local cur prev words cword + + local line="${COMP_LINE}" + local point="${COMP_POINT}" + + if [ "${point}" -gt "${#line}" ]; then + point="${#line}" + fi + + words=($line) + cword=0 + + local i=0 + local pos=0 + for word in "${words[@]}"; do + local word_start=$pos + local word_end=$((pos + ${#word})) + + if [ "${point}" -ge "${word_start}" ] && [ "${point}" -le "${word_end}" ]; then + cword=$i + cur="${word}" + break + fi + + pos=$((pos + ${#word} + 1)) + i=$((i + 1)) + done + + if [ "${point}" -gt "${pos}" ]; then + cword=${#words[@]} + cur="" + fi + + if [ "${cword}" -gt 0 ]; then + prev="${words[$((cword-1))]}" + else + prev="" + fi + + local args=( + -f "$COMP_LINE" + -C "$COMP_POINT" + -w "$cur" + -p "$prev" + -c "${words[0]}" + -i "$cword" + -a "${words[@]}" + ) + + local suggestions + if suggestions=$(jvn_comp "${args[@]}" 2>/dev/null); then + if [ "$suggestions" = "_file_" ]; then + compopt -o default + COMPREPLY=() + else + mapfile -t COMPREPLY < <(printf '%s\n' "$suggestions") + fi + else + COMPREPLY=() + fi +} + +complete -F _jvn_bash_completion jvn diff --git a/scripts/deploy/completions/fish.fish b/scripts/deploy/completions/fish.fish new file mode 100644 index 0000000..2904495 --- /dev/null +++ b/scripts/deploy/completions/fish.fish @@ -0,0 +1,43 @@ +#!/usr/bin/env fish +function __jvn_fish_complete + set -l cmdline (commandline -opc) + set -l buffer (commandline -b) + set -l cursor (commandline -C) + + set -l current_word "" + set -l previous_word "" + set -l word_index 0 + set -l char_count 0 + + for i in (seq (count $cmdline)) + set word $cmdline[$i] + set char_count (math $char_count + (string length "$word") + 1) + + if test $cursor -le $char_count + set word_index $i + set current_word $word + if test $i -gt 1 + set previous_word $cmdline[(math $i - 1)] + end + break + end + end + + set -l args \ + -f "$buffer" \ + -C "$cursor" \ + -w "$current_word" \ + -p "$previous_word" \ + -c "$cmdline[1]" \ + -i "$word_index" \ + -a $cmdline + + set -l output (jvn_comp $args 2>/dev/null) + if test "$output" = "_file_" + __fish_complete_path "$current_word" + else + printf "%s\n" $output + end +end + +complete -c jvn -a '(__jvn_fish_complete)' diff --git a/scripts/deploy/completions/powershell.ps1 b/scripts/deploy/completions/powershell.ps1 new file mode 100644 index 0000000..0c3cddc --- /dev/null +++ b/scripts/deploy/completions/powershell.ps1 @@ -0,0 +1,42 @@ +Register-ArgumentCompleter -CommandName jvn -ScriptBlock { + param($wordToComplete, $commandAst, $cursorPosition) + + $line = $commandAst.ToString() + $commandName = if ($commandAst.CommandElements.Count -gt 0) { + $commandAst.CommandElements[0].Value + } else { "" } + + $words = @() + $currentIndex = 0 + $parser = [System.Management.Automation.PSParser] + $tokens = $parser::Tokenize($line, [ref]$null) + + foreach ($token in $tokens) { + if ($token.Type -in 'CommandArgument', 'CommandParameter') { + $words += $token.Content + } + } + + $args = @( + "-f", $line + "-C", $cursorPosition.ToString() + "-w", $wordToComplete + "-p", if ($words.Count -gt 1) { $words[-2] } else { "" } + "-c", $commandName + "-i", ($words.Count - 1).ToString() + "-a", $words + ) + + $suggestions = jvn_comp $args 2>$null + + if ($suggestions) { + $suggestions | ForEach-Object { + if ($_ -eq "_file_") { + $completionType = 'ProviderItem' + } else { + $completionType = 'ParameterValue' + } + [System.Management.Automation.CompletionResult]::new($_, $_, $completionType, $_) + } + } +} diff --git a/scripts/deploy/completions/zsh.sh b/scripts/deploy/completions/zsh.sh new file mode 100644 index 0000000..2b2a96b --- /dev/null +++ b/scripts/deploy/completions/zsh.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env zsh +_jvn_completion() { + local -a args + local suggestions + + local buffer="$BUFFER" + local cursor="$CURSOR" + local current_word="${words[$CURRENT]}" + local previous_word="" + local command_name="${words[1]}" + local word_index="$CURRENT" + + if [[ $CURRENT -gt 1 ]]; then + previous_word="${words[$((CURRENT-1))]}" + fi + + args=( + -f "$buffer" + -C "$cursor" + -w "$current_word" + -p "$previous_word" + -c "$command_name" + -i "$word_index" + -a "${words[@]}" + ) + + suggestions=$(jvn_comp "${args[@]}" 2>/dev/null) + + if [[ $? -eq 0 ]] && [[ -n "$suggestions" ]]; then + local -a completions + completions=(${(f)suggestions}) + + if [[ "${completions[1]}" == "_file_" ]]; then + shift completions + _files + elif (( $+functions[_describe] )); then + _describe 'jvn commands' completions + else + compadd -a completions + fi + fi +} + +compdef _jvn_completion jvn + +if [[ $? -ne 0 ]]; then + compctl -K _jvn_completion jvn +fi diff --git a/scripts/deploy/jv_cli.ps1 b/scripts/deploy/jv_cli.ps1 index 2761137..3de330a 100644 --- a/scripts/deploy/jv_cli.ps1 +++ b/scripts/deploy/jv_cli.ps1 @@ -47,10 +47,10 @@ Set-Alias jmv jmv ### COMPLETION ### ################## -if (Test-Path "$SCRIPT_DIR\comp\jv.ps1") { +if (Test-Path "$SCRIPT_DIR\_legacy\comp\jv.ps1") { . "$SCRIPT_DIR\comp\jv.ps1" } -if (Test-Path "$SCRIPT_DIR\comp\jvv.ps1") { +if (Test-Path "$SCRIPT_DIR\_legacy\comp\jvv.ps1") { . "$SCRIPT_DIR\comp\jvv.ps1" } diff --git a/scripts/deploy/jv_cli.sh b/scripts/deploy/jv_cli.sh index 80ff757..b5212af 100755 --- a/scripts/deploy/jv_cli.sh +++ b/scripts/deploy/jv_cli.sh @@ -58,11 +58,11 @@ alias jvu='jv update' ### COMPLETION ### ################## -if [ -f "$SCRIPT_DIR/comp/jv.sh" ]; then - source "$SCRIPT_DIR/comp/jv.sh" +if [ -f "$SCRIPT_DIR/_legacy/comp/jv.sh" ]; then + source "$SCRIPT_DIR/_legacy/comp/jv.sh" fi -if [ -f "$SCRIPT_DIR/comp/jvv.sh" ]; then - source "$SCRIPT_DIR/comp/jvv.sh" +if [ -f "$SCRIPT_DIR/_legacy/comp/jvv.sh" ]; then + source "$SCRIPT_DIR/_legacy/comp/jvv.sh" fi ################## diff --git a/scripts/deploy/jvn.ps1 b/scripts/deploy/jvn.ps1 new file mode 100644 index 0000000..64959d0 --- /dev/null +++ b/scripts/deploy/jvn.ps1 @@ -0,0 +1,13 @@ +$SCRIPT_DIR = Split-Path -Parent $MyInvocation.MyCommand.Definition + +# Completion +$completionScript = Join-Path $SCRIPT_DIR "comp\jvn_pwsl.ps1" +if (Test-Path $completionScript) { + . $completionScript +} + +# Envirement +$binPath = Join-Path $SCRIPT_DIR "bin" +if (Test-Path $binPath) { + $env:PATH = "$binPath;$env:PATH" +} diff --git a/scripts/deploy/jvn.sh b/scripts/deploy/jvn.sh new file mode 100644 index 0000000..25c6ad5 --- /dev/null +++ b/scripts/deploy/jvn.sh @@ -0,0 +1,25 @@ +#!bin/bash +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" + +# Completion script +if [ -n "$BASH_VERSION" ]; then + # Bash + if [ -f "$SCRIPT_DIR/comp/jvn_bash.sh" ]; then + source "$SCRIPT_DIR/comp/jvn_bash.sh" + fi +elif [ -n "$ZSH_VERSION" ]; then + # Zsh + if [ -f "$SCRIPT_DIR/comp/jvn_zsh.sh" ]; then + source "$SCRIPT_DIR/comp/jvn_zsh.sh" + fi +elif [ -n "$FISH_VERSION" ]; then + # Fish + if [ -f "$SCRIPT_DIR/comp/jvn_fish.fish" ]; then + source "$SCRIPT_DIR/comp/jvn_fish.fish" + fi +fi + +# Envirement +if [ -d "$SCRIPT_DIR/bin" ]; then + export PATH="$SCRIPT_DIR/bin:$PATH" +fi diff --git a/scripts/deploy/completions/bash/completion_jv.sh b/scripts/deploy/legacy_completions/bash/completion_jv.sh index 364df9d..364df9d 100755 --- a/scripts/deploy/completions/bash/completion_jv.sh +++ b/scripts/deploy/legacy_completions/bash/completion_jv.sh diff --git a/scripts/deploy/completions/bash/completion_jvv.sh b/scripts/deploy/legacy_completions/bash/completion_jvv.sh index ce5668b..ce5668b 100755 --- a/scripts/deploy/completions/bash/completion_jvv.sh +++ b/scripts/deploy/legacy_completions/bash/completion_jvv.sh diff --git a/scripts/deploy/completions/powershell/completion_jv.ps1 b/scripts/deploy/legacy_completions/powershell/completion_jv.ps1 index 84ba01a..84ba01a 100644 --- a/scripts/deploy/completions/powershell/completion_jv.ps1 +++ b/scripts/deploy/legacy_completions/powershell/completion_jv.ps1 diff --git a/scripts/deploy/completions/powershell/completion_jvv.ps1 b/scripts/deploy/legacy_completions/powershell/completion_jvv.ps1 index fa773c0..fa773c0 100644 --- a/scripts/deploy/completions/powershell/completion_jvv.ps1 +++ b/scripts/deploy/legacy_completions/powershell/completion_jvv.ps1 diff --git a/scripts/deploy/zsh_support/how_to_install.md b/scripts/deploy/legacy_zsh_support/how_to_install.md index 343ea7b..343ea7b 100644 --- a/scripts/deploy/zsh_support/how_to_install.md +++ b/scripts/deploy/legacy_zsh_support/how_to_install.md diff --git a/scripts/deploy/zsh_support/install.sh b/scripts/deploy/legacy_zsh_support/install.sh index aa92952..aa92952 100755 --- a/scripts/deploy/zsh_support/install.sh +++ b/scripts/deploy/legacy_zsh_support/install.sh diff --git a/scripts/deploy/zsh_support/jvcs.plugin.zsh b/scripts/deploy/legacy_zsh_support/jvcs.plugin.zsh index ff3f213..ff3f213 100644 --- a/scripts/deploy/zsh_support/jvcs.plugin.zsh +++ b/scripts/deploy/legacy_zsh_support/jvcs.plugin.zsh diff --git a/src/bin/jvn_comp.rs b/src/bin/jvn_comp.rs new file mode 100644 index 0000000..d00916c --- /dev/null +++ b/src/bin/jvn_comp.rs @@ -0,0 +1,54 @@ +use clap::Parser; + +#[derive(Parser, Debug)] +#[command(author, version, about, long_about = None)] +struct CompletionContext { + /// The full command line + #[arg(short = 'f', long)] + command_line: String, + + /// Cursor position + #[arg(short = 'C', long)] + cursor_position: usize, + + /// Current word + #[arg(short = 'w', long)] + current_word: String, + + /// Previous word + #[arg(short = 'p', long)] + previous_word: String, + + /// Command name + #[arg(short = 'c', long)] + command_name: String, + + /// Word index + #[arg(short = 'i', long)] + word_index: usize, + + /// All words + #[arg(short = 'a', long, num_args = 1..)] + all_words: Vec<String>, +} + +fn main() { + let args = match CompletionContext::try_parse() { + Ok(args) => args, + Err(_) => std::process::exit(1), + }; + match comp(args) { + Some(suggestions) => { + suggestions + .iter() + .for_each(|suggest| println!("{}", suggest)); + } + None => { + println!("_file_"); + } + } +} + +fn comp(_args: CompletionContext) -> Option<Vec<String>> { + None +} |
