Parent directory

_wd.sh

2896 bytes
  1#compdef wd
  2
  3zstyle ':completion::complete:wd:*:descriptions' format '%B%d%b'
  4zstyle ':completion::complete:wd:*:commands' group-name commands
  5zstyle ':completion::complete:wd:*:warp_points' group-name warp_points
  6zstyle ':completion::complete:wd::' list-grouped
  7
  8zmodload zsh/mapfile
  9
 10function _wd() {
 11  local WD_CONFIG=${WD_CONFIG:-$HOME/.warprc}
 12  local ret=1
 13
 14  local -a commands
 15  local -a warp_points
 16
 17  warp_points=( "${(f)mapfile[$WD_CONFIG]//$HOME/~}" )
 18
 19  typeset -A points
 20  while read -r line
 21  do
 22    arr=(${(s,:,)line})
 23    name=${arr[1]}
 24    target_path=${arr[2]}
 25
 26    # replace ~ from path to fix completion (#17)
 27    target_path=${target_path/#\~/$HOME}
 28
 29    points[$name]=$target_path
 30  done < $WD_CONFIG
 31
 32  commands=(
 33    'add:Adds the current working directory to your warp points'
 34    'addcd:Adds a directory to your warp points'
 35    'add!:Overwrites existing warp point'
 36    'export:Export warp points as static named directories'
 37    'rm:Removes the given warp point'
 38    'list:Outputs all stored warp points'
 39    'ls:Show files from given warp point'
 40    'open:Open warp point in the default file explorer'
 41    'path:Show path to given warp point'
 42    'show:Outputs all warp points that point to the current directory or shows a specific target directory for a point'
 43    'help:Show this extremely helpful text'
 44    'clean:Remove points warping to nonexistent directories'
 45    'clean!:Remove nonexistent directories without confirmation'
 46    '..:Go back to last directory'
 47  )
 48
 49  _arguments -C \
 50    '1: :->first_arg' \
 51    '2: :->second_arg' && ret=0
 52
 53  local target=$words[2]
 54
 55  case $state in
 56    first_arg)
 57      _describe -t warp_points "Warp points" warp_points && ret=0
 58      _describe -t commands "Commands" commands && ret=0
 59      ;;
 60    second_arg)
 61      case $target in
 62        add\!|rm)
 63          _describe -t points "Warp points" warp_points && ret=0
 64          ;;
 65        add)
 66          _message 'Write the name of your warp point' && ret=0
 67          ;;
 68	addcd)
 69          _message 'Write the name of your path' && ret=0
 70          ;;
 71        show)
 72          _describe -t points "Warp points" warp_points && ret=0
 73          ;;
 74        ls)
 75          _describe -t points "Warp points" warp_points && ret=0
 76          ;;
 77        open)
 78          _describe -t points "Warp points" warp_points && ret=0
 79          ;;
 80        path)
 81          _describe -t points "Warp points" warp_points && ret=0
 82          ;;
 83        *)
 84          if [[ -v points[$target] ]]; then
 85            # complete sub directories from the warp point
 86            _path_files -W "(${points[$target]})" -/ && ret=0
 87          fi
 88
 89          # don't complete anything if warp point is not valid
 90          ;;
 91      esac
 92      ;;
 93  esac
 94
 95  return $ret
 96}
 97
 98_wd "$@"
 99
100# Local Variables:
101# mode: Shell-Script
102# sh-indentation: 2
103# indent-tabs-mode: nil
104# sh-basic-offset: 2
105# End:
106# vim: ft=zsh sw=2 ts=2 et