2019-10-23 10:04:12 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
2021-05-28 19:03:50 +00:00
|
|
|
# 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.
|
2021-06-20 20:09:45 +00:00
|
|
|
# Dependencies: fzf, find (only for multi-location search)
|
2021-05-28 19:03:50 +00:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
2019-10-23 10:04:12 +00:00
|
|
|
# Shell: POSIX compliant
|
2021-06-20 20:09:45 +00:00
|
|
|
# Author: Anna Arad, Arun Prakash Jana, KlzXS
|
2021-05-28 19:03:50 +00:00
|
|
|
|
|
|
|
IFS="$(printf '\n\r')"
|
2019-10-23 10:04:12 +00:00
|
|
|
|
2022-07-16 17:47:32 +00:00
|
|
|
# shellcheck disable=SC1090,SC1091
|
2019-11-21 20:44:25 +00:00
|
|
|
. "$(dirname "$0")"/.nnn-plugin-helper
|
2019-10-23 10:04:12 +00:00
|
|
|
|
2021-05-28 19:03:50 +00:00
|
|
|
CTX=+
|
2022-07-16 17:47:32 +00:00
|
|
|
LIST="${LIST:-""}"
|
2021-05-28 19:03:50 +00:00
|
|
|
|
|
|
|
if ! type fzf >/dev/null 2>&1; then
|
|
|
|
printf "fzf missing"
|
|
|
|
read -r _
|
2021-06-06 13:07:49 +00:00
|
|
|
exit 1
|
2021-05-28 19:03:50 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$1" ] && [ "$(file -b --mime-type "$1")" = 'text/plain' ] && [ -e "$(head -1 "$1")" ]; then
|
|
|
|
LIST="$1"
|
|
|
|
elif ! [ -s "$LIST" ]; then
|
|
|
|
sel=$(fzf)
|
2021-05-28 13:34:14 +00:00
|
|
|
# Show only the file and parent dir
|
2021-04-17 13:55:07 +00:00
|
|
|
# sel=$(fzf --delimiter / --with-nth=-2,-1 --tiebreak=begin --info=hidden)
|
2021-05-28 19:03:50 +00:00
|
|
|
|
|
|
|
LIST=''
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$LIST" ]; then
|
2021-06-20 20:09:45 +00:00
|
|
|
if type find >/dev/null 2>&1; then
|
2021-05-28 19:03:50 +00:00
|
|
|
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"
|
|
|
|
|
2022-02-01 12:00:20 +00:00
|
|
|
sel=$(xargs -d '\n' < "$tmpfile" -I{} find {} -type f -printf "%H//%P\n" | sed '/.*\/\/\(\..*\|.*\/\..*\)/d; s:/\+:/:g' | fzf --delimiter / --tiebreak=begin --info=hidden)
|
2021-06-20 20:09:45 +00:00
|
|
|
# Alternative for 'fd'
|
2022-02-01 12:00:20 +00:00
|
|
|
# sel=$(xargs -d '\n' < "$tmpfile" fd . | fzf --delimiter / --tiebreak=begin --info=hidden)
|
2021-05-28 19:03:50 +00:00
|
|
|
|
2024-02-13 13:32:42 +00:00
|
|
|
rm -- "$tmpfile"
|
2021-05-28 19:03:50 +00:00
|
|
|
else
|
2021-06-20 20:09:45 +00:00
|
|
|
printf "find missing"
|
2021-05-28 19:03:50 +00:00
|
|
|
read -r _
|
2022-02-01 12:00:20 +00:00
|
|
|
exit 1
|
2021-05-28 19:03:50 +00:00
|
|
|
fi
|
2019-10-23 10:04:12 +00:00
|
|
|
fi
|
|
|
|
|
2019-11-26 12:36:31 +00:00
|
|
|
if [ -n "$sel" ]; then
|
2021-03-14 07:29:24 +00:00
|
|
|
if [ "$sel" = "." ] || { ! [ -d "$sel" ] && ! [ -f "$sel" ]; }; then
|
2019-12-09 17:57:10 +00:00
|
|
|
exit 0
|
2021-03-14 06:49:40 +00:00
|
|
|
fi
|
2019-11-20 13:17:26 +00:00
|
|
|
|
2021-05-28 19:03:50 +00:00
|
|
|
# Check if the selected path returned by fzf command is absolute
|
2020-07-21 07:57:56 +00:00
|
|
|
case $sel in
|
2021-05-28 19:03:50 +00:00
|
|
|
/*) nnn_cd "$sel" "$CTX" ;;
|
2020-11-22 15:24:49 +00:00
|
|
|
*)
|
2020-07-21 07:57:56 +00:00
|
|
|
# Remove "./" prefix if it exists
|
|
|
|
sel="${sel#./}"
|
2020-11-22 15:24:49 +00:00
|
|
|
|
2020-07-21 07:57:56 +00:00
|
|
|
if [ "$PWD" = "/" ]; then
|
2021-05-28 19:03:50 +00:00
|
|
|
nnn_cd "/$sel" "$CTX"
|
2020-07-21 07:57:56 +00:00
|
|
|
else
|
2021-05-28 19:03:50 +00:00
|
|
|
nnn_cd "$PWD/$sel" "$CTX"
|
2020-07-21 07:57:56 +00:00
|
|
|
fi;;
|
2020-11-22 15:24:49 +00:00
|
|
|
esac
|
2019-10-23 10:04:12 +00:00
|
|
|
fi
|