sistema_progs

Programas para customizar o meu entorno de traballo nos meus equipos persoais
Log | Files | Refs

cdpath (1901B)


      1 #!/usr/bin/env sh
      2 
      3 # Description: 'cd' to the directory from CDPATH
      4 #
      5 # Details: If the CDPATH environment variable is not set, the default value of
      6 #          ${XDG_CONFIG_HOME:-$HOME/.config}/nnn/bookmarks will be used.
      7 #          You can create this directory and fill it with symbolic links to your
      8 #          favorite directories. It's a good idea to add it to CDPATH so that it
      9 #          could also be used from the command line outside of nnn.
     10 #          The fzf search is done on the directory basename (the first column).
     11 #
     12 #          This plugin is an extended version of the bookmarks plugin.
     13 #          If you set your CDPATH to ${XDG_CACHE_HOME:-$HOME/.cache}/nnn/bookmarks
     14 #          or to the value of BOOKMARKS_DIR, you can use it as a bookmarks replacement.
     15 #
     16 # Shell: POSIX compliant
     17 # Author: Yuri Kloubakov
     18 
     19 # shellcheck disable=SC1090,SC1091
     20 . "$(dirname "$0")"/.nnn-plugin-helper
     21 
     22 # Get a list of (symbolic links to) directories for every element of CDPATH
     23 get_dirs() {
     24     IFS=':'
     25     for path in $CDPATH; do
     26         for entry in "$path"/*; do
     27             if [ -d "$entry" ]; then
     28                 name=$(basename "$entry" | grep -o '^.\{1,24\}')
     29                 if [ -h "$entry" ]; then
     30                     slink=$(ls -dl -- "$entry")
     31                     entry=${slink#*" $entry -> "}
     32                 fi
     33                 printf "%-24s :%s\n" "${name}" "$entry"
     34             fi
     35         done
     36     done
     37 }
     38 
     39 abort() {
     40     echo "$1"
     41     read -r _
     42     exit 1
     43 }
     44 
     45 if [ -z "$CDPATH" ]; then
     46     CDPATH="${XDG_CONFIG_HOME:-$HOME/.config}/nnn/bookmarks"
     47     [ -d "$CDPATH" ] || abort "CDPATH is not set and there is no \"$CDPATH\" directory"
     48 fi
     49 
     50 dir_list=$(get_dirs)
     51 [ -n "$dir_list" ] || abort "There are no directories to choose from. Check your \"$CDPATH\"."
     52 
     53 dir=$(echo "$dir_list" | fzf --nth=1 --delimiter=':' | awk -F: 'END { print $2 }')
     54 if [ -n "$dir" ]; then
     55     nnn_cd "$dir" 0
     56 fi