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 for arg in "$@"; do 52 if [ "${arg}" = '--' ]; then 53 shift 54 break 55 fi 56 57 case "${arg}" in 58 -n|--n|-N|--N) 59 less_options="${less_options} -N" 60 shift 61 continue 62 ;; 63 64 --header=*) 65 less_options="${less_options} ${arg}" 66 shift 67 continue 68 ;; 69 70 -s|--s|-sixel|--sixel|-sixels|--sixels) 71 sixels=1 72 # less_options="${less_options} -r" 73 shift 74 continue 75 ;; 76 77 -t|--t|-top|--top) 78 less_options="${less_options} --header=1" 79 shift 80 continue 81 ;; 82 esac 83 84 break 85 done 86 87 if [ "${sixels}" -eq 0 ]; then 88 less_options="${less_options} -R" 89 fi 90 91 if [ $# -eq 0 ]; then 92 less ${less_options} -f - 93 exit $? 94 fi 95 96 bat_style='plain,header,numbers' 97 if [ $# -lt 2 ]; then 98 bat_style='plain,numbers' 99 less_options="${less_options} --header=1" 100 fi 101 102 # show all non-existing files given 103 failed=0 104 for arg in "$@"; do 105 if [ "${arg}" = "-" ]; then 106 continue 107 fi 108 if [ ! -e "${arg}" ]; then 109 printf "no file named \"%s\"\n" "${arg}" > /dev/stderr 110 failed=1 111 fi 112 done 113 114 if [ "${failed}" -gt 0 ]; then 115 exit 2 116 fi 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" > /dev/stderr 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 142 printf "\e[7m%-80s\e[0m\n" "${arg}" 143 144 if [ "${arg}" = '-' ]; then 145 kind='-: text/plain' 146 else 147 kind="$(file --mime-type "${arg}")" 148 fi 149 150 if echo "${kind}" | grep -E -q ' image/[a-z0-9\.-]+$'; then 151 if [ -z "${view_pic}" ]; then 152 printf "%s\n" "${kind}" 153 else 154 ${view_pic} "${arg}" 155 fi 156 continue 157 fi 158 159 if echo "${kind}" | grep -q ' inode/directory$'; then 160 printf "%s is a folder\n" "${arg}" 161 continue 162 fi 163 164 if echo "${kind}" | grep -E -q -v ' text/[a-z0-9\.\+-]+$'; then 165 if echo "${kind}" | grep -E -q -v ' application/j(avascript|son)$'; then 166 printf "%s\n" "${kind}" 167 continue 168 fi 169 fi 170 171 if [ -z "${bat}" ]; then 172 awk '{ printf "%6d %s\n", NR, $0 }' "${arg}" 173 continue 174 fi 175 176 "${bat}" \ 177 --style="${bat_style}" \ 178 --theme='Monokai Extended Light' \ 179 --wrap=never --color=always --paging=never "${arg}" | 180 # make colors readable even on light backgrounds 181 sed -e 's-\x1b\[38;5;70m-\x1b[38;5;28m-g' \ 182 -e 's-\x1b\[38;5;214m-\x1b[38;5;208m-g' \ 183 -e 's-\x1b\[38;5;243m-\x1b[38;5;103m-g' \ 184 -e 's-\x1b\[38;5;238m-\x1b[38;5;245m-g' \ 185 -e 's-\x1b\[38;5;228m-\x1b[48;5;228m-g' 186 done 2>&1 | less ${less_options}