Advertisement
opexxx

cdd

Jul 8th, 2014
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.21 KB | None | 0 0
  1. # vim: ft=sh
  2. # https://github.com/m4i/cdd
  3.  
  4. if [ -z "$CDD_FILE" ]; then
  5.   CDD_FILE=~/.cdd
  6. fi
  7. export CDD_FILE
  8.  
  9. if [ -z "$CDD_AUTO" ]; then
  10.   CDD_AUTO=1
  11. fi
  12. export CDD_AUTO
  13.  
  14. CDD_SESSION=
  15. CDD_WINDOW=
  16. CDD_PANE=
  17.  
  18. if [ -n "$TMUX" ]; then
  19.   _cdd_key="$(tmux display -p '#S,#I.#P')"
  20.   _cdd_window_pane="${_cdd_key##*,}"
  21.   CDD_SESSION="${_cdd_key%,*}"
  22.   CDD_WINDOW="${_cdd_window_pane%%.*}"
  23.   CDD_PANE="${_cdd_window_pane#*.}"
  24.   if [[ $CDD_SESSION != *[^0-9]* ]]; then
  25.     CDD_SESSION=_
  26.   fi
  27.   unset _cdd_key _cdd_window_pane
  28.  
  29. elif [ -n "$STY" ]; then
  30.   _cdd_host="$(hostname)"
  31.   CDD_SESSION="${STY#*.}"
  32.   if echo "$CDD_SESSION" | grep "^[^.]\+\.${_cdd_host%%.*}$" >/dev/null; then
  33.     CDD_SESSION=_
  34.   fi
  35.   CDD_WINDOW="$WINDOW"
  36.   CDD_PANE=0
  37.   unset _cdd_host
  38. fi
  39.  
  40. CDD_SESSION="$(echo "$CDD_SESSION" | sed 's/:/_/g')"
  41. export CDD_SESSION CDD_WINDOW CDD_PANE
  42.  
  43.  
  44. cdd() {
  45.   if [ $# -eq 0 ]; then
  46.     _cdd_cd last
  47.     return $?
  48.   fi
  49.  
  50.   case "$1" in
  51.     help|--help|-h)
  52.       _cdd_help
  53.       return
  54.       ;;
  55.     add)
  56.       if [ $# -eq 2 -o $# -eq 3 ]; then
  57.         _cdd_add "$2" "$3"
  58.         return $?
  59.       fi
  60.       ;;
  61.     delete)
  62.       if [ $# -eq 2 ]; then
  63.         _cdd_delete "$2"
  64.         return $?
  65.       fi
  66.       ;;
  67.     *)
  68.       if [ $# -eq 1 ]; then
  69.         _cdd_cd "$1"
  70.         return $?
  71.       fi
  72.   esac
  73.  
  74.   _cdd_help
  75.   return 1
  76. }
  77.  
  78.  
  79. _cdd_chpwd() {
  80.   (($CDD_AUTO)) || return
  81.  
  82.   if [ -n "$CDD_WINDOW" ]; then
  83.     _cdd_register "$CDD_SESSION,$CDD_WINDOW.$CDD_PANE" "$PWD"
  84.   fi
  85.   _cdd_register last "$PWD"
  86. }
  87.  
  88. # for compatibility with original cdd
  89. alias _reg_pwd_screennum=_cdd_chpwd
  90.  
  91.  
  92. _cdd_help() {
  93.   cat <<'__EOT__' >&2
  94. Usage:
  95.   cdd [last] move to the last directory
  96.   cdd <key> move to the <key> directory
  97.   cdd add <key> [directory] add the directory to $CDD_FILE
  98.   cdd delete <key> delete the directory from $CDD_FILE
  99.   cdd help|--help|-h print this help message
  100.  
  101. __EOT__
  102.   if [ -f "$CDD_FILE" ]; then
  103.     sort "$CDD_FILE" >&2
  104.   fi
  105. }
  106.  
  107.  
  108. _cdd_cd() {
  109.   if [ ! -f "$CDD_FILE" ]; then
  110.     echo "cdd: $CDD_FILE: No such file" >&2
  111.     return 2
  112.   fi
  113.  
  114.   local key="$(_cdd_canonicalize_key "$1")"
  115.  
  116.   local match="$(grep "^$key:" "$CDD_FILE")"
  117.   if [ -n "$match" ]; then
  118.     local dir="${match##*:}"
  119.     echo "cd $dir"
  120.     cd "$(echo "$dir" | sed "s;^~;$HOME;")"
  121.  
  122.   else
  123.     echo "cdd: $key: No such key"$'\n' >&2
  124.     _cdd_help
  125.     return 3
  126.   fi
  127. }
  128.  
  129.  
  130. _cdd_add() {
  131.   local key="${1%%:*}"
  132.   local dir="$2"
  133.  
  134.   local _dir
  135.   if [ -n "$dir" ]; then
  136.     local _cdd_auto="$CDD_AUTO"
  137.     CDD_AUTO=0
  138.     _dir="$(cd "$dir" >/dev/null 2>&1 && pwd)"; result=$?
  139.     CDD_AUTO="$_cdd_auto"
  140.     if [ "$result" -ne 0 ]; then
  141.       echo "cdd: can not cd $dir"$'\n' >&2
  142.       _cdd_help
  143.       return 4
  144.     fi
  145.   else
  146.     _dir="$PWD"
  147.   fi
  148.  
  149.   _cdd_register "$key" "$_dir"
  150. }
  151.  
  152.  
  153. _cdd_delete() {
  154.   if [ ! -f "$CDD_FILE" ]; then
  155.     echo "cdd: $CDD_FILE: No such file" >&2
  156.     return 2
  157.   fi
  158.  
  159.   local key="$(_cdd_canonicalize_key "$1")"
  160.  
  161.   if grep "^$key:" "$CDD_FILE" >/dev/null; then
  162.     sed -i.tmp -e "/^$key\:/d" "$CDD_FILE"
  163.     rm -f "$CDD_FILE.tmp"
  164.   else
  165.     echo "cdd: $key: No such key"$'\n' >&2
  166.     _cdd_help
  167.     return 3
  168.   fi
  169. }
  170.  
  171.  
  172. _cdd_canonicalize_key() {
  173.   local key="${1%%:*}"
  174.  
  175.   if echo "$key" | grep '^\(.\+,\)\?[0-9]\+\.\?$' >/dev/null; then
  176.     if [[ $key != *. ]]; then
  177.       key="$key."
  178.     fi
  179.     key="${key}0"
  180.   fi
  181.   if echo "$key" | grep '^[0-9]\+.[0-9]\+$' >/dev/null; then
  182.     key="${CDD_SESSION:-_},$key"
  183.   fi
  184.  
  185.   echo "$key"
  186. }
  187.  
  188.  
  189. _cdd_register() {
  190.   local key="$1"
  191.   local dir="$2"
  192.  
  193.   if [ -f "$CDD_FILE" ]; then
  194.     sed -i.tmp -e "/^$key\:/d" "$CDD_FILE"
  195.     rm -f "$CDD_FILE.tmp"
  196.   fi
  197.  
  198.   dir="$(echo "$dir" | sed "s;^$HOME;~;")"
  199.   echo "$key:$dir" >>"$CDD_FILE"
  200. }
  201.  
  202.  
  203. _cdd_comp() {
  204.   local mode="$1"
  205.   local -i CURRENT="$2"
  206.   local command="$3"
  207.  
  208.   [ -f "$CDD_FILE" ] || return
  209.  
  210.   if (( CURRENT == 3 )) && [[ $command = delete ]] || (( CURRENT == 2 )); then
  211.     _cdd_opts="$(sed "s/^${CDD_SESSION:-_},//" "$CDD_FILE")"
  212.     _cdd_descr='cdd directory'
  213.  
  214.     if [ "$mode" = zsh ]; then
  215.       _cdd_opts="$(echo "$_cdd_opts" | sed 's/\.0:/:/')"
  216.     fi
  217.  
  218.   elif (( CURRENT == 3 )) && [[ $command = add ]]; then
  219.     _cdd_opts="$(grep -v '^[^:]\+,[0-9]\+\.[0-9]\+:\|^last:' "$CDD_FILE")"
  220.     _cdd_descr='cdd key'
  221.   fi
  222. }
  223.  
  224.  
  225. if [ -n "$ZSH_VERSION" ]; then
  226.  
  227.   _cdd() {
  228.     typeset -A opt_args
  229.     local context state line
  230.  
  231.     _arguments '*: :->args' && return
  232.     [[ $state = args ]] || return
  233.  
  234.     local command="$line[1]"
  235.     local _cdd_opts= _cdd_descr= ret=1
  236.     local -a opts
  237.  
  238.     _cdd_comp zsh "$CURRENT" "$command"
  239.     if [ -n "$_cdd_opts" ]; then
  240.       local _ifs="$IFS"
  241.       IFS=$'\n'
  242.       opts=($(echo "$_cdd_opts"))
  243.       IFS="$_ifs"
  244.       _describe "$_cdd_descr" opts && ret=0
  245.     fi
  246.  
  247.     if (( CURRENT == 2 )); then
  248.       local _cdd_file="$(echo "$CDD_FILE" | sed "s;^$HOME;~;")"
  249.       opts=(
  250.         "add:add the directory to $_cdd_file"
  251.         "delete:delete the directory from $_cdd_file"
  252.       )
  253.       _describe -t commands 'cdd command' opts && ret=0
  254.  
  255.     elif (( CURRENT == 4 )) && [[ $command = add ]]; then
  256.       _path_files -/ 'directory' && ret=0
  257.     fi
  258.  
  259.     return ret
  260.   }
  261.   compdef _cdd cdd
  262.  
  263. elif [ -n "$BASH_VERSION" ]; then
  264.  
  265.   _cdd() {
  266.     COMPREPLY=()
  267.     local -i CURRENT=$(($COMP_CWORD + 1))
  268.     local command="${COMP_WORDS[1]}"
  269.     local _cdd_opts= _cdd_descr=
  270.     local cur="${COMP_WORDS[COMP_CWORD]}"
  271.  
  272.     _cdd_comp bash "$CURRENT" "$command"
  273.     if [ -n "$_cdd_opts" ]; then
  274.       _cdd_opts="$(echo "$_cdd_opts" | sed -e 's/ /\\\\ /g' -e 's/$/ /')"
  275.     fi
  276.  
  277.     if (( CURRENT == 2 )); then
  278.       _cdd_opts="$_cdd_opts"$'\n''add '$'\n''delete '
  279.  
  280.     elif [[ $command = add ]]; then
  281.       # cdd add foo:/foo =>
  282.       # "cdd" "add" "foo" ":" "/foo" ||
  283.       # "cdd" "add" "foo:/foo"
  284.       if (( CURRENT == 6 )) && [[ ${COMP_WORDS[3]} = : ]] || \
  285.          (( CURRENT == 4 )); then
  286.         cur="$(echo "$cur" | sed "s;^~;$HOME;")"
  287.         _cdd_opts="$(compgen -d "$cur" | sed -e 's/ /\\\\ /g' -e 's;$;/;')"
  288.       fi
  289.     fi
  290.  
  291.     if [ -n "$_cdd_opts" ]; then
  292.       local IFS=$'\n'
  293.       COMPREPLY=($(compgen -W "$_cdd_opts" -- "$cur"))
  294.     fi
  295.   }
  296.   complete -F _cdd -o nospace cdd
  297.  
  298. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement