Debian Codegraph @

plugins/fzopen

raw
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 # shellcheck disable=SC1090,SC1091
24 . "$(dirname "$0")"/.nnn-plugin-helper
25
26 if type fzf >/dev/null 2>&1; then
27 cmd="$FZF_DEFAULT_COMMAND"
28 if type fd >/dev/null 2>&1; then
29 [ -z "$cmd" ] && cmd="fd -t f 2>/dev/null"
30 else
31 [ -z "$cmd" ] && cmd="find . -type f 2>/dev/null"
32 fi
33 entry="$(eval "$cmd" | fzf -m)"
34 # To show only the file name
35 # entry=$(find . -type f 2>/dev/null | fzf --delimiter / --with-nth=-1 --tiebreak=begin --info=hidden)
36 elif type sk >/dev/null 2>&1; then
37 entry=$(find . -type f 2>/dev/null | sk)
38 else
39 exit 1
40 fi
41
42 # Check for picker mode
43 if [ "$3" ]; then
44 if [ "$entry" ]; then
45 case "$entry" in
46 /*) fullpath="$entry" ;;
47 *) fullpath="$PWD/$entry" ;;
48 esac
49 if [ "-" = "$3" ]; then
50 printf "%s\n" "$fullpath"
51 else
52 printf "%s\n" "$fullpath" > "$3"
53 fi
54
55 # Tell `nnn` to clear its internal selection
56 printf "%s" "0p" > "$NNN_PIPE"
57 fi
58
59 exit 0
60 fi
61
62 if [ "$entry" ]; then
63 if [ "$USE_NUKE" -ne 0 ]; then
64 "$NUKE" "$entry"
65 exit 0
66 fi
67
68 # Open the file (works for a single file only)
69 cmd_file=""
70 cmd_open=""
71 if uname | grep -q "Darwin"; then
72 cmd_file="file -bIL"
73 cmd_open="open"
74 else
75 cmd_file="file -biL"
76 cmd_open="xdg-open"
77 fi
78
79 case "$($cmd_file "$entry")" in
80 *text*)
81 "${VISUAL:-$EDITOR}" "$entry" ;;
82 *)
83 $cmd_open "$entry" >/dev/null 2>&1 ;;
84 esac
85 fi