aboutsummaryrefslogtreecommitdiff
path: root/jellystack-autocomplete.bash
blob: 55ffad4faf8a6b3c342edaeb00b845feee514cee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/bash
# File: jellystack-autocomplete.bash
# Location: /mnt/public/Support/Programs/jellyfin/scripts/
# Author: bgstack15, pawamoy
# Startdate: 2024-01-28-1 21:40
# SPDX-License-Identifier: CC-BY-SA-4.0
# Title: Bash autocompletion for rescan-library
# Project: jellystack
# Purpose: Make it easy to choose a jellyfin library to rescan
# History:
# Usage:
#    dot-source it and then alias the script:
#       . /mnt/public/Support/Programs/jellyfin/autocmplete-rescan.bash
#       alias rescan-library=/mnt/public/Support/Programs/jellyfin/rescan-library.sh
# Reference:
#    1. https://stackoverflow.com/questions/44453510/how-to-autocomplete-a-bash-commandline-with-file-paths-from-a-specific-directory/47799826#47799826
#    2. https://stackoverflow.com/questions/1146098/properly-handling-spaces-and-quotes-in-bash-completion
# Improve:
# Dependencies:
#    password stored in ~/.jellyfin.password
#    jellystack.py frontend and jellystack_lib.py

_complete_specific_path() {
   # ripped from https://stackoverflow.com/questions/44453510/how-to-autocomplete-a-bash-commandline-with-file-paths-from-a-specific-directory/47799826#47799826 and modified for jellystack
   # alt which did not work: https://stackoverflow.com/questions/1146098/properly-handling-spaces-and-quotes-in-bash-completion
   /mnt/public/Support/Programs/jellyfin/scripts/jellystack.py --autocomplete 1>/dev/null # populates ~/.cache/jellystack
   # declare variables
   local _item _COMPREPLY _old_pwd
   thisdir=~/.cache/jellystack

   # if we already are in the completed directory, skip this part
   _old_pwd="${PWD}"
   # magic here: go the specific directory!
   pushd "${thisdir}" &>/dev/null || return

   # init completion and run _filedir inside specific directory
   _init_completion -s || return
   _filedir

   # iterate on original replies
   for _item in "${COMPREPLY[@]}"; do
      # this check seems complicated, but it handles the case
      # where you have files/dirs of the same name
      # in the current directory and in the completed one:
      # we want only one "/" appended
      #if [ -d "${_item}" ] && [[ "${_item}" != */ ]] && [ ! -d "${_old_pwd}/${_item}" ]; then
      #   # append a slash if directory
      #   _COMPREPLY+=("${_item}/")
      #else
         _COMPREPLY+=("${_item}")
      #fi
   done

   # popd as early as possible
   popd &>/dev/null

   # if only one reply and it is a directory, don't append a space
   # (don't know why we must check for length == 2 though)
   if [ ${#_COMPREPLY[@]} -eq 2 ]; then
      if [[ "${_COMPREPLY}" == */ ]]; then
         compopt -o nospace
      fi
   fi

   # set the values in the right COMPREPLY variable
   COMPREPLY=( "${_COMPREPLY[@]}" )

   # clean up
   unset _COMPREPLY
   unset _item
}

complete -F _complete_specific_path rescan-library
alias rescan-library="/mnt/public/Support/Programs/jellyfin/scripts/rescan-library.sh"

_show_manager_confs() {
   # reference: vm4:/etc/bash_completion.d/docker-nfs-check
   local cur prev words cword;
   _init_completion || return
   _tmpfile1="$( mktemp )"
   # populate list
   find /mnt/public/Support/Programs/jellyfin/scripts/input -mindepth 1 -maxdepth 1 ! -type d -name '*.conf' -printf '%P\n' | sed -r -e 's/\.conf$//;' > "${_tmpfile1}"
   COMPREPLY=($( compgen -W "$( cat ${_tmpfile1} )" -- "$cur" | sed -r -e "/^${prev}/d;" ))
   command rm -rf "${_tmpfile1:-NOTHINGTODEL}" 1>/dev/null 2>&1
   return 0
} &&
complete -F _show_manager_confs manage-show
. /mnt/public/Support/Programs/jellyfin/scripts/show-manager.sh
alias manage-show="manage_show"
bgstack15