wofr06/lesspipe

Use bat's default config

Freed-Wu opened this issue · 1 comments

Done differently by checking the config file

I have a question:

By default,

       --style <style-components>

              Configure which elements (line numbers, file headers, grid borders, Git modifications, ..) to display in addition to the file contents. The argument is a comma-separated list of components to display
              (e.g. 'numbers,changes,grid') or a pre-defined style ('full').  To set a default style, add the '--style=".."' option to the configuration file or export the BAT_STYLE environment variable (e.g.: ex‐
              port BAT_STYLE=".."). Possible values: *default*, full, auto, plain, changes, header, header-filename, header-filesize, grid, rule, numbers, snip.

Why not let lesspipe use bat's default?

diff --git a/lesspipe.sh b/lesspipe.sh
index ac3b7e5..f047d65 100755
--- a/lesspipe.sh
+++ b/lesspipe.sh
@@ -411,7 +411,7 @@ has_colorizer () {
 		bat|batcat)
 			[[ -n $LESSCOLORIZER && $LESSCOLORIZER = *\ *--style=* ]] && style="${LESSCOLORIZER/* --style=/}"
 			[[ -z $style ]] && style=$BAT_STYLE
-			[[ -z $style ]] && style=plain
+			[[ -z $style ]] && style=default
 			# only allow an explicitly requested language
 			[[ -z $3 ]] && opt=() || opt=(-l "$3")
 			grep -q -e '^--style' "$HOME/.config/bat/config" || opt+=(--style="${style%% *}")

the style is default will check $BAT_STYLE and ~/.config/bat/config, so lesspipe don't need to do this work.

And in fact, if don't pass --style=XXX, bat will use --stye=default by default. So if we use bat's default, all code about style can be omitted.

diff --git a/lesspipe.sh b/lesspipe.sh
index ac3b7e5..14661a4 100755
--- a/lesspipe.sh
+++ b/lesspipe.sh
@@ -409,12 +409,8 @@ has_colorizer () {
 	[[ "$2" =~ ^[0-9]*$ || -z "$2" ]] && opt=() || opt=(-l "$2")
 	case $prog in
 		bat|batcat)
-			[[ -n $LESSCOLORIZER && $LESSCOLORIZER = *\ *--style=* ]] && style="${LESSCOLORIZER/* --style=/}"
-			[[ -z $style ]] && style=$BAT_STYLE
-			[[ -z $style ]] && style=plain
 			# only allow an explicitly requested language
 			[[ -z $3 ]] && opt=() || opt=(-l "$3")
-			grep -q -e '^--style' "$HOME/.config/bat/config" || opt+=(--style="${style%% *}")
 			opt+=("$COLOR" --paging=never "$1") ;;
 		pygmentize)
 			pygmentize -l "$2" /dev/null &>/dev/null && opt=(-l "$2") || opt=(-g)

The code lines is reduced, which reduce the probability of bug. I think it should be better?

And [[ "$2" =~ ^[0-9]*$ || -z "$2" ]] && opt=() || opt=(-l "$2") is not used because the latter code is [[ -z $3 ]] && opt=() || opt=(-l "$3")

I advise changing = to +=.

And [[ $2 == plain || -z $2 ]] && return make it cannot use bat to display plain text, but bat can display line number and header:
screen-2022-11-20-22-09-24

the top is less ChangeLog and bottom is bat ChangeLog. I think we should remove [[ $2 == plain || -z $2 ]] && return to let user can use the bottom. Right?

Just my 2c.

New approach: set style=plain and theme=ansi as default in bat. Text is then readable in terminals both with dark and light background and the appearance is more like in less without lesspipe. The theme can only be changed in the config file of bat.