]> Sergey Matveev's repositories - nnn.git/commitdiff
Change binary file detection in preview-tui/kitty (#587)
authorlvgx <l@vgx.fr>
Fri, 22 May 2020 02:48:00 +0000 (04:48 +0200)
committerGitHub <noreply@github.com>
Fri, 22 May 2020 02:48:00 +0000 (08:18 +0530)
plugins/preview-kitty
plugins/preview-tui

index 4bff54dc8fccb9b34ca31b9b28aea31b30b84e25..8ebf86fe4dc2aeb4297f7aabd5fd0c25300b7f4a 100755 (executable)
@@ -31,22 +31,23 @@ preview_file () {
     lines=$(($(tput lines)-1))
     cols=$(tput cols)
     mime="$(file -b --mime-type "$1")"
+    encoding="$(file -b --mime-encoding "$1")"
 
     if [ -d "$1" ]; then
         # Print directory tree
         # shellcheck disable=SC2015
         cd "$1" && \
         COLUMNS=$cols exa -G --colour=always 2>/dev/null || ls --color=alway
-    elif [ "${mime%%/*}" = "text" ] ; then
-        # Print file head
-        bat --terminal-width="$cols" --paging=never --decorations=always \
-            --color=always "$1" 2>/dev/null || cat
     elif [ "${mime%%/*}" = "image" ] ; then
         kitty +kitten icat --silent --transfer-mode=stream --stdin=no "$1"
-    else
-        # Binary file
+    elif [ "$encoding" = "binary" ] ; then
+        # Binary file: show file info
         printf -- "-------- \033[1;31mBinary file\033[0m --------\n"
         mediainfo "$1" 2>/dev/null || file -b "$1"
+    else
+        # Text file: print colored file content
+        bat --terminal-width="$cols" --paging=never --decorations=always \
+            --color=always "$1" 2>/dev/null || cat
     fi | head -n $lines
 }
 
index 3ebf19575e4469e81313c15f3c99f49189875147..5e1fd674bfe8b842d1f46c41a020f0c6c96a6224 100755 (executable)
@@ -27,18 +27,18 @@ preview_file () {
     clear
     lines=$(($(tput lines)-1))
     cols=$(tput cols)
-    mime="$(file -b --mime-type "$1")"
+    encoding="$(file -b --mime-encoding "$1")"
 
     if [ -d "$1" ]; then
         # Print directory tree
         cd "$1" && tree | head -n $lines | cut -c 1-"$cols"
-    elif [ "${mime%%/*}" = "text" ] ; then
-        # Print file head
-        head -n $lines "$1" | cut -c 1-"$cols"
-    else
-        # Binary file
+    elif [ "$encoding" = "binary" ] ; then
+        # Binary file: just print filetype info
         echo "-------- Binary file --------"
         file -b "$1"
+    else
+        # Text file: print file head
+        head -n $lines "$1" | cut -c 1-"$cols"
     fi
 }