Skip to content

Commit

Permalink
completion sepertae files 2 ways
Browse files Browse the repository at this point in the history
  • Loading branch information
grimcore committed Sep 13, 2024
1 parent 2c9ae17 commit f453e93
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 121 deletions.
46 changes: 46 additions & 0 deletions _ani-cli-bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

#!/bin/bash

_ani_cli_autocomplete() {
local cur prev opts

# Get the current and previous words in the command line
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"

# Define the main options
opts="
-c --continue
-d --download
-D --delete
-s --syncplay
-S --select-nth
-q --quality
-v --vlc
-V --version
-h --help
-e --episode
--dub
--rofi
--skip
--no-detach
-N --nextep-countdown
-U --update
"

# Check for conditional options
if [[ "${COMP_WORDS[@]}" =~ "--no-detach" ]]; then
opts+=" --exit-after-play"
fi

if [[ "${COMP_WORDS[@]}" =~ "-e" || "${COMP_WORDS[@]}" =~ "--episode" ]]; then
opts+=" -r --range"
fi

# Use `compgen` to generate the options based on the current word
COMPREPLY=($(compgen -W "${opts}" -- "$cur"))
}

# Register the completion function for `ani-cli`
complete -F _ani_cli_autocomplete ani-cli
31 changes: 31 additions & 0 deletions _ani-cli-zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

#!/bin/zsh

# Enable autocompletion for zsh
autoload -U compinit && compinit

_ani-cli() {
_arguments \
'(-c --continue)'{-c,--continue}'[Continue watching from history]' \
'(-d --download)'{-d,--download}'[Download the video instead of playing it]' \
'(-D --delete)'{-D,--delete}'[Delete the downloaded video]' \
'(-s --syncplay)'{-s,--syncplay}'[Use Syncplay to watch with friends]' \
'(-S --select-nth)'{-S,--select-nth}'[Select nth entry]' \
'(-q --quality)'{-q,--quality}'[Specify the video quality]' \
'(-v --vlc)'{-v,--vlc}'[Use VLC to play the video]' \
'(-V --version)'{-V,--version}'[Show the version of the script]' \
'(-h --help)'{-h,--help}'[Show help this help message]' \
'(-e --episode)'{-e,--episode}'[Specify the episode to watch]' \
'(-e --episode)'{-r,--range}'[Specify the range of episodes to watch]' \
'--dub[Dub the video]' \
'--rofi[Use rofi instead of fzf]' \
'--skip[Use ani-skip to skip the intro of the episode (mpv only)]' \
"--no-detach[ Don't detach the player (useful for in-terminal playback, mpv only)]" \
'(--no-detach)--exit-after-play[Exit after the video is played]' \
'--skip-title[Use given title as ani-skip query]' \
'(-N --nextep-countdown)'{-N,--nextep-countdown}'[Countdown to next episode]' \
'(-U --update)'{-U,--update}'[Update the script]'
}

# Register the completion function for `ani-cli`
compdef _ani-cli ani-cli
121 changes: 0 additions & 121 deletions ani-cli
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ help_info() {
Display a countdown to the next episode
-U, --update
Update the script
--completion
Some example usages:
%s -q 720p banana fish
%s --skip --skip-title \"one piece\" -S 2 one piece
Expand Down Expand Up @@ -109,125 +108,6 @@ update_script() {
exit 0
}

print_completion() {
# Detect the current shell
detect_shell() {
local shell_name
shell_name=$(basename "$SHELL")

case "$shell_name" in
bash)
echo "bash"
;;
zsh)
echo "zsh"
;;
*)
echo "unsupported"
;;
esac
}

zsh_completion() {
cat << 'EOF'
#!/bin/zsh
# Enable autocompletion for zsh
autoload -U compinit && compinit
_ani-cli() {
_arguments \
'(-c --continue)'{-c,--continue}'[Continue watching from history]' \
'(-d --download)'{-d,--download}'[Download the video instead of playing it]' \
'(-D --delete)'{-D,--delete}'[Delete the downloaded video]' \
'(-s --syncplay)'{-s,--syncplay}'[Use Syncplay to watch with friends]' \
'(-S --select-nth)'{-S,--select-nth}'[Select nth entry]' \
'(-q --quality)'{-q,--quality}'[Specify the video quality]' \
'(-v --vlc)'{-v,--vlc}'[Use VLC to play the video]' \
'(-V --version)'{-V,--version}'[Show the version of the script]' \
'(-h --help)'{-h,--help}'[Show help this help message]' \
'(-e --episode)'{-e,--episode}'[Specify the episode to watch]' \
'(-e --episode)'{-r,--range}'[Specify the range of episodes to watch]' \
'--dub[Dub the video]' \
'--rofi[Use rofi instead of fzf]' \
'--skip[Use ani-skip to skip the intro of the episode (mpv only)]' \
"--no-detach[ Don't detach the player (useful for in-terminal playback, mpv only)]" \
'(--no-detach)--exit-after-play[Exit after the video is played]' \
'--skip-title[Use given title as ani-skip query]' \
'(-N --nextep-countdown)'{-N,--nextep-countdown}'[Countdown to next episode]' \
'(-U --update)'{-U,--update}'[Update the script]'
}
# Register the completion function for `ani-cli`
compdef _ani-cli ani-cli
EOF
}

