File: vrt.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 # vrt [option...] [files...] 27 # 28 # View Rich Text is a simple non-interactive plain-text file viewer, which 29 # handles ANSI-codes and renders pictures out of data-URI lines. 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 '/^# +vrt /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 43 exit 0 44 ;; 45 esac 46 47 width=0 48 sixels=0 49 # less_options='-MKiCRS' 50 less_options='-MKiCS' 51 52 for arg in "$@"; do 53 if [ "${arg}" = '--' ]; then 54 shift 55 break 56 fi 57 58 case "${arg}" in 59 -n|--n|-N|--N) 60 less_options="${less_options} -N" 61 shift 62 continue 63 ;; 64 65 --header=*) 66 less_options="${less_options} ${arg}" 67 shift 68 continue 69 ;; 70 71 -s|--s|-sixel|--sixel|-sixels|--sixels) 72 sixels=1 73 # less_options="${less_options} -r" 74 shift 75 continue 76 ;; 77 78 -t|--t|-top|--top) 79 less_options="${less_options} --header=1" 80 shift 81 continue 82 ;; 83 84 -w|--w|-width|--width) 85 shift 86 if [ $# -eq 0 ]; then 87 printf "expected number after width option\n" >&2 88 exit 1 89 fi 90 width="$1" 91 shift 92 continue 93 ;; 94 esac 95 96 break 97 done 98 99 if [ "${sixels}" -eq 0 ]; then 100 less_options="${less_options} -R" 101 fi 102 103 if [ "${width}" -eq 0 ]; then 104 width="$(tput -T xterm cols)" 105 fi 106 107 if [ $# -eq 0 ]; then 108 less ${less_options} -f - 109 exit $? 110 fi 111 112 if [ $# -lt 2 ]; then 113 less_options="${less_options} --header=1" 114 fi 115 116 # show all non-existing files given 117 failed=0 118 for arg in "$@"; do 119 if [ "${arg}" = "-" ]; then 120 continue 121 fi 122 if [ ! -e "${arg}" ]; then 123 printf "no file named \"%s\"\n" "${arg}" > /dev/stderr 124 failed=1 125 fi 126 done 127 128 if [ "${failed}" -gt 0 ]; then 129 exit 2 130 fi 131 132 view_pic='' 133 if [ -e /usr/bin/chafa ]; then 134 view_pic='chafa' 135 if [ "${sixels}" -eq 1 ]; then 136 view_pic='chafa --format sixels' 137 fi 138 fi 139 140 awk -v width="${width}" -v view_pic="${view_pic}" ' 141 BEGIN { 142 if (width >= 170) width = (width - 10) / 2 143 fmt = "sh -c \"base64 -d | %s --size %dx\"" 144 render_pic_cmd = sprintf(fmt, view_pic, width) 145 } 146 147 FNR == 1 { 148 if (files++ > 0) print "" 149 printf "\x1b[7m%-80s\x1b[0m\n", FILENAME 150 } 151 152 { gsub(/\r$/, "") } 153 154 /^data:image\/[a-z]+;base64,[A-Za-z0-9/+]*=*$/ { 155 if (view_pic == "") { 156 print "image renderer \"chafa\" not found" > "/dev/stderr" 157 exit 1 158 } 159 160 gsub(/^data:image\/[a-z]+;base64,/, "") 161 print | render_pic_cmd 162 close(render_pic_cmd) 163 next 164 } 165 166 # { print; next } 167 168 /\t/ { print; next } 169 170 { 171 pos = 0 172 173 for (i = 1; i <= NF; i++) { 174 plain = $i 175 gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", plain) 176 w = length(plain) + (i > 1) 177 178 if (pos + w >= width || w >= width) { 179 print "" 180 pos = 0 181 } 182 183 pos += w 184 185 if (i == 1) printf "%s", $i 186 else printf " %s", $i 187 } 188 189 if (pos > 0 || NF == 0) print "" 190 } 191 ' "$@" | less ${less_options}