File: v.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2020-2025 pacman64
   6 #
   7 # Permission is hereby granted, free of charge, to any person obtaining a copy
   8 # of this software and associated documentation files (the “Software”), to deal
   9 # in the Software without restriction, including without limitation the rights
  10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 # copies of the Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice shall be included in
  15 # all copies or substantial portions of the Software.
  16 #
  17 # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23 # SOFTWARE.
  24 
  25 
  26 # v [option...] [files...]
  27 #
  28 # View is a simple non-interactive file viewer, which handles plain-text files
  29 # and pictures of all commonly-used formats.
  30 #
  31 # The help option is `-h`, `--h`, `-help`, or `--help`. The only other option
  32 # is `-n` or `--n`, which adds line numbers for the overall results.
  33 
  34 
  35 less_options='-MKiCRS'
  36 awk_code='{ printf "%6d  %s\n", NR, $0 }'
  37 # awk_code='1'
  38 
  39 case "$1" in
  40     -h|--h|-help|--help)
  41         awk '/^# +v /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  42         exit 0
  43     ;;
  44     -n|--n|-N|--N)
  45         less_options='-MKNiCRS'
  46         # awk_code='{ printf "%6d  %s\n", NR, $0 }'
  47         shift
  48     ;;
  49 esac
  50 
  51 [ "$1" = "--" ] && shift
  52 
  53 if [ $# -eq 0 ]; then
  54     less "${less_options}" -
  55     exit $?
  56 fi
  57 
  58 if [ $# -lt 2 ]; then
  59     less_options="${less_options} --header=1"
  60 fi
  61 
  62 bat=''
  63 if [ -e '/usr/bin/batcat' ]; then
  64     bat='/usr/bin/batcat'
  65 elif [ -e '/usr/bin/bat' ]; then
  66     bat='/usr/bin/bat'
  67 fi
  68 
  69 got_chafa=0
  70 if [ -e '/usr/bin/chafa' ]; then
  71     got_chafa=1
  72 fi
  73 
  74 gap=0
  75 
  76 for arg in "$@"; do
  77     [ "${gap}" -gt 0 ] && printf "\n"
  78     gap=1
  79 
  80     # printf "\e[48;2;218;218;218m%-80s\e[0m\n" "${arg}"
  81     printf "\e[7m%-80s\e[0m\n" "${arg}"
  82 
  83     if [ "${arg}" = '-' ]; then
  84         kind='-: text/plain'
  85     else
  86         kind="$(file --mime-type "${arg}")"
  87     fi
  88 
  89     if echo "${kind}" | grep -E -q ' image/[a-z0-9\.-]+$'; then
  90         if [ "${got_chafa}" -eq 1 ]; then
  91             chafa "${arg}"
  92         else
  93             printf "%s\n" "${kind}"
  94         fi
  95         continue
  96     fi
  97 
  98     if echo "${kind}" | grep -q ' inode/directory$'; then
  99         printf "%s is a folder\n" "${arg}"
 100         continue
 101     fi
 102 
 103     if echo "${kind}" | grep -E -q -v ' text/[a-z0-9\.\+-]+$'; then
 104         if echo "${kind}" | grep -E -q -v ' application/j(avascript|son)$'; then
 105             printf "%s\n" "${kind}"
 106             continue
 107         fi
 108     fi
 109 
 110     if [ -z "${bat}" ]; then
 111         awk "${awk_code}" "${arg}"
 112         continue
 113     fi
 114 
 115     "${bat}" \
 116         --style=plain,header,numbers \
 117         --theme='Monokai Extended Light' \
 118         --wrap=never --color=always --paging=never "${arg}" |
 119             # make colors readable even on light backgrounds
 120             sed -e 's-\x1b\[38;5;70m-\x1b[38;5;28m-g' \
 121                 -e 's-\x1b\[38;5;214m-\x1b[38;5;208m-g' \
 122                 -e 's-\x1b\[38;5;243m-\x1b[38;5;103m-g' \
 123                 -e 's-\x1b\[38;5;238m-\x1b[38;5;245m-g' \
 124                 -e 's-\x1b\[38;5;228m-\x1b[48;5;228m-g'
 125 done 2>&1 | less "${less_options}"