bash_completion() {
cat << 'EOF'
#!/bin/bash
_ani_cli_autocomplete() {
local cur prev opts
# Get the current and previous words in the command line
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
# Define the main options
opts="
-c --continue
-d --download
-D --delete
-s --syncplay
-S --select-nth
-q --quality
-v --vlc
-V --version
-h --help
-e --episode
--dub
--rofi
--skip
--no-detach
-N --nextep-countdown
-U --update
"
# Check for conditional options
if [[ "${COMP_WORDS[@]}" =~ "--no-detach" ]]; then
opts+=" --exit-after-play"
fi
if [[ "${COMP_WORDS[@]}" =~ "-e" || "${COMP_WORDS[@]}" =~ "--episode" ]]; then
opts+=" -r --range"
fi
# Use `compgen` to generate the options based on the current word
COMPREPLY=($(compgen -W "${opts}" -- "$cur"))
}
# Register the completion function for `ani-cli`
complete -F _ani_cli_autocomplete ani-cli
EOF
}

# Apply the appropriate setup based on the detected shell
shell=$(detect_shell)

case "$shell" in
bash)
bash_completion
;;
zsh)
zsh_completion
;;
*)
echo "Autocompletion is not yet supported for $shell. Sorry!"
;;
esac
}
# checks if dependencies are present
dep_ch() {
for dep; do
Expand Down Expand Up @@ -538,7 +418,6 @@ while [ $# -gt 0 ]; do
;;
-N | --nextep-countdown) search=nextep ;;
-U | --update) update_script ;;
--completion) print_completion && die ;;
*) query="$(printf "%s" "$query $1" | sed "s|^ ||;s| |+|g")" ;;
esac
shift
Expand Down
132 changes: 132 additions & 0 deletions completion_script
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#!/bin/sh

# #############################################################################
# -----------------------< shell detecting completion >-----------------------
# #############################################################################

# Detect the current shell
detect_shell() {
shell_name=$(basename "$SHELL")

case "$shell_name" in
bash)
echo "bash"
;;
zsh)
echo "zsh"
;;
*)
echo "unsupported"
;;
esac
}

# Apply the appropriate setup based on the detected shell
shell=$(detect_shell)

print_completion () {
case "$shell" in
bash)
cat << 'EOF'
#!/bin/bash
_ani_cli_autocomplete() {
local cur prev opts
# Get the current and previous words in the command line
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
# Define the main options
opts="
-c --continue
-d --download
-D --delete
-s --syncplay
-S --select-nth
-q --quality
-v --vlc
-V --version
-h --help
-e --episode
--dub
--rofi
--skip
--no-detach
-N --nextep-countdown
-U --update
"
# Check for conditional options
if [[ "${COMP_WORDS[@]}" =~ "--no-detach" ]]; then
opts+=" --exit-after-play"
fi
if [[ "${COMP_WORDS[@]}" =~ "-e" || "${COMP_WORDS[@]}" =~ "--episode" ]]; then
opts+=" -r --range"
fi
# Use `compgen` to generate the options based on the current word
COMPREPLY=($(compgen -W "${opts}" -- "$cur"))
}
# Register the completion function for `ani-cli`
complete -F _ani_cli_autocomplete ani-cli
EOF
;;
zsh)
cat << 'EOF'
#!/bin/zsh
# Enable autocompletion for zsh
autoload -U compinit && compinit
_ani-cli() {
_arguments \
'(-c --continue)'{-c,--continue}'[Continue watching from history]' \
'(-d --download)'{-d,--download}'[Download the video instead of playing it]' \
'(-D --delete)'{-D,--delete}'[Delete the downloaded video]' \
'(-s --syncplay)'{-s,--syncplay}'[Use Syncplay to watch with friends]' \
'(-S --select-nth)'{-S,--select-nth}'[Select nth entry]' \
'(-q --quality)'{-q,--quality}'[Specify the video quality]' \
'(-v --vlc)'{-v,--vlc}'[Use VLC to play the video]' \
'(-V --version)'{-V,--version}'[Show the version of the script]' \
'(-h --help)'{-h,--help}'[Show help this help message]' \
'(-e --episode)'{-e,--episode}'[Specify the episode to watch]' \
'(-e --episode)'{-r,--range}'[Specify the range of episodes to watch]' \
'--dub[Dub the video]' \
'--rofi[Use rofi instead of fzf]' \
'--skip[Use ani-skip to skip the intro of the episode (mpv only)]' \
"--no-detach[ Don't detach the player (useful for in-terminal playback, mpv only)]" \
'(--no-detach)--exit-after-play[Exit after the video is played]' \
'--skip-title[Use given title as ani-skip query]' \
'(-N --nextep-countdown)'{-N,--nextep-countdown}'[Countdown to next episode]' \
'(-U --update)'{-U,--update}'[Update the script]'
}
# Register the completion function for `ani-cli`
compdef _ani-cli ani-cli
EOF
;;
*)
echo "Autocompletion is not yet supported for $shell. Sorry!"
;;
esac
}

# Generate and store the completion script
print_completion > ~/_ani-cli-completion

# Append the source command to the appropriate shell configuration file
case "$shell" in
bash)
echo "source ~/_ani-cli-completion" >> ~/.bashrc
;;
zsh)
echo "source ~/_ani-cli-completion" >> ~/.zshrc
;;
*)
echo "Shell $shell is not supported for autocompletion setup."
;;
esac

0 comments on commit f453e93

Please sign in to comment.