#!/bin/sh # The MIT License (MIT) # # Copyright (c) 2026 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. # v [option...] [files...] # # # View is a simple non-interactive file viewer, which handles plain-text files # and pictures of all commonly-used formats. # # 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 '/^# +v /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac 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 continue ;; --header=*) less_options="${less_options} ${arg}" shift continue ;; -s|--s|-sixel|--sixel|-sixels|--sixels) sixels=1 # less_options="${less_options} -r" shift continue ;; -t|--t|-top|--top) less_options="${less_options} --header=1" shift continue ;; esac break done if [ "${sixels}" -eq 0 ]; then less_options="${less_options} -R" fi if [ $# -eq 0 ]; then less ${less_options} -f - exit $? fi bat_style='plain,header,numbers' if [ $# -lt 2 ]; then bat_style='plain,numbers' 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 bat='' if [ -e /usr/bin/batcat ]; then bat='/usr/bin/batcat' elif [ -e /usr/bin/bat ]; then bat='/usr/bin/bat' else printf "file viewer 'bat'/'batcat' not found" > /dev/stderr exit 1 fi view_pic='' if [ -e /usr/bin/chafa ]; then view_pic='chafa' if [ "${sixels}" -eq 1 ]; then view_pic='chafa --format sixels' fi fi gap=0 for arg in "$@"; do [ "${gap}" -gt 0 ] && printf "\n" gap=1 printf "\e[7m%-80s\e[0m\n" "${arg}" if [ "${arg}" = '-' ]; then kind='-: text/plain' else kind="$(file --mime-type "${arg}")" fi if echo "${kind}" | grep -E -q ' image/[a-z0-9\.-]+$'; then if [ -z "${view_pic}" ]; then printf "%s\n" "${kind}" else ${view_pic} "${arg}" fi continue fi if echo "${kind}" | grep -q ' inode/directory$'; then printf "%s is a folder\n" "${arg}" continue fi if echo "${kind}" | grep -E -q -v ' text/[a-z0-9\.\+-]+$'; then if echo "${kind}" | grep -E -q -v ' application/j(avascript|son)$'; then printf "%s\n" "${kind}" continue fi fi if [ -z "${bat}" ]; then awk '{ printf "%6d %s\n", NR, $0 }' "${arg}" continue fi "${bat}" \ --style="${bat_style}" \ --theme='Monokai Extended Light' \ --wrap=never --color=always --paging=never "${arg}" | # make colors readable even on light backgrounds sed -e 's-\x1b\[38;5;70m-\x1b[38;5;28m-g' \ -e 's-\x1b\[38;5;214m-\x1b[38;5;208m-g' \ -e 's-\x1b\[38;5;243m-\x1b[38;5;103m-g' \ -e 's-\x1b\[38;5;238m-\x1b[38;5;245m-g' \ -e 's-\x1b\[38;5;228m-\x1b[48;5;228m-g' done 2>&1 | less ${less_options}