#!/bin/sh # The MIT License (MIT) # # Copyright © 2025 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # vrt [option...] [files...] # # # View Rich Text is a simple non-interactive plain-text file viewer, which # handles ANSI-codes and renders pictures out of data-URI lines. # # The options are, available in single and double-dashed versions # # -h show this help message # -help show this help message # # -header=n always show first `n` lines at the top # # -n show line numbers for overall results # -N show line numbers for overall results # # -s show pictures using the sixel format (experimental) # -sixel show pictures using the sixel format (experimental) # # -t show first line at the top at all times # -top show first line at the top at all times case "$1" in -h|--h|-help|--help) awk '/^# +vrt /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac width=0 sixels=0 # less_options='-MKiCRS' less_options='-MKiCS' for arg in "$@"; do if [ "${arg}" = '--' ]; then shift break fi case "${arg}" in -n|--n|-N|--N) less_options="${less_options} -N" shift ;; --header=*) less_options="${less_options} ${arg}" shift ;; -s|--s|-sixel|--sixel|-sixels|--sixels) sixels=1 # less_options="${less_options} -r" shift ;; -t|--t|-top|--top) less_options="${less_options} --header=1" shift ;; -w|--w|-width|--width) shift if [ $# -eq 0 ]; then printf "expected number after width option\n" >&2 exit 1 fi width="$1" shift ;; esac done if [ "${sixels}" -eq 0 ]; then less_options="${less_options} -R" fi if [ "${width}" -eq 0 ]; then width="$(tput cols)" fi command='awk' if [ -e /usr/bin/gawk ]; then command='gawk' fi if [ $# -eq 0 ]; then less "${less_options}" -f - exit $? fi if [ $# -lt 2 ]; then less_options="${less_options} --header=1" fi # show all non-existing files given failed=0 for arg in "$@"; do if [ "${arg}" = "-" ]; then continue fi if [ ! -e "${arg}" ]; then printf "no file named \"%s\"\n" "${arg}" > /dev/stderr failed=1 fi done if [ "${failed}" -gt 0 ]; then exit 2 fi view_pic='' if [ -e /usr/bin/chafa ]; then view_pic='chafa' if [ "${sixels}" -eq 1 ]; then view_pic='chafa --format sixels' fi fi ${command} -v width="${width}" -v view_pic="${view_pic}" ' BEGIN { if (width >= 170) width = (width - 10) / 2 fmt = "sh -c \"base64 -d | %s --size %dx\"" render_pic_cmd = sprintf(fmt, view_pic, width) } FNR == 1 { if (files++ > 0) print "" printf "\x1b[7m%-80s\x1b[0m\n", FILENAME } { gsub(/\r$/, "") } /^data:image\/[a-z]+;base64,[A-Za-z0-9/+]*=*$/ { if (view_pic == "") { print "image renderer \"chafa\" not found" > "/dev/stderr" exit 1 } gsub(/^data:image\/[a-z]+;base64,/, "") print | render_pic_cmd close(render_pic_cmd) next } # { print; next } /\t/ { print; next } { pos = 0 for (i = 1; i <= NF; i++) { plain = $i gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", plain) w = length(plain) + (i > 1) if (pos + w >= width || w >= width) { print "" pos = 0 } pos += w if (i == 1) printf "%s", $i else printf " %s", $i } if (pos > 0 || NF == 0) print "" } ' "$@" | less "${less_options}"