dotfiles/.local/bin/scripts/b

22 lines
963 B
Bash
Executable File

#!/bin/sh
# [b]rowse - custom ls or bat depending on file type
# Show type & contents of given files or PWD
set -eo pipefail
# gets the last arg or current dir
for last; do true; done
last=${last:-.}
# Elevate permissions if the file exists but is not readable
# TODO: find the first visible directory up the tree and check if that is accessible
test -e "$last" || test -e "$(dirname $last)" && { test -r "$last" || elevate=sudo; }
tput setaf 4 && $elevate file -E "$last" | ( grep -v --color=never 'directory$' || true )
case "$($elevate file --dereference --mime "$last")" in
*inode/directory*) tput sgr0 && $(test -x "$last" || echo "sudo") ls -l --color=always --human-readable --group-directories-first --file-type --dereference-command-line --all "$@" | less -XF;;
*binary) ;;
*) $elevate bat --style header --force-colorization "$@" | less -XF
tput setaf 3 && $elevate stat --format '%A size %sB, birth: %.10w mod %.10y' "$last"
;;
esac