From 4bc8e1a8be7d3ac2b9788a1557afb8981c4aea1c Mon Sep 17 00:00:00 2001 From: Arun Prakash Jana Date: Sat, 29 May 2021 00:33:50 +0530 Subject: [PATCH] Rename plugin fzdirs to fzcd, retains fzcd features --- plugins/README.md | 3 +- plugins/fzcd | 79 +++++++++++++++++++++++++++++++++++++++------- plugins/fzdirs | 80 ----------------------------------------------- 3 files changed, 69 insertions(+), 93 deletions(-) delete mode 100755 plugins/fzdirs diff --git a/plugins/README.md b/plugins/README.md index 4cd176bc..c3c50117 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -25,8 +25,7 @@ Plugins extend the capabilities of `nnn`. They are _executable_ scripts (or bina | [dups](dups) | List non-empty duplicate files in current dir | bash | find, md5sum,
sort uniq xargs | | [finder](finder) | Run custom find command and list | sh | - | | [fixname](fixname) | Clean filename to be more shell-friendly [✓] | bash | sed | -| [fzcd](fzcd) | Change to the directory of a fuzzy-selected file/dir | sh | fzf | -| [fzdirs](fzdirs) | Fuzzy search multiple directories [✓] | sh | fzf, fd | +| [fzcd](fzcd) | Fuzzy search multiple dirs (or `$PWD`) and visit file [✓] | sh | fzf | | [fzhist](fzhist) | Fuzzy-select a cmd from history, edit in `$EDITOR` and run | sh | fzf, mktemp | | [fzopen](fzopen) | Fuzzy find file(s) in subtree to edit/open/pick | sh | fzf, xdg-open | | [fzplug](fzplug) | Fuzzy find, preview and run other plugins | sh | fzf | diff --git a/plugins/fzcd b/plugins/fzcd index 537e50e8..077456ab 100755 --- a/plugins/fzcd +++ b/plugins/fzcd @@ -1,18 +1,76 @@ #!/usr/bin/env sh -# Description: Run fzf and go to the directory of the file selected +# Description: Fuzzy search multiple locations read-in from a path-list file +# (or $PWD) and open the selected file's dir in a smart context. +# Dependencies: fzf, fd (only for multi-location search) +# +# Details: Paths in list file should be newline-separated absolute paths. +# Paths can be file paths; the script will scan the parent dirs. +# +# The path-list file precedence is: +# - "$1" (the hovered file) if it exists, is plain-text and the +# first line points to an existing file +# - "$LIST" if set below +# - "$2" (the current directory) [mimics plugin fzcd behaviour] +# +# The path-list file can be generated easily: +# - pick the (file)paths in picker mode to path-list file +# - OR, edit selection in nnn and save as path-list file +# +# The plugin clears nnn selection as the user can be tempted to delete +# duplicate files after finding copies and remove selection by mistake. # # Shell: POSIX compliant -# Author: Anna Arad +# Author: Anna Arad, Arun Prakash Jana + +IFS="$(printf '\n\r')" . "$(dirname "$0")"/.nnn-plugin-helper -if [ "$(cmd_exists fzf)" -eq "0" ]; then - sel=$(fzf) +CTX=+ +LIST="$LIST" + +if ! type fzf >/dev/null 2>&1; then + printf "fzf missing" + read -r _ + exit 1 +fi + +if [ -n "$1" ] && [ "$(file -b --mime-type "$1")" = 'text/plain' ] && [ -e "$(head -1 "$1")" ]; then + LIST="$1" +elif ! [ -s "$LIST" ]; then + sel=$(fzf) # Show only the file and parent dir # sel=$(fzf --delimiter / --with-nth=-2,-1 --tiebreak=begin --info=hidden) -else - exit 1 + + LIST='' +fi + +if [ -n "$LIST" ]; then + if type fd >/dev/null 2>&1; then + tmpfile=$(mktemp /tmp/abc-script.XXXXXX) + + while IFS= read -r path; do + if [ -d "$path" ]; then + printf "%s\n" "$path" >> "$tmpfile" + elif [ -f "$path" ]; then + printf "%s\n" "$(dirname "$path")" >> "$tmpfile" + fi + done < "$LIST" + + # Clear selection + if [ -p "$NNN_PIPE" ]; then + printf "-" >"$NNN_PIPE" + fi + + sel=$(xargs -d '\n' -a "$tmpfile" fd -H . | fzf --delimiter / --tiebreak=begin --info=hidden) + + rm "$tmpfile" + else + printf "fd missing" + read -r _ + exit 1 + fi fi if [ -n "$sel" ]; then @@ -20,18 +78,17 @@ if [ -n "$sel" ]; then exit 0 fi - # Check if selected path returned - # by fzf command is absolute + # Check if the selected path returned by fzf command is absolute case $sel in - /*) nnn_cd "$sel" ;; + /*) nnn_cd "$sel" "$CTX" ;; *) # Remove "./" prefix if it exists sel="${sel#./}" if [ "$PWD" = "/" ]; then - nnn_cd "/$sel" + nnn_cd "/$sel" "$CTX" else - nnn_cd "$PWD/$sel" + nnn_cd "$PWD/$sel" "$CTX" fi;; esac fi diff --git a/plugins/fzdirs b/plugins/fzdirs deleted file mode 100755 index 1acecf03..00000000 --- a/plugins/fzdirs +++ /dev/null @@ -1,80 +0,0 @@ -#!/usr/bin/env sh - -# Description: Fuzzy search multiple locations read-in from a path-list -# file and open the selected file's directory in a smart context. -# Dependencies: fzf, fd -# -# Details: Paths in list file should be newline-separated absolute paths. -# Paths can be file paths; the script will scan the parent dirs. -# -# The path-list file precedence is: -# - "$1" (the hovered file) if it exists, is plain-text and the -# first line points to an existing file -# - "$LIST" if set below -# - "$2" (the current directory) [mimics plugin fzcd behaviour] -# -# The path-list file can be generated easily: -# - pick the (file)paths in picker mode to path-list file -# - OR, edit selection in nnn and save as path-list file -# -# The plugin clears nnn selection as the user can be tempted to delete -# duplicate files after finding copies and remove selection by mistake. -# -# Shell: POSIX compliant -# Author: Arun Prakash Jana - -IFS="$(printf '\n\r')" - -. "$(dirname "$0")"/.nnn-plugin-helper - -CTX=+ -LIST="$LIST" - -if [ -n "$1" ] && [ "$(file -b --mime-type "$1")" = 'text/plain' ] && [ -e "$(head -1 "$1")" ]; then - LIST="$1" -elif ! [ -s "$LIST" ]; then - LIST="$2" -fi - -if [ "$(cmd_exists fzf)" -eq "0" ]; then - tmpfile=$(mktemp /tmp/abc-script.XXXXXX) - - while IFS= read -r path; do - if [ -d "$path" ]; then - printf "%s\n" "$path" >> "$tmpfile" - elif [ -f "$path" ]; then - printf "%s\n" "$(dirname "$path")" >> "$tmpfile" - fi - done < "$LIST" - - # Clear selection - if [ -p "$NNN_PIPE" ]; then - printf "-" >"$NNN_PIPE" - fi - - sel=$(xargs -d '\n' -a "$tmpfile" fd -H . | fzf --delimiter / --tiebreak=begin --info=hidden) - - rm "$tmpfile" -else - exit 1 -fi - -if [ -n "$sel" ]; then - if [ "$sel" = "." ] || { ! [ -d "$sel" ] && ! [ -f "$sel" ]; }; then - exit 0 - fi - - # Check if the selected path returned by fzf command is absolute - case $sel in - /*) nnn_cd "$sel" "$CTX" ;; - *) - # Remove "./" prefix if it exists - sel="${sel#./}" - - if [ "$PWD" = "/" ]; then - nnn_cd "/$sel" "$CTX" - else - nnn_cd "$PWD/$sel" "$CTX" - fi;; - esac -fi