File: v.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 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 #
  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 if [ $# -lt 2 ]; then
  98     less_options="${less_options} --header=1"
  99 fi
 100 
 101 # show all non-existing files given
 102 failed=0
 103 for arg in "$@"; do
 104     if [ "${arg}" = "-" ]; then
 105         continue
 106     fi
 107     if [ ! -e "${arg}" ]; then
 108         printf "no file named \"%s\"\n" "${arg}" > /dev/stderr
 109         failed=1
 110     fi
 111 done
 112 
 113 if [ "${failed}" -gt 0 ]; then
 114     exit 2
 115 fi
 116 
 117 bat=''
 118 if [ -e /usr/bin/batcat ]; then
 119     bat='/usr/bin/batcat'
 120 elif [ -e /usr/bin/bat ]; then
 121     bat='/usr/bin/bat'
 122 fi
 123 
 124 view_pic=''
 125 if [ -e /usr/bin/chafa ]; then
 126     view_pic='chafa'
 127     if [ "${sixels}" -eq 1 ]; then
 128         view_pic='chafa --format sixels'
 129     fi
 130 fi
 131 
 132 gap=0
 133 
 134 for arg in "$@"; do
 135     [ "${gap}" -gt 0 ] && printf "\n"
 136     gap=1
 137 
 138     printf "\e[7m%-80s\e[0m\n" "${arg}"
 139 
 140     if [ "${arg}" = '-' ]; then
 141         kind='-: text/plain'
 142     else
 143         kind="$(file --mime-type "${arg}")"
 144     fi
 145 
 146     if echo "${kind}" | grep -E -q ' image/[a-z0-9\.-]+$'; then
 147         if [ -z "${view_pic}" ]; then
 148             printf "%s\n" "${kind}"
 149         else
 150             ${view_pic} "${arg}"
 151         fi
 152         continue
 153     fi
 154 
 155     if echo "${kind}" | grep -q ' inode/directory$'; then
 156         printf "%s is a folder\n" "${arg}"
 157         continue
 158     fi
 159 
 160     if echo "${kind}" | grep -E -q -v ' text/[a-z0-9\.\+-]+$'; then
 161         if echo "${kind}" | grep -E -q -v ' application/j(avascript|son)$'; then
 162             printf "%s\n" "${kind}"
 163             continue
 164         fi
 165     fi
 166 
 167     if [ -z "${bat}" ]; then
 168         awk '{ printf "%6d  %s\n", NR, $0 }' "${arg}"
 169         continue
 170     fi
 171 
 172     "${bat}" \
 173         --style=plain,header,numbers \
 174         --theme='Monokai Extended Light' \
 175         --wrap=never --color=always --paging=never "${arg}" |
 176     # make colors readable even on light backgrounds
 177     sed -e 's-\x1b\[38;5;70m-\x1b[38;5;28m-g' \
 178         -e 's-\x1b\[38;5;214m-\x1b[38;5;208m-g' \
 179         -e 's-\x1b\[38;5;243m-\x1b[38;5;103m-g' \
 180         -e 's-\x1b\[38;5;238m-\x1b[38;5;245m-g' \
 181         -e 's-\x1b\[38;5;228m-\x1b[48;5;228m-g'
 182 done 2>&1 | less ${less_options}