Add nvim as a diff tool for getplugs plugin (#527)

* Add nvim as a diff tool for getplugs plugin

* Comment out `is_cmd_exists()`

* Add nvim diff tool for diffs plugin

* Add $EDITOR to nuke plugin for editing/reading text file format

* Modify diff command to fix CircleCI failures

* Fix getplugs prompt

* Allow nuke to use custom $PAGER
This commit is contained in:
Krisan Alifari 2020-04-20 23:49:35 +07:00 committed by GitHub
parent 4b5ecbe8fc
commit d549ae7486
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 35 deletions

View file

@ -12,12 +12,18 @@
selection=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection selection=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection
if which nvim >/dev/null 2>&1; then
diffcmd="nvim -d"
else
diffcmd="vimdiff +0"
fi
dirdiff() { dirdiff() {
dir1=$(mktemp "${TMPDIR:-/tmp}"/nnn-"$(basename "$1")".XXXXXXXX) dir1=$(mktemp "${TMPDIR:-/tmp}"/nnn-"$(basename "$1")".XXXXXXXX)
dir2=$(mktemp "${TMPDIR:-/tmp}"/nnn-"$(basename "$2")".XXXXXXXX) dir2=$(mktemp "${TMPDIR:-/tmp}"/nnn-"$(basename "$2")".XXXXXXXX)
ls -A1 "$1" > "$dir1" ls -A1 "$1" > "$dir1"
ls -A1 "$2" > "$dir2" ls -A1 "$2" > "$dir2"
vimdiff "$dir1" "$dir2" $diffcmd "$dir1" "$dir2"
rm "$dir1" "$dir2" rm "$dir1" "$dir2"
} }
@ -33,14 +39,14 @@ if [ -s "$selection" ]; then
# Vim: Warning: Input is not from a terminal # Vim: Warning: Input is not from a terminal
# xargs -0 -o vimdiff < $selection # xargs -0 -o vimdiff < $selection
xargs -0 vimdiff +0 < "$selection" eval xargs -0 "$diffcmd" < "$selection"
fi fi
elif ! [ -z "$1" ]; then elif ! [ -z "$1" ]; then
f1="$(echo "$arr" | sed -n '1p')" f1="$(echo "$arr" | sed -n '1p')"
if [ -d "$f1" ] && [ -d "$1" ]; then if [ -d "$f1" ] && [ -d "$1" ]; then
dirdiff "$f1" "$1" dirdiff "$f1" "$1"
elif [ -f "$f1" ] && [ -f "$1" ]; then elif [ -f "$f1" ] && [ -f "$1" ]; then
vimdiff +0 "$f1" "$1" $diffcmd "$f1" "$1"
else else
echo "cannot compare file with directory" echo "cannot compare file with directory"
fi fi

View file

@ -8,17 +8,21 @@
CONFIG_DIR=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/ CONFIG_DIR=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/
PLUGIN_DIR=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/plugins PLUGIN_DIR=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/plugins
is_cmd_exists () { # is_cmd_exists () {
which "$1" > /dev/null 2>&1 # which "$1" > /dev/null 2>&1
echo $? # echo $?
} # }
merge () { merge () {
vimdiff +0 "$1" "$2" if which nvim >/dev/null 2>&1; then
nvim -d "$1" "$2"
else
vimdiff +0 "$1" "$2"
fi
} }
prompt () { prompt () {
printf "%s" "Plugin $1 already exists and is different.\n" printf "%s\n" "Plugin $1 already exists and is different."
printf "Keep (k), merge (m), overwrite (o) [default: k]? " printf "Keep (k), merge (m), overwrite (o) [default: k]? "
read -r operation read -r operation

View file

@ -76,6 +76,8 @@ IMAGE_CACHE_PATH="$(dirname "$1")"/.thumbs
FPATH="$1" FPATH="$1"
FNAME=$(basename "$1") FNAME=$(basename "$1")
EDITOR="${EDITOR:-vi}"
PAGER="${PAGER:-less -R}"
ext="${FNAME##*.}" ext="${FNAME##*.}"
if ! [ -z "$ext" ]; then if ! [ -z "$ext" ]; then
ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')" ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
@ -87,13 +89,13 @@ handle_pdf() {
exit 0 exit 0
elif which pdftotext >/dev/null 2>&1; then elif which pdftotext >/dev/null 2>&1; then
## Preview as text conversion ## Preview as text conversion
pdftotext -l 10 -nopgbrk -q -- "${FPATH}" - | less -R pdftotext -l 10 -nopgbrk -q -- "${FPATH}" - | eval "$PAGER"
exit 0 exit 0
elif which mutool >/dev/null 2>&1; then elif which mutool >/dev/null 2>&1; then
mutool draw -F txt -i -- "${FPATH}" 1-10 mutool draw -F txt -i -- "${FPATH}" 1-10
exit 0 exit 0
elif which exiftool >/dev/null 2>&1; then elif which exiftool >/dev/null 2>&1; then
exiftool "${FPATH}" | less -R exiftool "${FPATH}" | eval "$PAGER"
exit 0 exit 0
fi fi
} }
@ -106,10 +108,10 @@ handle_audio() {
mpv "${FPATH}" >/dev/null 2>&1 & mpv "${FPATH}" >/dev/null 2>&1 &
exit 0 exit 0
elif which mediainfo >/dev/null 2>&1; then elif which mediainfo >/dev/null 2>&1; then
mediainfo "${FPATH}" | less -R mediainfo "${FPATH}" | eval "$PAGER"
exit 0 exit 0
elif which exiftool >/dev/null 2>&1; then elif which exiftool >/dev/null 2>&1; then
exiftool "${FPATH}"| less -R exiftool "${FPATH}"| eval "$PAGER"
exit 0 exit 0
fi fi
} }
@ -125,13 +127,13 @@ handle_video() {
# Thumbnail # Thumbnail
[ -d "${IMAGE_CACHE_PATH}" ] || mkdir "${IMAGE_CACHE_PATH}" [ -d "${IMAGE_CACHE_PATH}" ] || mkdir "${IMAGE_CACHE_PATH}"
ffmpegthumbnailer -i "${FPATH}" -o "${IMAGE_CACHE_PATH}/${FNAME}.jpg" -s 0 ffmpegthumbnailer -i "${FPATH}" -o "${IMAGE_CACHE_PATH}/${FNAME}.jpg" -s 0
viu -n "${IMAGE_CACHE_PATH}/${FNAME}.jpg" | less -R viu -n "${IMAGE_CACHE_PATH}/${FNAME}.jpg" | eval "$PAGER"
exit 0 exit 0
elif which mediainfo >/dev/null 2>&1; then elif which mediainfo >/dev/null 2>&1; then
mediainfo "${FPATH}" | less -R mediainfo "${FPATH}" | eval "$PAGER"
exit 0 exit 0
elif which exiftool >/dev/null 2>&1; then elif which exiftool >/dev/null 2>&1; then
exiftool "${FPATH}"| less -R exiftool "${FPATH}"| eval "$PAGER"
exit 0 exit 0
fi fi
} }
@ -143,23 +145,23 @@ handle_extension() {
a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\ a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\
rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zip) rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zip)
if which atool >/dev/null 2>&1; then if which atool >/dev/null 2>&1; then
atool --list -- "${FPATH}" | less -R atool --list -- "${FPATH}" | eval "$PAGER"
exit 0 exit 0
elif which bsdtar >/dev/null 2>&1; then elif which bsdtar >/dev/null 2>&1; then
bsdtar --list --file "${FPATH}" | less -R bsdtar --list --file "${FPATH}" | eval "$PAGER"
exit 0 exit 0
fi fi
exit 1;; exit 1;;
rar) rar)
if which unrar >/dev/null 2>&1; then if which unrar >/dev/null 2>&1; then
## Avoid password prompt by providing empty password ## Avoid password prompt by providing empty password
unrar lt -p- -- "${FPATH}" | less -R unrar lt -p- -- "${FPATH}" | eval "$PAGER"
fi fi
exit 1;; exit 1;;
7z) 7z)
if which 7z >/dev/null 2>&1; then if which 7z >/dev/null 2>&1; then
## Avoid password prompt by providing empty password ## Avoid password prompt by providing empty password
7z l -p -- "${FPATH}" | less -R 7z l -p -- "${FPATH}" | eval "$PAGER"
exit 0 exit 0
fi fi
exit 1;; exit 1;;
@ -181,7 +183,7 @@ handle_extension() {
## Log files ## Log files
log) log)
vi "${FPATH}" "$EDITOR" "${FPATH}"
exit 0;; exit 0;;
## BitTorrent ## BitTorrent
@ -199,7 +201,7 @@ handle_extension() {
odt|ods|odp|sxw) odt|ods|odp|sxw)
if which odt2txt >/dev/null 2>&1; then if which odt2txt >/dev/null 2>&1; then
## Preview as text conversion ## Preview as text conversion
odt2txt "${FPATH}" | less -R odt2txt "${FPATH}" | eval "$PAGER"
exit 0 exit 0
fi fi
exit 1;; exit 1;;
@ -207,10 +209,10 @@ handle_extension() {
## Markdown ## Markdown
md) md)
if which glow >/dev/null 2>&1; then if which glow >/dev/null 2>&1; then
glow -sdark "${FPATH}" | less -R glow -sdark "${FPATH}" | eval "$PAGER"
exit 0 exit 0
elif which lowdown >/dev/null 2>&1; then elif which lowdown >/dev/null 2>&1; then
lowdown -Tterm "${FPATH}" | less -R lowdown -Tterm "${FPATH}" | eval "$PAGER"
exit 0 exit 0
fi fi
;; ;;
@ -219,13 +221,13 @@ handle_extension() {
htm|html|xhtml) htm|html|xhtml)
## Preview as text conversion ## Preview as text conversion
if which w3m >/dev/null 2>&1; then if which w3m >/dev/null 2>&1; then
w3m -dump "${FPATH}" | less -R w3m -dump "${FPATH}" | eval "$PAGER"
exit 0 exit 0
elif which lynx >/dev/null 2>&1; then elif which lynx >/dev/null 2>&1; then
lynx -dump -- "${FPATH}" | less -R lynx -dump -- "${FPATH}" | eval "$PAGER"
exit 0 exit 0
elif which elinks >/dev/null 2>&1; then elif which elinks >/dev/null 2>&1; then
elinks -dump "${FPATH}" | less -R elinks -dump "${FPATH}" | eval "$PAGER"
exit 0 exit 0
fi fi
;; ;;
@ -233,10 +235,10 @@ handle_extension() {
## JSON ## JSON
json) json)
if which jq >/dev/null 2>&1; then if which jq >/dev/null 2>&1; then
jq --color-output . "${FPATH}" | less -R jq --color-output . "${FPATH}" | eval "$PAGER"
exit 0 exit 0
elif which python >/dev/null 2>&1; then elif which python >/dev/null 2>&1; then
python -m json.tool -- "${FPATH}" | less -R python -m json.tool -- "${FPATH}" | eval "$PAGER"
exit 0 exit 0
fi fi
;; ;;
@ -292,13 +294,13 @@ handle_multimedia() {
sxiv_load_dir "${FPATH}" >/dev/null 2>&1 & sxiv_load_dir "${FPATH}" >/dev/null 2>&1 &
exit 0 exit 0
elif which viu >/dev/null 2>&1; then elif which viu >/dev/null 2>&1; then
viu -n "${FPATH}" | less -R viu -n "${FPATH}" | eval "$PAGER"
exit 0 exit 0
elif which img2txt >/dev/null 2>&1; then elif which img2txt >/dev/null 2>&1; then
img2txt --gamma=0.6 -- "${FPATH}" | less -R img2txt --gamma=0.6 -- "${FPATH}" | eval "$PAGER"
exit 0 exit 0
elif which exiftool >/dev/null 2>&1; then elif which exiftool >/dev/null 2>&1; then
exiftool "${FPATH}" | less -R exiftool "${FPATH}" | eval "$PAGER"
exit 0 exit 0
fi fi
# local orientation # local orientation
@ -418,7 +420,7 @@ handle_mime() {
## Text ## Text
text/* | */xml) text/* | */xml)
vi "${FPATH}" "$EDITOR" "${FPATH}"
exit 0;; exit 0;;
## Syntax highlight ## Syntax highlight
# if [[ "$( stat --printf='%s' -- "${FPATH}" )" -gt "${HIGHLIGHT_SIZE_MAX}" ]]; then # if [[ "$( stat --printf='%s' -- "${FPATH}" )" -gt "${HIGHLIGHT_SIZE_MAX}" ]]; then
@ -442,10 +444,10 @@ handle_mime() {
image/vnd.djvu) image/vnd.djvu)
if which djvutxt >/dev/null 2>&1; then if which djvutxt >/dev/null 2>&1; then
## Preview as text conversion (requires djvulibre) ## Preview as text conversion (requires djvulibre)
djvutxt "${FPATH}" | less -R djvutxt "${FPATH}" | eval "$PAGER"
exit 0 exit 0
elif which exiftool >/dev/null 2>&1; then elif which exiftool >/dev/null 2>&1; then
exiftool "${FPATH}" | less -R exiftool "${FPATH}" | eval "$PAGER"
exit 0 exit 0
fi fi
exit 1;; exit 1;;