From 581171bdc61665109a87503b56e3657cc05474b6 Mon Sep 17 00:00:00 2001 From: Suvayu Ali Date: Thu, 19 Sep 2024 04:31:28 +0530 Subject: [PATCH 1/4] lesspipe.sh: check style & theme are non-empty before adding flags Without the check, when there are no --style/--theme flags (e.g. when they are in the user config file), the `bat` command line was constructed something like this: bat -l sh --style= --theme= --color=always --paging=never This led to the following error and failure to colorize: error: invalid value '' for '--style ': Unknown style, '' For more information, try '--help'. Skip adding those flags when they are empty (not detected). --- lesspipe.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lesspipe.sh b/lesspipe.sh index 23e2c9e..cadba1d 100755 --- a/lesspipe.sh +++ b/lesspipe.sh @@ -453,7 +453,8 @@ has_colorizer () { [[ -z $style ]] && style=plain [[ -z $theme ]] && theme=ansi fi - opt+=(--style="${style%% *}" --theme="${theme%%[|&;<>]*}") + [[ -n $style ]] && opt+=(--style="${style%% *}") + [[ -n $theme ]] && opt+=(--theme="${theme%%[|&;<>]*}") opt+=("$COLOR" --paging=never "$1") ;; pygmentize) pygmentize -l "$lang" /dev/null &>/dev/null && opt=(-l "$lang") || opt=(-g) From e408411e8ddea597d4966599f8f1f08d74259296 Mon Sep 17 00:00:00 2001 From: Suvayu Ali Date: Thu, 19 Sep 2024 04:36:29 +0530 Subject: [PATCH 2/4] lesspipe.sh: ask `bat` for config file location Instead of hard coded path in `$HOME/.config`, ask `bat` for the correct path. This is more robust since `bat` it is platform independent. --- lesspipe.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lesspipe.sh b/lesspipe.sh index cadba1d..d207dc0 100755 --- a/lesspipe.sh +++ b/lesspipe.sh @@ -437,17 +437,18 @@ has_colorizer () { [[ -n $3 ]] && lang=$3 || lang=$2 case $prog in bat|batcat) + batconfig=$(bat --config-file) [[ -n $lang ]] && $prog --list-languages|sed 's/.*:/,/;s/$/,/'|grep -i ",$lang," > /dev/null && opt=(-l "$lang") [[ -n $LESSCOLORIZER && $LESSCOLORIZER = *\ *--style=* ]] && style="${LESSCOLORIZER/* --style=/}" [[ -z $style ]] && style=$BAT_STYLE [[ -n $LESSCOLORIZER && $LESSCOLORIZER = *\ *--theme=* ]] && theme="${LESSCOLORIZER/* --theme=/}" [[ -z $theme ]] && theme=$BAT_THEME - if [[ -r "$HOME/.config/bat/config" ]]; then + if [[ -r "$batconfig" ]]; then if [[ -z $style ]]; then - grep -q -e '^--style' "$HOME/.config/bat/config" || style=plain + grep -q -e '^--style' "$batconfig" || style=plain fi if [[ -z $theme ]]; then - grep -q -e '^--theme' "$HOME/.config/bat/config" || theme=ansi + grep -q -e '^--theme' "$batconfig" || theme=ansi fi else [[ -z $style ]] && style=plain From 332b3d4c4d02a3ac30182805dac3e90cfe9e2dc0 Mon Sep 17 00:00:00 2001 From: Wolfgang Friebel Date: Sun, 22 Sep 2024 10:07:16 +0200 Subject: [PATCH 3/4] Update lesspipe.sh Changed the commit slightly to allow for the name batcat --- lesspipe.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lesspipe.sh b/lesspipe.sh index d207dc0..8c585a2 100755 --- a/lesspipe.sh +++ b/lesspipe.sh @@ -437,7 +437,7 @@ has_colorizer () { [[ -n $3 ]] && lang=$3 || lang=$2 case $prog in bat|batcat) - batconfig=$(bat --config-file) + batconfig=$($prog --config-file) [[ -n $lang ]] && $prog --list-languages|sed 's/.*:/,/;s/$/,/'|grep -i ",$lang," > /dev/null && opt=(-l "$lang") [[ -n $LESSCOLORIZER && $LESSCOLORIZER = *\ *--style=* ]] && style="${LESSCOLORIZER/* --style=/}" [[ -z $style ]] && style=$BAT_STYLE From 19abff7e02526952c99bad9e0f19e7334388b9e0 Mon Sep 17 00:00:00 2001 From: Suvayu Ali Date: Sun, 22 Sep 2024 08:24:14 +0000 Subject: [PATCH 4/4] strip white space from bat opts before checking if empty Co-authored-by: Nobuyoshi Nakada --- lesspipe.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lesspipe.sh b/lesspipe.sh index 8c585a2..a279cff 100755 --- a/lesspipe.sh +++ b/lesspipe.sh @@ -454,8 +454,8 @@ has_colorizer () { [[ -z $style ]] && style=plain [[ -z $theme ]] && theme=ansi fi - [[ -n $style ]] && opt+=(--style="${style%% *}") - [[ -n $theme ]] && opt+=(--theme="${theme%%[|&;<>]*}") + style="${style%% *}" theme="${theme%%[|&;<>]*}" + opt+=(${style:+--style="$style"} ${theme:+--theme="$theme"}) opt+=("$COLOR" --paging=never "$1") ;; pygmentize) pygmentize -l "$lang" /dev/null &>/dev/null && opt=(-l "$lang") || opt=(-g)