sistema_progs

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

finder (2639B)


      1 #!/usr/bin/env bash
      2 
      3 # Description: Run custom search and list results in smart context
      4 #
      5 # Note: This plugin retains search history
      6 #
      7 # Usage:
      8 #   Run plugin and enter e.g. "-size +10M" to list files in current
      9 #   directory larger than 10M. By default entered expressions are
     10 #   interpreted as arguments to find. Results have to be NUL
     11 #   terminated which is done by default for find. Alternatively one
     12 #   can prepend a '$' to run a custom search program such as fd or
     13 #   ripgrep. Entered expressions will be saved in history file to
     14 #   be listed as bookmarks and and can be entered by index and edited.
     15 #
     16 # Shell: Bash
     17 # Author: Arun Prakash Jana, Luuk van Baal
     18 TMPDIR="${TMPDIR:-/tmp}"
     19 NNN_FINDHIST="${NNN_FINDHIST:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/finderbms}"
     20 NNN_FINDHISTLEN="${NNN_FINDHISTLEN:-10000}"
     21 
     22 printexamples() {
     23     printf -- "-maxdepth 1 -name pattern
     24 -maxdepth 1 -size +100M
     25 \$fd -0 pattern
     26 \$fd -0 -d 2 -S +100M
     27 \$grep -rlZ pattern
     28 \$rg -l0 pattern
     29 \$fzf -m | tr '\\\n' '\\\0'\n"
     30 }
     31 
     32 printexprs() {
     33     for ((i = "$1"; i < ${#fexprs[@]}; i++)); do
     34         printf '%s\t%s\n' "$((i + 1))" "${fexprs[$i]}"
     35     done
     36 }
     37 
     38 mapexpr() {
     39     if [ "$fexpr" -eq "$fexpr" ] 2>/dev/null; then
     40         fexpr=${fexprs[$((fexpr - 1))]}
     41         read -r -e -p "Search expression: " -i "$fexpr" fexpr
     42     else
     43         return 1
     44     fi
     45 }
     46 
     47 readexpr() {
     48     case "$fexpr" in
     49         h)   clear
     50              printf "Examples:\n"
     51              mapfile -t fexprs < <(printexamples)
     52              printexprs 0
     53              read -r -p "Search expression or index: " fexpr
     54              mapexpr
     55              [ -n "$fexpr" ] && readexpr ;;
     56         \$*) cmd="${fexpr:1}" ;;
     57         *)   mapexpr && readexpr
     58              cmd="find . $fexpr -print0" ;;
     59     esac
     60 }
     61 
     62 clear
     63 [ -f "$NNN_FINDHIST" ] || printexamples > "$NNN_FINDHIST"
     64 
     65 mapfile -t fexprs < <(sort "$NNN_FINDHIST" | uniq -c | sort -nr | head -n5 |\
     66   awk '{for (i=2; i<NF; i++) printf $i " "; print $NF}')
     67 printf "Most used search expressions:\n"
     68 printexprs 0
     69 
     70 mapfile -t -O"$i" fexprs < <(tac "$NNN_FINDHIST" | awk '!a[$0]++' | head -n5)
     71 printf "Most recently used search expressions:\n"
     72 printexprs "$i"
     73 read -r -p "Search expression or index (h for help): " fexpr
     74 
     75 mapexpr
     76 
     77 if [ -n "$fexpr" ]; then
     78     printf "+l" > "$NNN_PIPE"
     79     while :; do
     80         readexpr
     81         eval "$cmd" > "$NNN_PIPE" && break
     82         read -r -e -p "Search expression: " -i "$fexpr" fexpr
     83     done
     84     if [ -n "$fexpr" ]; then
     85         tail -n"$NNN_FINDHISTLEN" "$NNN_FINDHIST" > "$TMPDIR/finderbms"
     86         printf "%s\n" "$fexpr" >> "$TMPDIR/finderbms"
     87         mv "$TMPDIR/finderbms" "$NNN_FINDHIST"
     88     fi
     89 fi