From 5567dd6cffed8bd31a73f63a879106162b7b86a0 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Thu, 6 Nov 2025 22:07:06 +0800 Subject: feat: add shell integration and completion scripts - Add cli.sh for aliases and completion loading - Create completion_jv.sh for jv command bash completion - Create completion_jvv.sh for jvv command bash completion - Support dynamic completion using --raw output from commands --- scripts/cli.sh | 22 ++++++++ scripts/completion_jv.sh | 136 ++++++++++++++++++++++++++++++++++++++++++++++ scripts/completion_jvv.sh | 77 ++++++++++++++++++++++++++ 3 files changed, 235 insertions(+) create mode 100644 scripts/cli.sh create mode 100755 scripts/completion_jv.sh create mode 100755 scripts/completion_jvv.sh (limited to 'scripts') diff --git a/scripts/cli.sh b/scripts/cli.sh new file mode 100644 index 0000000..ba21c54 --- /dev/null +++ b/scripts/cli.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# Get the real directory where this script is located +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" + +# Aliases +alias jvh='jv here' +alias jvu='jv update' +alias jvt='jv track' + +# Completion +if [ -f "$SCRIPT_DIR/completion_jv.sh" ]; then + source "$SCRIPT_DIR/completion_jv.sh" +fi +if [ -f "$SCRIPT_DIR/completion_jvv.sh" ]; then + source "$SCRIPT_DIR/completion_jvv.sh" +fi + +# Add bin directory to PATH +if [ -d "$SCRIPT_DIR/bin" ]; then + export PATH="$SCRIPT_DIR/bin:$PATH" +fi diff --git a/scripts/completion_jv.sh b/scripts/completion_jv.sh new file mode 100755 index 0000000..f8613da --- /dev/null +++ b/scripts/completion_jv.sh @@ -0,0 +1,136 @@ +#!/bin/bash + +_jv_completion() { + local cur prev words cword + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + words=("${COMP_WORDS[@]}") + cword=$COMP_CWORD + + # Current + local cmd="${words[0]}" + local subcmd="${words[1]}" + local subsubcmd="${words[2]}" + + # Subcommands + local base_commands="create init direct unstain account update sheet here import export in out move mv docs exit use sheets accounts as make drop track hold throw" + + # Subcommands - Account + local account_commands="list as add remove movekey mvkey mvk help" + + # Subcommands - Sheet + local sheet_commands="list use exit make drop help" + + # Subcommands - Sheet + local sheet_commands="list use exit make drop help" + + # Completion subcommands + if [[ $cword -eq 1 ]]; then + COMPREPLY=($(compgen -W "$base_commands" -- "$cur")) + return 0 + fi + + # Completion account + if [[ "$subcmd" == "account" || "$subcmd" == "acc" ]]; then + if [[ $cword -eq 2 ]]; then + COMPREPLY=($(compgen -W "$account_commands" -- "$cur")) + return 0 + fi + + case "$subsubcmd" in + "as"|"remove"|"mvkey"|"mvk"|"movekey") + if [[ $cword -eq 3 ]]; then + # Use jv account list --raw + local accounts + accounts=$($cmd account list --raw 2>/dev/null) + COMPREPLY=($(compgen -W "$accounts" -- "$cur")) + elif [[ $cword -eq 4 && ("$subsubcmd" == "mvkey" || "$subsubcmd" == "mvk" || "$subsubcmd" == "movekey") ]]; then + COMPREPLY=($(compgen -f -- "$cur")) + fi + ;; + "-"|"rm") + if [[ $cword -eq 3 ]]; then + local accounts + accounts=$($cmd account list --raw 2>/dev/null) + COMPREPLY=($(compgen -W "$accounts" -- "$cur")) + fi + ;; + esac + return 0 + fi + + # Completion sheet + if [[ "$subcmd" == "sheet" || "$subcmd" == "sh" ]]; then + if [[ $cword -eq 2 ]]; then + COMPREPLY=($(compgen -W "$sheet_commands" -- "$cur")) + return 0 + fi + + case "$subsubcmd" in + # Use jv sheet list --raw --all/--other + "use"|"drop") + if [[ $cword -eq 3 ]]; then + local sheets + sheets=$($cmd sheet list --raw 2>/dev/null) + COMPREPLY=($(compgen -W "$sheets" -- "$cur")) + fi + ;; + "make") + if [[ $cword -eq 3 ]]; then + local all_sheets + all_sheets=$($cmd sheet list --all --raw 2>/dev/null) + COMPREPLY=($(compgen -W "$all_sheets" -- "$cur")) + fi + ;; + esac + return 0 + fi + + # aliases + case "$subcmd" in + "as") + if [[ $cword -eq 2 ]]; then + local accounts + accounts=$($cmd account list --raw 2>/dev/null) + COMPREPLY=($(compgen -W "$accounts" -- "$cur")) + fi + ;; + "use") + if [[ $cword -eq 2 ]]; then + local sheets + sheets=$($cmd sheet list --raw 2>/dev/null) + COMPREPLY=($(compgen -W "$sheets" -- "$cur")) + fi + ;; + "make") + if [[ $cword -eq 2 ]]; then + local all_sheets + all_sheets=$($cmd sheet list --all --raw 2>/dev/null) + COMPREPLY=($(compgen -W "$all_sheets" -- "$cur")) + fi + ;; + "drop") + if [[ $cword -eq 2 ]]; then + local sheets + sheets=$($cmd sheet list --raw 2>/dev/null) + COMPREPLY=($(compgen -W "$sheets" -- "$cur")) + fi + ;; + "docs") + if [[ $cword -eq 2 ]]; then + local docs + docs=$($cmd docs list --raw 2>/dev/null) + COMPREPLY=($(compgen -W "$docs" -- "$cur")) + fi + ;; + "move"|"mv") + COMPREPLY=($(compgen -f -- "$cur")) + ;; + "import"|"export"|"in"|"out"|"track"|"hold"|"throw") + COMPREPLY=($(compgen -f -- "$cur")) + ;; + esac +} + +complete -F _jv_completion jv diff --git a/scripts/completion_jvv.sh b/scripts/completion_jvv.sh new file mode 100755 index 0000000..7e8bb7a --- /dev/null +++ b/scripts/completion_jvv.sh @@ -0,0 +1,77 @@ +#!/bin/bash + +_jvv_completion() { + local cur prev words cword + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + words=("${COMP_WORDS[@]}") + cword=$COMP_CWORD + + # Current + local cmd="${words[0]}" + local subcmd="${words[1]}" + local subsubcmd="${words[2]}" + + # Subcommands + local base_commands="create init here member service listen members -c -i -H -m -l -M" + + # Subcommands - Member + local member_commands="register remove list help + - ls" + + # Subcommands - Service + local service_commands="listen help" + + # Completion subcommands + if [[ $cword -eq 1 ]]; then + COMPREPLY=($(compgen -W "$base_commands" -- "$cur")) + return 0 + fi + + # Completion member + if [[ "$subcmd" == "member" || "$subcmd" == "-m" ]]; then + if [[ $cword -eq 2 ]]; then + COMPREPLY=($(compgen -W "$member_commands" -- "$cur")) + return 0 + fi + + case "$subsubcmd" in + "remove"|"-") + if [[ $cword -eq 3 ]]; then + # Use jvv member list --raw + local members + members=$($cmd member list --raw 2>/dev/null) + COMPREPLY=($(compgen -W "$members" -- "$cur")) + fi + ;; + esac + return 0 + fi + + # Completion service + if [[ "$subcmd" == "service" ]]; then + if [[ $cword -eq 2 ]]; then + COMPREPLY=($(compgen -W "$service_commands" -- "$cur")) + return 0 + fi + return 0 + fi + + # aliases + case "$subcmd" in + "-m") + if [[ $cword -eq 2 ]]; then + COMPREPLY=($(compgen -W "$member_commands" -- "$cur")) + fi + ;; + "listen"|"-l") + # listen command has no arguments to complete + ;; + "members"|"-M") + # members command has no arguments to complete + ;; + esac +} + +# Register completion function +complete -F _jvv_completion jvv -- cgit