2021-09-20 09:14:34 +00:00
|
|
|
#!/bin/bash
|
2021-07-29 14:02:20 +00:00
|
|
|
# [b]rowse - custom ls or bat depending on file type
|
2020-10-23 12:00:41 +00:00
|
|
|
# Show type & contents of given files or PWD
|
2021-10-15 11:41:05 +00:00
|
|
|
# depends: tput stat bat neovim
|
2021-11-02 12:00:02 +00:00
|
|
|
# args: files to inspect
|
2020-12-10 16:34:56 +00:00
|
|
|
|
2021-07-13 10:15:58 +00:00
|
|
|
set -eo pipefail
|
2021-11-02 12:00:02 +00:00
|
|
|
# get the last arg or current dir
|
2020-11-16 11:29:31 +00:00
|
|
|
for last; do true; done
|
|
|
|
last=${last:-.}
|
2020-12-10 16:34:56 +00:00
|
|
|
|
2021-10-12 06:42:22 +00:00
|
|
|
# Elevate permissions if the file is not accessible
|
|
|
|
test -x "$(dirname "$last")" &&
|
|
|
|
{ test -r "$last" || ! test -e "$last"; } ||
|
|
|
|
elevate=sudo
|
2021-11-04 11:24:23 +00:00
|
|
|
tput setaf 4 && $elevate file -E "$last" | { grep -v --color=never 'directory$' || true; }
|
2020-12-10 16:34:56 +00:00
|
|
|
|
2021-07-13 10:15:58 +00:00
|
|
|
case "$($elevate file --dereference --mime "$last")" in
|
2021-11-11 19:45:58 +00: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 -RF;;
|
2021-11-04 11:24:23 +00:00
|
|
|
(*binary)
|
2021-11-07 02:50:04 +00:00
|
|
|
case "$(file "$last")" in
|
|
|
|
(*:\ *compress*) /usr/share/nvim/runtime/macros/less.sh "$@";;
|
|
|
|
(*:\ *\ image\ data*) viu --height "$(echo $LINES/2 | bc)" "$@";;
|
|
|
|
esac
|
2021-11-04 11:24:23 +00:00
|
|
|
tput setaf 3 && $elevate stat --format '%A size %sB, birth: %.10w mod %.10y' "$last"
|
|
|
|
;;
|
2021-11-11 19:45:58 +00:00
|
|
|
(*) $elevate bat --style header "$@" --pager 'less -RF'
|
2021-11-04 11:24:23 +00:00
|
|
|
tput setaf 3 && $elevate stat --format '%A size %sB, birth: %.10w mod %.10y' "$last"
|
|
|
|
# TODO don't duplicate stat
|
|
|
|
;;
|
2020-11-16 11:29:31 +00:00
|
|
|
esac
|