sistema_progs

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

cdpath (1865B)


      1 #!/usr/bin/env sh
      2 
      3 # Description: 'cd' to the directory from CDPATH
      4 #
      5 # Details: If the CDPATH environmet 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 . "$(dirname "$0")"/.nnn-plugin-helper
     20 
     21 # Get a list of (symbolic links to) directories for every element of CDPATH
     22 get_dirs() {
     23     IFS=':'
     24     for path in $CDPATH; do
     25         for entry in "$path"/*; do
     26             if [ -d "$entry" ]; then
     27                 name=$(basename "$entry" | grep -o '^.\{1,24\}')
     28                 if [ -h "$entry" ]; then
     29                     slink=$(ls -dl -- "$entry")
     30                     entry=${slink#*" $entry -> "}
     31                 fi
     32                 printf "%-24s :%s\n" "${name}" "$entry"
     33             fi
     34         done
     35     done
     36 }
     37 
     38 abort() {
     39     echo "$1"
     40     read -r _
     41     exit 1
     42 }
     43 
     44 if [ -z "$CDPATH" ]; then
     45     CDPATH="${XDG_CONFIG_HOME:-$HOME/.config}/nnn/bookmarks"
     46     [ -d "$CDPATH" ] || abort "CDPATH is not set and there is no \"$CDPATH\" directory"
     47 fi
     48 
     49 dir_list=$(get_dirs)
     50 [ -n "$dir_list" ] || abort "There are no directories to choose from. Check your \"$CDPATH\"."
     51 
     52 dir=$(echo "$dir_list" | fzf --nth=1 --delimiter=':' | awk -F: 'END { print $2 }')
     53 if [ -n "$dir" ]; then
     54     nnn_cd "$dir" 0
     55 fi