File: fzopen

package info (click to toggle)
nnn 5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,184 kB
  • sloc: ansic: 11,902; sh: 3,585; makefile: 512; cpp: 80; python: 31; csh: 2
file content (85 lines) | stat: -rwxr-xr-x 2,263 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env sh

# 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.
#
# Dependencies: fd/find, fzf/skim, xdg-open/open (on macOS)
#
# Shell: POSIX compliant
# Author: Arun Prakash Jana

NUKE="${XDG_CONFIG_HOME:-$HOME/.config}/nnn/plugins/nuke"
USE_NUKE=0

# shellcheck disable=SC1090,SC1091
. "$(dirname "$0")"/.nnn-plugin-helper

if type fzf >/dev/null 2>&1; then
    cmd="$FZF_DEFAULT_COMMAND"
    if type fd >/dev/null 2>&1; then
        [ -z "$cmd" ] && cmd="fd -t f 2>/dev/null"
    else
        [ -z "$cmd" ] && cmd="find . -type f 2>/dev/null"
    fi
    entry="$(eval "$cmd" | fzf -m)"
    # To show only the file name
    # entry=$(find . -type f 2>/dev/null | fzf --delimiter / --with-nth=-1 --tiebreak=begin --info=hidden)
elif type sk >/dev/null 2>&1; then
	entry=$(find . -type f 2>/dev/null | sk)
else
    exit 1
fi

# Check for picker mode
if [ "$3" ]; then
    if [ "$entry" ]; then
        case "$entry" in
            /*) fullpath="$entry" ;;
            *)  fullpath="$PWD/$entry" ;;
        esac
        if [ "-" = "$3" ]; then
            printf "%s\n" "$fullpath"
        else
            printf "%s\n" "$fullpath" > "$3"
        fi

        # Tell `nnn` to clear its internal selection
        printf "%s" "0p" > "$NNN_PIPE"
    fi

    exit 0
fi

if [ "$entry" ]; then
    if [ "$USE_NUKE" -ne 0 ]; then
        "$NUKE" "$entry"
        exit 0
    fi

    # Open the file (works for a single file only)
    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
        *text*)
            "${VISUAL:-$EDITOR}" "$entry" ;;
        *)
            $cmd_open "$entry" >/dev/null 2>&1 ;;
    esac
fi