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 while [ $# -gt 0 ]; do 53 if [ "$1" = '--' ]; then 54 shift 55 break 56 fi 57 58 case "$1" 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} $1" 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 [ "${arg}" = "-" ] && continue 120 [ -e "${arg}" ] && continue 121 printf "no file named \"%s\"\n" "${arg}" >&2 122 failed=1 123 done 124 125 [ "${failed}" -gt 0 ] && exit 2 126 127 view_pic='' 128 if [ -e /usr/bin/chafa ]; then 129 view_pic='chafa' 130 if [ "${sixels}" -eq 1 ]; then 131 view_pic='chafa --format sixels' 132 fi 133 fi 134 135 awk -v width="${width}" -v view_pic="${view_pic}" ' 136 BEGIN { 137 if (width >= 170) width = (width - 10) / 2 138 fmt = "sh -c \"base64 -d | %s --size %dx\"" 139 render_pic_cmd = sprintf(fmt, view_pic, width) 140 } 141 142 FNR == 1 { 143 if (files++ > 0) print "" 144 printf "\x1b[7m%-80s\x1b[0m\n", FILENAME 145 } 146 147 { gsub(/\r$/, "") } 148 149 /^data:image\/[a-z]+;base64,[A-Za-z0-9/+]*=*$/ { 150 if (view_pic == "") { 151 print "image renderer \"chafa\" not found" > "/dev/stderr" 152 exit 1 153 } 154 155 gsub(/^data:image\/[a-z]+;base64,/, "") 156 print | render_pic_cmd 157 close(render_pic_cmd) 158 next 159 } 160 161 # { print; next } 162 163 /\t/ { print; next } 164 165 { 166 pos = 0 167 168 for (i = 1; i <= NF; i++) { 169 plain = $i 170 gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", plain) 171 w = length(plain) + (i > 1) 172 173 if (pos + w >= width || w >= width) { 174 print "" 175 pos = 0 176 } 177 178 pos += w 179 180 if (i == 1) printf "%s", $i 181 else printf " %s", $i 182 } 183 184 if (pos > 0 || NF == 0) print "" 185 } 186 ' "$@" | less ${less_options}