dotfiles/.local/bin/scripts/b

24 lines
997 B
Text
Raw Normal View History

2021-09-20 11:14:34 +02:00
#!/bin/bash
2021-07-29 16:02:20 +02:00
# [b]rowse - custom ls or bat depending on file type
# Show type & contents of given files or PWD
2021-10-15 13:41:05 +02:00
# depends: tput stat bat neovim
2020-12-10 17:34:56 +01:00
2021-07-13 12:15:58 +02:00
set -eo pipefail
2020-12-10 17:34:56 +01:00
# gets the last arg or current dir
2020-11-16 12:29:31 +01:00
for last; do true; done
last=${last:-.}
2020-12-10 17:34:56 +01:00
# Elevate permissions if the file is not accessible
test -x "$(dirname "$last")" &&
{ test -r "$last" || ! test -e "$last"; } ||
elevate=sudo
tput setaf 4 && $elevate file -E "$last" | ( grep -v --color=never 'directory$' || true )
2020-12-10 17:34:56 +01:00
2021-07-13 12:15:58 +02:00
case "$($elevate file --dereference --mime "$last")" in
2021-07-29 12:44:51 +02:00
*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;;
2021-10-15 13:41:05 +02:00
*binary) expr "$(file "$last")" : ".*compressed" >/dev/null && /usr/share/nvim/runtime/macros/less.sh "$@";;
*) $elevate bat --style header "$@" --pager='less -XF'
2021-07-13 12:15:58 +02:00
tput setaf 3 && $elevate stat --format '%A size %sB, birth: %.10w mod %.10y' "$last"
;;
2020-11-16 12:29:31 +01:00
esac