"Fossies" - the Fresh Open Source Software Archive

Member "nnn-4.2/plugins/fzopen" (21 Jul 2021, 2000 Bytes) of package /linux/misc/nnn-v4.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "fzopen": v4.1.1_vs_v4.2.

    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         if [ "-" = "$3" ]; then
   45             printf "%s\n" "$entry"
   46         else
   47             printf "%s\n" "$entry" > "$3"
   48         fi
   49 
   50         # Tell `nnn` to clear its internal selection
   51         printf "%s" "0p" > "$NNN_PIPE"
   52     fi
   53 
   54     exit 0
   55 fi
   56 
   57 if [ "$USE_NUKE" -ne 0 ]; then
   58     "$NUKE" "$entry"
   59     exit 0
   60 fi
   61 
   62 # Open the file (works for a single file only)
   63 cmd_file=""
   64 cmd_open=""
   65 if uname | grep -q "Darwin"; then
   66     cmd_file="file -bIL"
   67     cmd_open="open"
   68 else
   69     cmd_file="file -biL"
   70     cmd_open="xdg-open"
   71 fi
   72 
   73 case "$($cmd_file "$entry")" in
   74     *text*)
   75         "${VISUAL:-$EDITOR}" "$entry" ;;
   76     *)
   77         $cmd_open "$entry" >/dev/null 2>&1 ;;
   78 esac