wofr06 / lesspipe

lesspipe - display more with less
GNU General Public License v2.0
485 stars 51 forks source link

pygmentize is called when listing tgz file. #71

Closed iceblock-coder closed 2 years ago

iceblock-coder commented 2 years ago

Hello,

Trying to less some log files which are fastened in a tgz. Although tarcolor is tested positive, pygmentize is still kicked-in to guess colorizing the output. Which causes the output weird.

** Just tar:

user@ubun2004:temp% tar tvzf 111.tgz
drwxrwxrwx user/user 0 2021-12-23 16:11 111/222/
-rwxrwxrwx user/user 0 2021-12-23 16:11 111/222/1234
-rwxrwxrwx user/user 0 2021-12-23 16:11 111/222/2345
-rwxrwxrwx user/user 0 2021-12-23 16:11 111/222/3456
drwxrwxrwx user/user 0 2021-12-23 16:11 111/333/
-rwxrwxrwx user/user 0 2021-12-23 16:11 111/333/1111
-rwxrwxrwx user/user 0 2021-12-23 16:11 111/333/2222
-rwxrwxrwx user/user 0 2021-12-23 16:11 111/333/3333
user@ubun2004:temp% 

** Less:

drwxrwxrwx user/user 0 2021-12-23 16:11 [38;5;136m[01;34m111/222/[38;5;241m0m
-rwxrwxrwx user/user 0 2021-12-23 16:11 [38;5;241m01;32m111/222/1234[38;5;241m0m
-rwxrwxrwx user/user 0 2021-12-23 16:11 [38;5;241m01;32m111/222/2345[38;5;241m0m
-rwxrwxrwx user/user 0 2021-12-23 16:11 [38;5;241m01;32m111/222/3456[38;5;241m0m
drwxrwxrwx user/user 0 2021-12-23 16:11 [38;5;241m01;34m111/333/[38;5;241m0m
-rwxrwxrwx user/user 0 2021-12-23 16:11 [38;5;241m01;32m111/333/1111[38;5;241m0m
-rwxrwxrwx user/user 0 2021-12-23 16:11 [38;5;241m01;32m111/333/2222[38;5;241m0m
-rwxrwxrwx user/user 0 2021-12-23 16:11 [38;5;241m01;32m111/333/3333[38;5;241m0m

** Full debug log:

user@ubun2004:temp% less 111.tgz
+ set +o noclobber
+ setopt sh_word_split
+ PATH=/home/user/.local/bin:/home/user/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/user/mydoc/Downloads/git/lesspipe
+ has_cmd w3m
+ command -v w3m
+ has_cmd lynx
+ command -v lynx
+ has_cmd elinks
+ command -v elinks
+ has_cmd html2text
+ command -v html2text
+ sep=:
+ altsep==
+ [[ -e 111.tgz ]]
+ [[ 111.tgz == *:* ]]
+ [[ 111.tgz == *=* ]]
+ tmpdir=/tmp/lesspipe.5345
+ [[ -d /tmp/lesspipe.5345 ]]
+ mkdir /tmp/lesspipe.5345
+ trap 'rm -rf '\''/tmp/lesspipe.5345'\''' EXIT
+ trap - PIPE
++ nexttmp
++ declare new=/tmp/lesspipe.5345/lesspipe.4890
++ echo /tmp/lesspipe.5345/lesspipe.4890
+ t=/tmp/lesspipe.5345/lesspipe.4890
+ analyze_args
++ ps -T -opid= -oargs=
+ cmdtree=' 3132 -zsh
 3224 less -FXRi 111.tgz
 3225 sh -c /usr/bin/zsh -c \ /home/user/mydoc/Downloads/git/lesspipe/lesspipe.sh\ 111.tgz
 3226 bash -x /home/user/mydoc/Downloads/git/lesspipe/lesspipe.sh 111.tgz
 3230 ps -T -opid= -oargs='
+ [[  3132 -zsh
 3224 less -FXRi 111.tgz
 3225 sh -c /usr/bin/zsh -c \ /home/user/mydoc/Downloads/git/lesspipe/lesspipe.sh\ 111.tgz
 3226 bash -x /home/user/mydoc/Downloads/git/lesspipe/lesspipe.sh 111.tgz
 3230 ps -T -opid= -oargs= =~ ./test.pl .*|./test.pl ]]
++ echo 3132 -zsh 3224 less -FXRi 111.tgz 3225 sh -c /usr/bin/zsh -c '\' '/home/user/mydoc/Downloads/git/lesspipe/lesspipe.sh\' 111.tgz 3226 bash -x /home/user/mydoc/Downloads/git/lesspipe/lesspipe.sh 111.tgz 3230 ps -T -opid= -oargs=
++ sed 's/[0-9]* //;s/ [0-9]* /;/g;s/^[^;]*;//'
+ cmdtree='less -FXRi 111.tgz;sh -c /usr/bin/zsh -c \ /home/user/mydoc/Downloads/git/lesspipe/lesspipe.sh\ 111.tgz;bash -x /home/user/mydoc/Downloads/git/lesspipe/lesspipe.sh 111.tgz;ps -T -opid= -oargs='
+ lessarg='less -FXRi 111.tgz'
+ [[ less -FXRi 111.tgz == *test.pl\ * ]]
+ [[ less -FXRi 111.tgz == *less\ *\ +F\ * ]]
+ [[ less -FXRi 111.tgz == *less\ *\ : ]]
+ [[ | /home/user/mydoc/Downloads/git/lesspipe/lesspipe.sh %s == *-* ]]
+ lessarg=' less -FXRi 111.tgz'
++ echo less -FXRi 111.tgz
++ sed 's/-[a-zA-Z]*[rR]/-r/'
+ lessarg='less -ri 111.tgz'
+ has_cmd tput
+ command -v tput
++ tput colors
+ [[ 256 -ge 8 ]]
+ [[ less -ri 111.tgz == *-[rR]* ]]
+ COLOR=--color=always
+ [[ less -ri 111.tgz == *\ [:=]* ]]
+ [[ | /home/user/mydoc/Downloads/git/lesspipe/lesspipe.sh %s == *\|-* ]]
+ [[ -d /tmp/lesspipe.5345 ]]
+ IFS=:
+ a=111.tgz
+ IFS=' '
+ [[ 111.tgz == '' ]]
+ show 111.tgz
+ file1=111.tgz
+ rest1=
+ [[ '' == ::* ]]
+ [[ ! -e 111.tgz ]]
+ rest11=
+ file2=
+ rest2=
+ [[ '' == ::* ]]
+ rest2=
+ rest11=
+ [[ '' == '' ]]
++ filetype 111.tgz
++ fname=111.tgz
++ [[ 111.tgz == - ]]
+++ fileext 111.tgz
+++ case "$1" in
+++ extension=tgz
+++ echo tgz
++ fext=tgz
+++ file -L -s -b --mime 111.tgz
++ declare 'ft=application/gzip; charset=binary'
++ fchar=binary
++ fcat=application
++ ft='gzip; charset=binary'
++ ft=gzip
++ ft=gzip
++ ftype=gzip
++ case "$ftype" in
++ case "$fext" in
++ [[ application == application ]]
++ [[ gzip == octet-stream ]]
++ [[ application == text ]]
++ echo gzip:binary:application
+ ft=gzip:binary:application
+ get_unpack_cmd gzip:binary:application 111.tgz ''
+ fchar=gzip:binary
+ fchar=binary
+ fcat=application
+ x=gzip
+ cmd=
+ [[ '' == :: ]]
+ declare t
+ case $x in
+ [[ 111.tgz == - ]]
+ fileext=111.tgz
+ fileext=111.tgz
+ fileext=111.tgz
+ has_cmd gzip
+ command -v gzip
+ cmd=($x -cd "$2")
+ return
+ [[ gzip != '' ]]
+ show -
+ file1=-
+ rest1=
+ [[ '' == ::* ]]
+ [[ ! -e - ]]
+ [[ - != \- ]]
+ rest11=
+ file2=
+ rest2=
+ [[ '' == ::* ]]
+ rest2=
+ rest11=
+ [[ gzip == '' ]]
+ [[ '' == '' ]]
+ c1=("${cmd[@]}")
++ gzip -cd 111.tgz
++ filetype -
++ fname=-
++ [[ - == - ]]
+++ nexttmp
+++ declare new=/tmp/lesspipe.5345/lesspipe.26674
+++ echo /tmp/lesspipe.5345/lesspipe.26674
++ declare t=/tmp/lesspipe.5345/lesspipe.26674
++ head -c 40000
++ set /tmp/lesspipe.5345/lesspipe.26674 ''
++ fname=111.tgz
+++ fileext 111.tgz
+++ case "$1" in
+++ extension=tgz
+++ echo tgz
++ fext=tgz
+++ file -L -s -b --mime /tmp/lesspipe.5345/lesspipe.26674
++ declare 'ft=application/x-tar; charset=binary'
++ fchar=binary
++ fcat=application
++ ft='x-tar; charset=binary'
++ ft=x-tar
++ ft=tar
++ ftype=tar
++ case "$ftype" in
++ case "$fext" in
++ [[ application == application ]]
++ [[ tar == octet-stream ]]
++ [[ application == text ]]
++ echo tar:binary:application
+ ft=tar:binary:application
+ get_unpack_cmd tar:binary:application - ''
+ fchar=tar:binary
+ fchar=binary
+ fcat=application
+ x=tar
+ cmd=
+ [[ '' == :: ]]
+ declare t
+ case $x in
+ [[ binary == utf-16le ]]
+ [[ '' == : ]]
+ file2=
+ file2=
+ [[ -n '' ]]
+ rest1=
+ rest2=
+ case "$x" in
+ has_cmd bsdtar
+ command -v bsdtar
+ cmd=(istar tar "$2" "$file2")
+ [[ -n istar ]]
+ [[ -z '' ]]
+ msg 'use tar_file:contained_file to view a file in the archive'
+ [[ -n '' ]]
+ echo '==> use tar_file:contained_file to view a file in the archive'
+ [[ -n istar ]]
+ [[ -n '' ]]
+ [[ istar != '' ]]
+ show -
+ file1=-
+ rest1=
+ [[ '' == ::* ]]
==> use tar_file:contained_file to view a file in the archive
+ [[ ! -e - ]]
+ [[ - != \- ]]
+ rest11=
+ file2=
+ rest2=
+ [[ '' == ::* ]]
+ rest2=
+ rest11=
+ [[ istar == '' ]]
+ [[ gzip == '' ]]
+ [[ '' == '' ]]
+ c2=("${cmd[@]}")
++ gzip -cd 111.tgz
++ istar tar - ''
++ prog=tar
++ [[ - =~ ^[a-z_-]*:.* ]]
++ [[ -n '' ]]
++ [[ --color=always == *always ]]
++ has_cmd tarcolor
++ command -v tarcolor
++ filetype -
++ fname=-
++ [[ - == - ]]
++ tar tvf -
++ tarcolor
+++ nexttmp
+++ declare new=/tmp/lesspipe.5345/lesspipe.19395
+++ echo /tmp/lesspipe.5345/lesspipe.19395
++ declare t=/tmp/lesspipe.5345/lesspipe.19395
++ head -c 40000
++ set /tmp/lesspipe.5345/lesspipe.19395 ''
++ fname=111.tgz
+++ fileext 111.tgz
+++ case "$1" in
+++ extension=tgz
+++ echo tgz
++ fext=tgz
+++ file -L -s -b --mime /tmp/lesspipe.5345/lesspipe.19395
++ declare 'ft=text/plain; charset=us-ascii'
++ fchar=us-ascii
++ fcat=text
++ ft='plain; charset=us-ascii'
++ ft=plain
++ ft=plain
++ ftype=plain
++ case "$ftype" in
++ case "$fext" in
++ [[ text == application ]]
++ [[ text == text ]]
++ [[ plain == plain ]]
+++ file -L -s -b /tmp/lesspipe.5345/lesspipe.19395
++ ft='ASCII text, with escape sequences'
++ case $ft in
++ case "$fext" in
++ [[ us-ascii != binary ]]
++ case "$fext" in
++ echo plain:us-ascii:text
+ ft=plain:us-ascii:text
+ get_unpack_cmd plain:us-ascii:text - ''
+ fchar=plain:us-ascii
+ fchar=us-ascii
+ fcat=text
+ x=plain
+ cmd=
+ [[ '' == :: ]]
+ declare t
+ case $x in
+ [[ us-ascii == utf-16le ]]
+ [[ '' == : ]]
+ file2=
+ file2=
+ [[ -n '' ]]
+ rest1=
+ rest2=
+ case "$x" in
+ [[ -n '' ]]
+ [[ -n '' ]]
+ [[ '' != '' ]]
+ gzip -cd 111.tgz
+ istar tar - ''
+ prog=tar
+ [[ - =~ ^[a-z_-]*:.* ]]
+ [[ -n '' ]]
+ [[ --color=always == *always ]]
+ has_cmd tarcolor
+ command -v tarcolor
+ isfinal plain:us-ascii:text - ''
+ [[ '' == *: ]]
+ [[ -z '' ]]
+ [[ -n '' ]]
+ case "$x" in
+ [[ -z '' ]]
+ tar tvf -
+ tarcolor
++ fileext -
++ case "$1" in
++ echo ''
+ fext=
+ [[ text == audio ]]
+ [[ text == video ]]
+ [[ text == image ]]
+ [[ us-ascii == binary ]]
+ [[ -z '' ]]
+ [[ -n '' ]]
+ [[ -n '' ]]
+ [[ -n '' ]]
+ [[ -z '' ]]
+ [[ text == text ]]
+ [[ plain != plain ]]
+ [[ -z '' ]]
++ fileext 111.tgz
++ case "$1" in
++ extension=tgz
++ echo tgz
+ fext=tgz
++ has_colorizer - tgz
++ arg=-
++ [[ - = - ]]
++ arg=
++ [[ --color=always == *always ]]
++ [[ tgz == plain ]]
++ [[ -z tgz ]]
++ prog=
++ for i in bat batcat pygmentize source-highlight code2color vimcolor
++ [[ -z '' ]]
++ has_cmd bat
++ command -v bat
++ for i in bat batcat pygmentize source-highlight code2color vimcolor
++ [[ -z '' ]]
++ has_cmd batcat
++ command -v batcat
++ for i in bat batcat pygmentize source-highlight code2color vimcolor
++ [[ -z '' ]]
++ has_cmd pygmentize
++ command -v pygmentize
++ prog=pygmentize
++ for i in bat batcat pygmentize source-highlight code2color vimcolor
++ [[ -z pygmentize ]]
++ [[ pygmentize == source-highlight ]]
++ for i in bat batcat pygmentize source-highlight code2color vimcolor
++ [[ -z pygmentize ]]
++ [[ pygmentize == code2color ]]
++ for i in bat batcat pygmentize source-highlight code2color vimcolor
++ [[ -z pygmentize ]]
++ [[ pygmentize == vimcolor ]]
++ [[ tgz =~ ^[0-9]*$ ]]
++ opt=' -l tgz'
++ case $prog in
++ [[ -n '' ]]
+++ pygmentize -l tgz /dev/null
++ res=
++ opt=' -g'
++ echo 'pygmentize -g '
+ colorizer='pygmentize -g '
+ [[ -n pygmentize -g  ]]
+ [[ text != binary ]]
+ pygmentize -g
drwxrwxrwx user/user 0 2021-12-23 16:11 [38;5;136m[01;34m111/222/[38;5;241m0m
-rwxrwxrwx user/user 0 2021-12-23 16:11 [38;5;241m01;32m111/222/1234[38;5;241m0m
-rwxrwxrwx user/user 0 2021-12-23 16:11 [38;5;241m01;32m111/222/2345[38;5;241m0m
-rwxrwxrwx user/user 0 2021-12-23 16:11 [38;5;241m01;32m111/222/3456[38;5;241m0m
drwxrwxrwx user/user 0 2021-12-23 16:11 [38;5;241m01;34m111/333/[38;5;241m0m
-rwxrwxrwx user/user 0 2021-12-23 16:11 [38;5;241m01;32m111/333/1111[38;5;241m0m
-rwxrwxrwx user/user 0 2021-12-23 16:11 [38;5;241m01;32m111/333/2222[38;5;241m0m
-rwxrwxrwx user/user 0 2021-12-23 16:11 [38;5;241m01;32m111/333/3333[38;5;241m0m
+ return
+ rm -rf /tmp/lesspipe.5345
iceblock-coder commented 2 years ago

Updated: In previous workaround, when a non .tgz file(e.g a .gz compessed log file) comes, pygmentize guesses again. So I decide to escape guess at all.

diff --git a/lesspipe.sh b/lesspipe.sh
index 4569e5c..88142eb 100755
--- a/lesspipe.sh
+++ b/lesspipe.sh
@@ -390,7 +390,7 @@ has_colorizer () {
       opt="$opt $COLOR" ;;
     pygmentize)
                [[ -n $LESSCOLORIZER && $LESSCOLORIZER =~ pygmentize\ \ *-O\ *style=[a-z]* ]] && prog=$LESSCOLORIZER
-               res=$(pygmentize -l $2 /dev/null 2>/dev/null) && opt=" -l $2" || opt=" -g" ;;
+               res=$(pygmentize -l $2 /dev/null 2>/dev/null) && opt=" -l $2" || return ;;
     source-highlight)
       prog="source-highlight --failsafe -f esc"
       [[ -z $arg ]] && arg=/dev/stdin

Original: My current approach is to add an escape for tar.

diff --git a/lesspipe.sh b/lesspipe.sh
index 105ccd5..0d59914 100755
--- a/lesspipe.sh
+++ b/lesspipe.sh
@@ -215,7 +215,7 @@ show () {
     if [[ "$cmd" != "" ]]; then
       show "-$rest1"
     else
-      isfinal "$ft" "$file1" "$rest11"
+      isfinal "$ft" "$file1" "$rest11" ''
     fi
   elif [[ "$c1" == "" ]]; then
     c1=("${cmd[@]}")
@@ -224,7 +224,7 @@ show () {
     if [[ "$cmd" != "" ]]; then
       show "-$rest1"
     else
-      "${c1[@]}" | isfinal "$ft" - "$rest11"
+      "${c1[@]}" | isfinal "$ft" - "$rest11" "${c1[0]}"
     fi
   elif [[ "$c2" == "" ]]; then
     c2=("${cmd[@]}")
@@ -233,7 +233,7 @@ show () {
     if [[ "$cmd" != "" ]]; then
       show "-$rest1"
     else
-       "${c1[@]}" | "${c2[@]}" | isfinal "$ft" - "$rest11"
+       "${c1[@]}" | "${c2[@]}" | isfinal "$ft" - "$rest11" "${c2[0]}"
     fi
   elif [[ "$c3" == "" ]]; then
     c3=("${cmd[@]}")
@@ -242,7 +242,7 @@ show () {
     if [[ "$cmd" != "" ]]; then
       show "-$rest1"
     else
-      "${c1[@]}" | "${c2[@]}" | "${c3[@]}" | isfinal "$ft" - "$rest11"
+      "${c1[@]}" | "${c2[@]}" | "${c3[@]}" | isfinal "$ft" - "$rest11" "${c3[0]}"
     fi
   elif [[ "$c4" == "" ]]; then
     c4=("${cmd[@]}")
@@ -251,7 +251,7 @@ show () {
     if [[ "$cmd" != "" ]]; then
       show "-$rest1"
     else
-      "${c1[@]}" | "${c2[@]}" | "${c3[@]}" | "${c4[@]}" | isfinal "$ft" - "$rest11"
+      "${c1[@]}" | "${c2[@]}" | "${c3[@]}" | "${c4[@]}" | isfinal "$ft" - "$rest11" "${c4[0]}"
     fi
   elif [[ "$c5" == "" ]]; then
     c5=("${cmd[@]}")
@@ -260,7 +260,7 @@ show () {
     if [[ "$cmd" != "" ]]; then
       echo "$0: Too many levels of encapsulation"
     else
-      "${c1[@]}" | "${c2[@]}" | "${c3[@]}" | "${c4[@]}" | "${c5[@]}" | isfinal "$ft" - "$rest11"
+      "${c1[@]}" | "${c2[@]}" | "${c3[@]}" | "${c4[@]}" | "${c5[@]}" | isfinal "$ft" - "$rest11" "${c5[0]}"
     fi
   fi
 }
@@ -371,9 +371,21 @@ has_colorizer () {
   arg="$1"
   [[ "$arg" = - ]] && arg=
   [[ $COLOR == *always ]] || return
-  [[ $2 == plain || -z $2 ]] && return
+  if [[ $2 == plain || -z $2 ]] && [[ $3 != istar ]]
+    then
+      return
+  fi
   prog=${LESSCOLORIZER%% *}

+  if [[ $3 == istar ]] ; then
+    if has_cmd tarcolor; then
+      echo tarcolor
+    else
+      echo cat
+    fi
+    return
+  fi
+  
   for i in bat batcat pygmentize source-highlight code2color vimcolor ; do
     [[ -z $prog || $prog == $i ]] && has_cmd $i && prog=$i
   done
@@ -535,7 +547,7 @@ isfinal () {
     [[ -n "$file2" ]] && fext="$file2"
     [[ -z "$fext" && $fcat == text && $x != plain ]] && fext=$x
     [[ -z "$fext" ]] && fext=$(fileext "$fileext")
-    colorizer=$(has_colorizer "$2" "$fext")
+    colorizer=$(has_colorizer "$2" "$fext" "$4")
     if [[ -n $colorizer && $fcat != binary ]]; then
       $colorizer && return
     fi
@@ -549,8 +561,8 @@ istar () {
   [[ "$2" =~ ^[a-z_-]*:.* ]] && echo $2: remote operation tar host:file not allowed && return
   if [[ -n $3 ]]; then
     $prog Oxf "$2" "$3" 2>&1
-  elif [[ $COLOR == *always ]] && has_cmd tarcolor; then
-    $prog tvf "$2" | tarcolor
+#  elif [[ $COLOR == *always ]] && has_cmd tarcolor; then
+#    $prog tvf "$2" | tarcolor
   else
     $prog tvf "$2"
   fi
wofr06 commented 2 years ago

Archive listings are now colored using archive_color (slightly modified tarcolor). They are no longer handed to another colorizer.