From 885cfd47345017ca8524c259e53949312360fc8f Mon Sep 17 00:00:00 2001 From: Arun Prakash Jana Date: Mon, 9 Dec 2019 18:36:48 +0530 Subject: [PATCH] Support both fzf and fzy --- plugins/README.md | 8 ++++---- plugins/fzcd | 2 +- plugins/fzhist | 12 ++++++++++-- plugins/fzopen | 12 +++++++++++- plugins/launch | 4 ++-- plugins/pskill | 35 ++++++++++++++++++++--------------- 6 files changed, 48 insertions(+), 25 deletions(-) diff --git a/plugins/README.md b/plugins/README.md index 1906df7e..4bf724b1 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -20,9 +20,9 @@ Plugins extend the capabilities of `nnn`. They are _executable_ scripts (or bina | diffs | Diff for selection (limited to 2 for directories) | sh | vimdiff | | dragdrop | Drag/drop files from/into nnn | sh | [dragon](https://github.com/mwh/dragon) | | exetoggle | Toggle executable status of hovered file | sh | chmod | -| fzcd | Change to the directory of a fuzzy-selected file/dir | sh | fzf/fzy
(optional fd) | -| fzhist | Fuzzy-select a cmd from history, edit in `$EDITOR` and run | sh | fzy | -| fzopen | Fuzzy find a file in dir subtree and edit or open | sh | fzy, xdg-open | +| fzcd | Change to the directory of a fuzzy-selected file/dir | sh | fzf/fzy
fd/fdfind/find | +| fzhist | Fuzzy-select a cmd from history, edit in `$EDITOR` and run | sh | fzf/fzy | +| fzopen | Fuzzy find a file in dir subtree and edit or open | sh | fzf/fzy, xdg-open | | getplugs | Update plugins | sh | curl | | gutenread | Browse, download, read from Project Gutenberg | sh | curl, unzip, w3m
[epr](https://github.com/wustho/epr) (optional) | | hexview | View a file in hex in `$PAGER` | sh | xxd | @@ -46,7 +46,7 @@ Plugins extend the capabilities of `nnn`. They are _executable_ scripts (or bina | pdfread | Read a PDF or text file aloud | sh | pdftotext, mpv,
pico2wave | | pdfview | View PDF file in `$PAGER` | sh | pdftotext/
mupdf-tools | | picker | Pick files and list one per line (to pipe) | sh | nnn | -| pskill | Fuzzy list by name and kill process or zombie | sh | fzy, sudo/doas | +| pskill | Fuzzy list by name and kill process or zombie | sh | fzf/fzy, ps,
sudo/doas | | renamer | Batch rename selection or files in dir | sh | [qmv](https://www.nongnu.org/renameutils/)/[vidir](https://joeyh.name/code/moreutils/) | | ringtone | Create a variable bitrate mp3 ringtone from file | sh | date, ffmpeg | | splitjoin | Split file or join selection | sh | split, cat | diff --git a/plugins/fzcd b/plugins/fzcd index d10b119f..88f53976 100755 --- a/plugins/fzcd +++ b/plugins/fzcd @@ -1,6 +1,6 @@ #!/usr/bin/env sh -# Description: Run fzf/fzy/fd/fdfind/find and go to the directory of the file selected +# Description: Run fzf/fzy, fd/fdfind/find and go to the directory of the file selected # # Shell: POSIX compliant # Author: Anna Arad diff --git a/plugins/fzhist b/plugins/fzhist index c47efa25..e5618ae9 100755 --- a/plugins/fzhist +++ b/plugins/fzhist @@ -6,14 +6,22 @@ # Shell: POSIX compliant # Author: Arun Prakash Jana +if which fzf >/dev/null 2>&1; then + fuzzy=fzf +elif which fzy >/dev/null 2>&1; then + fuzzy=fzy +else + exit 1 +fi + shellname="$(basename "$SHELL")" if [ "$shellname" = "bash" ]; then hist_file="$HOME/.bash_history" - entry="$(fzy < "$hist_file")" + entry="$("$fuzzy" < "$hist_file")" elif [ "$shellname" = "fish" ]; then hist_file="$HOME/.config/fish/fish_history" - entry="$(grep "\- cmd: " "$hist_file" | cut -c 8- | fzy)" + entry="$(grep "\- cmd: " "$hist_file" | cut -c 8- | "$fuzzy")" fi if ! [ -z "$entry" ]; then diff --git a/plugins/fzopen b/plugins/fzopen index 046e7e91..91a504d5 100755 --- a/plugins/fzopen +++ b/plugins/fzopen @@ -4,10 +4,20 @@ # Opens in $VISUAL or $EDITOR if text # Opens other type of files with xdg-open # +# Requires: fzf/fzy, xdg-open +# # Shell: POSIX compliant # Author: Arun Prakash Jana -entry="$(find . -type f 2>/dev/null | fzy)" +if which fzf >/dev/null 2>&1; then + fuzzy=fzf +elif which fzy >/dev/null 2>&1; then + fuzzy=fzy +else + exit 1 +fi + +entry="$(find . -type f 2>/dev/null | "$fuzzy")" case "$(file -biL "$entry")" in *text*) diff --git a/plugins/launch b/plugins/launch index 018aca30..0444e91b 100755 --- a/plugins/launch +++ b/plugins/launch @@ -9,7 +9,7 @@ # # xfce4-terminal -e "${XDG_CONFIG_HOME:-$HOME/.config}/nnn/plugins/launch # -# Requires: fzf or fzy +# Requires: fzf/fzy # # Usage: launch [delay] # delay is in seconds, if omitted launch waits for 1 sec @@ -25,7 +25,7 @@ IFS=':' get_selection() { if which fzf >/dev/null 2>&1; then - { IFS=':'; ls -H $PATH; } | sort | fzy + { IFS=':'; ls -H $PATH; } | sort | fzf elif which fzy >/dev/null 2>&1; then { IFS=':'; ls -H $PATH; } | sort | fzy else diff --git a/plugins/pskill b/plugins/pskill index d02c9fa4..fdb5396d 100755 --- a/plugins/pskill +++ b/plugins/pskill @@ -2,29 +2,34 @@ # Description: Fuzzy list and kill a (zombie) process by name # +# Requires: fzf or fzy, ps +# # Note: To kill a zombie process enter "zombie" # # Shell: POSIX compliant # Author: Arun Prakash Jana -is_cmd_exists () { - which "$1" > /dev/null 2>&1 - echo $? -} - -if [ "$(is_cmd_exists sudo)" -eq "0" ]; then - sucmd=sudo -elif [ "$(is_cmd_exists doas)" -eq "0" ]; then - sucmd=doas -else - sucmd=: # noop -fi - printf "Enter process name ['defunct' for zombies]: " read -r psname +# shellcheck disable=SC2009 if ! [ -z "$psname" ]; then - # shellcheck disable=SC2009 - cmd="$(ps -ax | grep -iw "$psname" | fzy | sed -e 's/^[ \t]*//' | cut -d' ' -f1)" + if which sudo >/dev/null 2>&1; then + sucmd=sudo + elif which doas >/dev/null 2>&1; then + sucmd=doas + else + sucmd=: # noop + fi + + if which fzf >/dev/null 2>&1; then + fuzzy=fzf + elif which fzy >/dev/null 2>&1; then + fuzzy=fzy + else + exit 1 + fi + + cmd="$(ps -ax | grep -iw "$psname" | "$fuzzy" | sed -e 's/^[ \t]*//' | cut -d' ' -f1)" $sucmd kill -9 "$cmd" fi