fzopen (2123B)
1 #!/usr/bin/env sh 2 3 # Description: Regular mode: 4 # Fuzzy find a file in directory subtree. 5 # Opens in $VISUAL or $EDITOR if text. 6 # Opens other type of files with xdg-open. 7 # Work only with a single file selected. 8 # 9 # Picker mode: 10 # If picker mode output file is passed, it 11 # will be overwritten with any picked files. 12 # Leaves untouched if no file is picked. 13 # Works with single/multiple files selected. 14 # 15 # Dependencies: fd/find, fzf/skim, xdg-open/open (on macOS) 16 # 17 # Shell: POSIX compliant 18 # Author: Arun Prakash Jana 19 20 NUKE="${XDG_CONFIG_HOME:-$HOME/.config}/nnn/plugins/nuke" 21 USE_NUKE=0 22 23 . "$(dirname "$0")"/.nnn-plugin-helper 24 25 if type fzf >/dev/null 2>&1; then 26 cmd="$FZF_DEFAULT_COMMAND" 27 if type fd >/dev/null 2>&1; then 28 [ -z "$cmd" ] && cmd="fd -t f 2>/dev/null" 29 else 30 [ -z "$cmd" ] && cmd="find . -type f 2>/dev/null" 31 fi 32 entry="$(eval "$cmd" | fzf -m)" 33 # To show only the file name 34 # entry=$(find . -type f 2>/dev/null | fzf --delimiter / --with-nth=-1 --tiebreak=begin --info=hidden) 35 elif type sk >/dev/null 2>&1; then 36 entry=$(find . -type f 2>/dev/null | sk) 37 else 38 exit 1 39 fi 40 41 # Check for picker mode 42 if [ "$3" ]; then 43 if [ "$entry" ]; then 44 case "$entry" in 45 /*) fullpath="$entry" ;; 46 *) fullpath="$PWD/$entry" ;; 47 esac 48 if [ "-" = "$3" ]; then 49 printf "%s\n" "$fullpath" 50 else 51 printf "%s\n" "$fullpath" > "$3" 52 fi 53 54 # Tell `nnn` to clear its internal selection 55 printf "%s" "0p" > "$NNN_PIPE" 56 fi 57 58 exit 0 59 fi 60 61 if [ "$USE_NUKE" -ne 0 ]; then 62 "$NUKE" "$entry" 63 exit 0 64 fi 65 66 # Open the file (works for a single file only) 67 cmd_file="" 68 cmd_open="" 69 if uname | grep -q "Darwin"; then 70 cmd_file="file -bIL" 71 cmd_open="open" 72 else 73 cmd_file="file -biL" 74 cmd_open="xdg-open" 75 fi 76 77 case "$($cmd_file "$entry")" in 78 *text*) 79 "${VISUAL:-$EDITOR}" "$entry" ;; 80 *) 81 $cmd_open "$entry" >/dev/null 2>&1 ;; 82 esac