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