#!/bin/bash # File: autocomplete-rescan.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"