From 7ccd7e4757ea73b0e2cba4e6a6972f8f6035752f Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Tue, 3 Oct 2023 12:59:07 +0200 Subject: [PATCH] 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. --- sh/.config/sh/env | 2 + sh/.local/bin/dark_bg | 72 +++++++++++++++++++ .../.config/vifm/scripts/vifm-default-viewer | 2 +- terminal/.config/vifm/vifmrc | 8 ++- 4 files changed, 82 insertions(+), 2 deletions(-) create mode 100755 sh/.local/bin/dark_bg diff --git a/sh/.config/sh/env b/sh/.config/sh/env index c835c24..f333088 100644 --- a/sh/.config/sh/env +++ b/sh/.config/sh/env @@ -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 :" diff --git a/sh/.local/bin/dark_bg b/sh/.local/bin/dark_bg new file mode 100755 index 0000000..380451a --- /dev/null +++ b/sh/.local/bin/dark_bg @@ -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 diff --git a/terminal/.config/vifm/scripts/vifm-default-viewer b/terminal/.config/vifm/scripts/vifm-default-viewer index b4d0497..0ad47c6 100755 --- a/terminal/.config/vifm/scripts/vifm-default-viewer +++ b/terminal/.config/vifm/scripts/vifm-default-viewer @@ -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 diff --git a/terminal/.config/vifm/vifmrc b/terminal/.config/vifm/vifmrc index 688480a..cf813a8 100644 --- a/terminal/.config/vifm/vifmrc +++ b/terminal/.config/vifm/vifmrc @@ -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