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 case "$1" in
  36     -h|--h|-help|--help)
  37         awk '/^# +v /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  38         exit 0
  39     ;;
  40 esac
  41 
  42 less_options='-MKiCRS'
  43 
  44 for arg in "$@"; do
  45     case "${arg}" in
  46         -n|--n|-N|--N)
  47             less_options="${less_options} -N"
  48             shift
  49         ;;
  50         --header=*)
  51             less_options="${less_options} ${arg}"
  52             shift
  53         ;;
  54         -t|--t|-top|--top)
  55             less_options="${less_options} --header=1"
  56             shift
  57         ;;
  58         *) break;;
  59     esac
  60 done
  61 
  62 [ "$1" = "--" ] && shift
  63 
  64 if [ $# -eq 0 ]; then
  65     less "${less_options}" -f -
  66     exit $?
  67 fi
  68 
  69 if [ $# -lt 2 ]; then
  70     less_options="${less_options} --header=1"
  71 fi
  72 
  73 # show all non-existing files given
  74 failed=0
  75 for arg in "$@"; do
  76     if [ "${arg}" = "-" ]; then
  77         continue
  78     fi
  79     if [ ! -e "${arg}" ]; then
  80         printf "no file named \"%s\"\n" "${arg}" > /dev/stderr
  81         failed=1
  82     fi
  83 done
  84 
  85 if [ "${failed}" -gt 0 ]; then
  86     exit 2
  87 fi
  88 
  89 bat=''
  90 if [ -e /usr/bin/batcat ]; then
  91     bat='/usr/bin/batcat'
  92 elif [ -e /usr/bin/bat ]; then
  93     bat='/usr/bin/bat'
  94 fi
  95 
  96 got_chafa=0
  97 if [ -e '/usr/bin/chafa' ]; then
  98     got_chafa=1
  99 fi
 100 
 101 gap=0
 102 
 103 for arg in "$@"; do
 104     [ "${gap}" -gt 0 ] && printf "\n"
 105     gap=1
 106 
 107     printf "\e[7m%-80s\e[0m\n" "${arg}"
 108 
 109     if [ "${arg}" = '-' ]; then
 110         kind='-: text/plain'
 111     else
 112         kind="$(file --mime-type "${arg}")"
 113     fi
 114 
 115     if echo "${kind}" | grep -E -q ' image/[a-z0-9\.-]+$'; then
 116         if [ "${got_chafa}" -eq 1 ]; then
 117             chafa "${arg}"
 118         else
 119             printf "%s\n" "${kind}"
 120         fi
 121         continue
 122     fi
 123 
 124     if echo "${kind}" | grep -q ' inode/directory$'; then
 125         printf "%s is a folder\n" "${arg}"
 126         continue
 127     fi
 128 
 129     if echo "${kind}" | grep -E -q -v ' text/[a-z0-9\.\+-]+$'; then
 130         if echo "${kind}" | grep -E -q -v ' application/j(avascript|son)$'; then
 131             printf "%s\n" "${kind}"
 132             continue
 133         fi
 134     fi
 135 
 136     if [ -z "${bat}" ]; then
 137         awk '{ printf "%6d  %s\n", NR, $0 }' "${arg}"
 138         continue
 139     fi
 140 
 141     "${bat}" \
 142         --style=plain,header,numbers \
 143         --theme='Monokai Extended Light' \
 144         --wrap=never --color=always --paging=never "${arg}" |
 145     # make colors readable even on light backgrounds
 146     sed -e 's-\x1b\[38;5;70m-\x1b[38;5;28m-g' \
 147         -e 's-\x1b\[38;5;214m-\x1b[38;5;208m-g' \
 148         -e 's-\x1b\[38;5;243m-\x1b[38;5;103m-g' \
 149         -e 's-\x1b\[38;5;238m-\x1b[38;5;245m-g' \
 150         -e 's-\x1b\[38;5;228m-\x1b[48;5;228m-g'
 151 done 2>&1 | less "${less_options}"