2019-01-03 23:05:51 +05:30
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
2021-05-17 00:55:29 +05:30
|
|
|
# Description: Regular mode:
|
|
|
|
# Fuzzy find a file in directory subtree.
|
|
|
|
# Opens in $VISUAL or $EDITOR if text.
|
|
|
|
# Opens other type of files with xdg-open.
|
|
|
|
# Work only with a single file selected.
|
|
|
|
#
|
|
|
|
# Picker mode:
|
|
|
|
# If picker mode output file is passed, it
|
|
|
|
# will be overwritten with any picked files.
|
|
|
|
# Leaves untouched if no file is picked.
|
|
|
|
# Works with single/multiple files selected.
|
2019-01-03 23:05:51 +05:30
|
|
|
#
|
2021-06-11 11:46:51 +02:00
|
|
|
# Dependencies: fd/find, fzf/skim, xdg-open/open (on macOS)
|
2019-12-09 18:36:48 +05:30
|
|
|
#
|
2019-03-24 21:11:43 +05:30
|
|
|
# Shell: POSIX compliant
|
2019-01-03 23:05:51 +05:30
|
|
|
# Author: Arun Prakash Jana
|
|
|
|
|
2021-07-17 20:32:01 +05:30
|
|
|
NUKE="${XDG_CONFIG_HOME:-$HOME/.config}/nnn/plugins/nuke"
|
|
|
|
USE_NUKE=0
|
|
|
|
|
2022-07-16 23:17:32 +05:30
|
|
|
# shellcheck disable=SC1090,SC1091
|
2021-05-17 00:55:29 +05:30
|
|
|
. "$(dirname "$0")"/.nnn-plugin-helper
|
|
|
|
|
2021-05-14 17:33:28 +05:30
|
|
|
if type fzf >/dev/null 2>&1; then
|
2020-04-12 20:36:59 +07:00
|
|
|
cmd="$FZF_DEFAULT_COMMAND"
|
2021-05-14 17:33:28 +05:30
|
|
|
if type fd >/dev/null 2>&1; then
|
2020-04-27 00:05:11 +05:30
|
|
|
[ -z "$cmd" ] && cmd="fd -t f 2>/dev/null"
|
|
|
|
else
|
|
|
|
[ -z "$cmd" ] && cmd="find . -type f 2>/dev/null"
|
|
|
|
fi
|
2021-06-11 15:48:29 +02:00
|
|
|
entry="$(eval "$cmd" | fzf -m)"
|
2020-02-20 08:37:49 +05:30
|
|
|
# To show only the file name
|
|
|
|
# entry=$(find . -type f 2>/dev/null | fzf --delimiter / --with-nth=-1 --tiebreak=begin --info=hidden)
|
2021-05-14 17:33:28 +05:30
|
|
|
elif type sk >/dev/null 2>&1; then
|
2020-04-20 19:09:30 +02:00
|
|
|
entry=$(find . -type f 2>/dev/null | sk)
|
2019-12-09 18:36:48 +05:30
|
|
|
else
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-05-17 00:55:29 +05:30
|
|
|
# Check for picker mode
|
|
|
|
if [ "$3" ]; then
|
|
|
|
if [ "$entry" ]; then
|
2021-11-01 14:05:18 +06:00
|
|
|
case "$entry" in
|
2021-11-01 14:15:49 +06:00
|
|
|
/*) fullpath="$entry" ;;
|
|
|
|
*) fullpath="$PWD/$entry" ;;
|
2021-11-01 14:05:18 +06:00
|
|
|
esac
|
2021-05-17 00:55:29 +05:30
|
|
|
if [ "-" = "$3" ]; then
|
2021-11-01 14:05:18 +06:00
|
|
|
printf "%s\n" "$fullpath"
|
2021-05-17 00:55:29 +05:30
|
|
|
else
|
2021-11-01 14:05:18 +06:00
|
|
|
printf "%s\n" "$fullpath" > "$3"
|
2021-05-17 00:55:29 +05:30
|
|
|
fi
|
|
|
|
|
|
|
|
# Tell `nnn` to clear its internal selection
|
|
|
|
printf "%s" "0p" > "$NNN_PIPE"
|
|
|
|
fi
|
|
|
|
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2021-07-17 20:32:01 +05:30
|
|
|
if [ "$USE_NUKE" -ne 0 ]; then
|
|
|
|
"$NUKE" "$entry"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2021-05-17 00:55:29 +05:30
|
|
|
# Open the file (works for a single file only)
|
2021-06-11 11:46:51 +02:00
|
|
|
cmd_file=""
|
|
|
|
cmd_open=""
|
|
|
|
if uname | grep -q "Darwin"; then
|
|
|
|
cmd_file="file -bIL"
|
|
|
|
cmd_open="open"
|
|
|
|
else
|
|
|
|
cmd_file="file -biL"
|
|
|
|
cmd_open="xdg-open"
|
|
|
|
fi
|
|
|
|
|
|
|
|
case "$($cmd_file "$entry")" in
|
2019-08-22 19:58:39 +05:30
|
|
|
*text*)
|
|
|
|
"${VISUAL:-$EDITOR}" "$entry" ;;
|
|
|
|
*)
|
2021-06-11 11:46:51 +02:00
|
|
|
$cmd_open "$entry" >/dev/null 2>&1 ;;
|
2019-08-22 19:58:39 +05:30
|
|
|
esac
|