#!/bin/sh SCRIPT="$(basename "$0")" long='width,verbose,help' short='wvh' if ! opts="$(getopt -o $short -l $long -n "$SCRIPT" -- "$@")"; then exit 1 fi eval set -- "$opts" help() { cat <<-EOF $SCRIPT Toby Vincent $SCRIPT Filter script used for lesspipe.sh filtering USAGE: $SCRIPT [OPTION ...] [PATH ...] OPTIONS: -w, --width Specify width. By default uses FZF_PREVIEW_COLUMNS if set, and falls back to COLUMNS -v, --verbose Increase verbosity -h, --help Show this help EOF } say() { if [ -z "$LESSQUIET" ]; then printf "%s: %s\n" "$SCRIPT" "$*" fi } say_verbose() { if [ "$verbose" -gt "0" ]; then say "$*" fi } say_err() { say "$*" >&2 } err() { err_dir="$1" shift say_err "cannot preview '$err_dir': $*" exit 1 } err_help() { help err "$*" } has() { command -v "$1" >/dev/null } in_git_repo() { [ -d "$1/.git" ] || git -C "$1" rev-parse --is-inside-work-tree >/dev/null 2>&1 } filetype() { case "$1" in https://git.sr.ht*) ftype="sourcehut" ;; https://github.com*) ftype="github" ;; *.md | *.MD | *.mkd | *.markdown | *.rst) ftype='markdown' ;; /*) [ -d "$1" ] && ftype='directory' ;; "${1%/*}/${1#*/}") for remote in ${GIT_REMOTES:-https://git.sr.ht https://github.com}; do if git ls-remote "$remote/$1" CHECK_GIT_REMOTE_URL_REACHABILITY; then ftype=$(filetype "$remote/$1") break fi done ;; esac printf %s\\n "$ftype" } verbose=0 width=${FZF_PREVIEW_COLUMNS:-$COLUMNS} while true; do case "$1" in -h | --help) help exit 0 ;; -v | --verbose) verbose=$((verbose + 1)) shift ;; -w | --width) width=$2 shift 2 ;; --) shift break ;; *) err_help "Invalid argument: $1" ;; esac done if [ "$#" -eq 0 ]; then IFS=' ' set -o noglob # shellcheck disable=2046 set -- $(cat) fi if [ -z "$width" ]; then width=$(tput cols) fi ft=$(filetype "$1") case "$ft" in directory) if has onefetch && in_git_repo "$1" 2>/dev/null; then if [ "$width" -lt "80" ]; then version=$( ( onefetch --version | sed 's/onefetch //' printf '2.16' ) | sort -V | tail -n1 ) if [ "$version" = '2.16.0' ]; then args='--no-art' else args='--show-logo=never' fi fi cmd="onefetch --include-hidden $args" elif [ -f "$1/README.md" ]; then exec $SCRIPT "$1/README.md" else cmd="exa --tree --git-ignore --level=3 --icons" fi ;; markdown) had bat && cmd="bat --color always" ;; sourcehut) has hut && cmd="hut git show --repo" ;; github) has gh && cmd="gh repo view" ;; esac if [ -z "$cmd" ]; then exit 1 fi $cmd "$1" 2>/dev/null