vifm: Fancy markdown display with glow or bat

For anything markdown or that gets displayed as markdown
(e.g. docx files) in the vifm preview, we now use glow
or bat to display a nicely styled and colored version.

For glow I added a script which tries to detect the
current terminal background between dark/light to
correctly set the color scheme.
This commit is contained in:
Marty Oehme 2023-10-03 12:59:07 +02:00
parent 893c177358
commit 7ccd7e4757
Signed by: Marty
GPG Key ID: EDBF2ED917B2EF6A
4 changed files with 82 additions and 2 deletions

View File

@ -50,6 +50,8 @@ export LANG="en_US.UTF-8"
export SHELL="${SHELL:-/bin/bash}"
export TERM=xterm-256color
# set env var to current shell background luminosity (used by vifm)
. dark_bg
if exist fzf; then
export FZF_DEFAULT_OPTS="--bind 'tab:toggle+down,shift-tab:toggle+up,ctrl-g:top,ctrl-t:toggle-preview,ctrl-d:preview-half-page-down,ctrl-u:preview-half-page-up' --color=light -1 -m --delimiter :"

72
sh/.local/bin/dark_bg Executable file
View File

@ -0,0 +1,72 @@
#!/usr/bin/env sh
#
# Determine if term background is dark or light.
# Uses a couple different heuristics, most of them
# from this:
# https://unix.stackexchange.com/questions/245378/common-environment-variable-to-set-dark-or-light-terminal-background
# unix exchange.
#
# If the background is dark, returns exit code 0 (true).
# If the background is light, returns exit code 1 (false).
# TODO implement alternative $COLORBFG method
is_dark() {
IFS=/ read -r bg_r bg_g bg_b << EOF
${1:-"0000/0000/0000"}
EOF
IFS=/ read -r fg_r fg_g fg_b << EOF
${2:-"ffff/ffff/ffff"}
EOF
calc=$(echo "($bg_r+$bg_g+$bg_b)-($fg_r+$fg_g+$fg_b)" | tr '[:lower:]' '[:upper:]')
luminance=$(echo "ibase=16; $calc" | bc)
if [ "$luminance" -lt 0 ]; then
true
else
false
fi
}
orig_ssty=$(stty -g)
stty raw -echo min 0 time 0
printf '\e]11;?\a'
sleep 0.01
read -r answer
bg="${answer#*;}"
printf '\e]10;?\a'
sleep 0.01
read -r answer
fg="${answer#*;}"
stty "$orig_ssty"
bg=$(echo "${bg}" | sed 's/.*\(rgb:[0-9a-f/]*\).*/\1/')
fg=$(echo "${fg}" | sed 's/.*\(rgb:[0-9a-f/]*\).*/\1/')
# from the amazing
# https://stackoverflow.com/questions/2683279/how-to-detect-if-a-script-is-being-sourced
# answer
sourced=0
if [ -n "$ZSH_VERSION" ]; then
case $ZSH_EVAL_CONTEXT in *:file) sourced=1;; esac
elif [ -n "$KSH_VERSION" ]; then
# shellcheck disable=SC2296
[ "$(cd -- "$(dirname -- "$0")" && pwd -P)/$(basename -- "$0")" != "$(cd -- "$(dirname -- "${.sh.file}")" && pwd -P)/$(basename -- "${.sh.file}")" ] && sourced=1
elif [ -n "$BASH_VERSION" ]; then
(return 0 2>/dev/null) && sourced=1
else
# Detects `sh` and `dash`; add additional shell filenames as needed.
case ${0##*/} in sh|-sh|dash|-dash) sourced=1;; esac
fi
is_dark "${bg#rgb:}" "${fg#rgb:}"
outp="$?"
if [ "$sourced" -eq 1 ]; then
if [ "$outp" -eq 0 ]; then
export TERM_DARK=true
else
export TERM_DARK=false
fi
else
exit "$outp"
fi

View File

@ -21,5 +21,5 @@ charset=${info#*=}
if [ "x$charset" == "xbinary" ]; then
hexdump -e '"%08_ax: "' -e '8/1 "%02x " " " 8/1 "%02x "' -e '" |" 16/1 "%_p"' -e '"\n"' -v -n $nbytes "$1"
else
head -$nlines "$1" | enconv -g -L $language -x $encoding
head -$nlines "$1" | enconv -g -L $language -x $encoding | bat --color=always --style=plain
fi

View File

@ -552,7 +552,8 @@ filetype *.docx
\ pandoc -s -t markdown %f | nvim,
\ docx2txt %f - | nvim
fileviewer *.docx
\ pandoc -s -t markdown %f,
\ [ "$TERM_DARK" = "true" ] && pandoc -s -t markdown %f | glow --style=dark - || pandoc -s -t markdown %f | glow --style=light -,
\ pandoc -s -t markdown %f | bat --color=always --style=plain
\ docx2txt %f -
" TuDu files
@ -569,6 +570,11 @@ fileviewer */
\ exa --color always --tree -L2,
\ tree -L 2,
" markdown text
fileviewer *.md
\ [ "$TERM_DARK" = "true" ] && glow --style=dark %c || glow --style=light %c,
\ bat --color=always --style=plain
" use custom viewer script for rest
fileviewer * vifm-default-viewer %c