File: v.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2026 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 #
  29 # View is a simple non-interactive file viewer, which handles plain-text files
  30 # and pictures of all commonly-used formats.
  31 #
  32 # The options are, available in single and double-dashed versions
  33 #
  34 #   -h           show this help message
  35 #   -help        show this help message
  36 #
  37 #   -header=n    always show first `n` lines at the top
  38 #
  39 #   -n           show line numbers for overall results
  40 #   -N           show line numbers for overall results
  41 #
  42 #   -s           show pictures using the sixel format (experimental)
  43 #   -sixel       show pictures using the sixel format (experimental)
  44 #
  45 #   -t           show first line at the top at all times
  46 #   -top         show first line at the top at all times
  47 
  48 
  49 case "$1" in
  50     -h|--h|-help|--help)
  51         awk '/^# +v /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  52         exit 0
  53     ;;
  54 esac
  55 
  56 sixels=0
  57 # less_options='-MKiCRS'
  58 less_options='-MKiCS'
  59 
  60 for arg in "$@"; do
  61     if [ "${arg}" = '--' ]; then
  62         shift
  63         break
  64     fi
  65 
  66     case "${arg}" in
  67         -n|--n|-N|--N)
  68             less_options="${less_options} -N"
  69             shift
  70         ;;
  71         --header=*)
  72             less_options="${less_options} ${arg}"
  73             shift
  74         ;;
  75         -s|--s|-sixel|--sixel|-sixels|--sixels)
  76             sixels=1
  77             # less_options="${less_options} -r"
  78             shift
  79         ;;
  80         -t|--t|-top|--top)
  81             less_options="${less_options} --header=1"
  82             shift
  83         ;;
  84         *) break;;
  85     esac
  86 done
  87 
  88 if [ "${sixels}" -eq 0 ]; then
  89     less_options="${less_options} -R"
  90 fi
  91 
  92 if [ $# -eq 0 ]; then
  93     less ${less_options} -f -
  94     exit $?
  95 fi
  96 
  97 bat_style='plain,header,numbers'
  98 if [ $# -lt 2 ]; then
  99     bat_style='plain,numbers'
 100     less_options="${less_options} --header=1"
 101 fi
 102 
 103 # show all non-existing files given
 104 failed=0
 105 for arg in "$@"; do
 106     if [ "${arg}" = "-" ]; then
 107         continue
 108     fi
 109     if [ ! -e "${arg}" ]; then
 110         printf "no file named \"%s\"\n" "${arg}" > /dev/stderr
 111         failed=1
 112     fi
 113 done
 114 
 115 if [ "${failed}" -gt 0 ]; then
 116     exit 2
 117 fi
 118 
 119 bat=''
 120 if [ -e /usr/bin/batcat ]; then
 121     bat='/usr/bin/batcat'
 122 elif [ -e /usr/bin/bat ]; then
 123     bat='/usr/bin/bat'
 124 else
 125     printf "file viewer 'bat'/'batcat' not found" > /dev/stderr
 126     exit 1
 127 fi
 128 
 129 view_pic=''
 130 if [ -e /usr/bin/chafa ]; then
 131     view_pic='chafa'
 132     if [ "${sixels}" -eq 1 ]; then
 133         view_pic='chafa --format sixels'
 134     fi
 135 fi
 136 
 137 gap=0
 138 
 139 for arg in "$@"; do
 140     [ "${gap}" -gt 0 ] && printf "\n"
 141     gap=1
 142 
 143     printf "\e[7m%-80s\e[0m\n" "${arg}"
 144 
 145     if [ "${arg}" = '-' ]; then
 146         kind='-: text/plain'
 147     else
 148         kind="$(file --mime-type "${arg}")"
 149     fi
 150 
 151     if echo "${kind}" | grep -E -q ' image/[a-z0-9\.-]+$'; then
 152         if [ -z "${view_pic}" ]; then
 153             printf "%s\n" "${kind}"
 154         else
 155             ${view_pic} "${arg}"
 156         fi
 157         continue
 158     fi
 159 
 160     if echo "${kind}" | grep -q ' inode/directory$'; then
 161         printf "%s is a folder\n" "${arg}"
 162         continue
 163     fi
 164 
 165     if echo "${kind}" | grep -E -q -v ' text/[a-z0-9\.\+-]+$'; then
 166         if echo "${kind}" | grep -E -q -v ' application/j(avascript|son)$'; then
 167             printf "%s\n" "${kind}"
 168             continue
 169         fi
 170     fi
 171 
 172     if [ -z "${bat}" ]; then
 173         awk '{ printf "%6d  %s\n", NR, $0 }' "${arg}"
 174         continue
 175     fi
 176 
 177     "${bat}" \
 178         --style="${bat_style}" \
 179         --theme='Monokai Extended Light' \
 180         --wrap=never --color=always --paging=never "${arg}" |
 181     # make colors readable even on light backgrounds
 182     sed -e 's-\x1b\[38;5;70m-\x1b[38;5;28m-g' \
 183         -e 's-\x1b\[38;5;214m-\x1b[38;5;208m-g' \
 184         -e 's-\x1b\[38;5;243m-\x1b[38;5;103m-g' \
 185         -e 's-\x1b\[38;5;238m-\x1b[38;5;245m-g' \
 186         -e 's-\x1b\[38;5;228m-\x1b[48;5;228m-g'
 187 done 2>&1 | less ${less_options}