#!/usr/bin/env osh
shopt -s strict:all
DEP="vim mpv fbida-fbi fbida-fbgs"
DEC="Browse files on your filesystem"
DOC="
An interactive prompt-driven filebrowser. Enter the name of the
folder to enter to cd or enter a file to open it.
Set \$F_FILES_HANDLERS to customize handlers to use to open files.
Note that sequence matters. Each line in \$F_FILES_HANDLERS has 4
parts seperated by the ^ character, the first field is a regex
for grep, the second field is fbp fields to populate, and the
third is the command to run which will be suffixed with "\$@".
The fourth field is an optional arbitrary shell code to be eval'd
after terminating the program. This can be helpful to run cleanup
code for buggy applications like fbi which don't always properly
on C-c or for any other terminal restoration code.
"
VAR="
F_FILES_HANDLERS='
[.](mp4|mov|avi)$ ^ 9 0 [ ] p q ^ mpv --volume=10 --vo=tct ^
[.](webm|ogg|mp3|m4a|wav|flac|opus)$ ^ 9 0 [ ] p q ^ mpv --volume=10 --no-video ^
[.](jpg|jpeg|gif|webm|png)$ ^ q j k a + - 100s 150s 200s v h i q ^ fbi ^ pkill -9 fbi
[.](pdf)$ ^ q j k a + - 100s 150s 200s v h i q ^ fbgs -r 200 ^ pkill -9 fbi
. ^ Z X Q :q :w :wq j k h l ^ vim ^
'
F_FILES_FBSET_FBPRATIO='2 / 3'
F_FILES_FBSET_YRES='1440'
"
terminate() {
reset
exit 0
}
handle() {
local EXT FBKEYS CMD CLEANUP
IFS=$'\n'
for HANDLER in $F_FILES_HANDLERS; do
echo "$HANDLER" | grep -q . || continue
IFS=$' '
EXT="$(echo "$HANDLER" | cut -d^ -f1 | tr -d ' ')"
FBPKEYS="$(echo "$HANDLER" | cut -d^ -f2)"
CMD="$(echo "$HANDLER" | cut -d^ -f3 | awk '{$1=$1};1')"
CLEANUP="$(echo "$HANDLER" | cut -d^ -f4)"
if echo "$1" | grep -qiE "$EXT"; then
[ -p /tmp/fbp.fifo ] && {
echo -e "\b\f\e"
echo "$FBPKEYS" | tr " " "\n"
} > /tmp/fbp.fifo
clear
# Note, custom fbsetting applicable because fbi
fbset -yres "$(echo "$F_FILES_FBSET_YRES * $F_FILES_FBSET_FBPRATIO" | bc)"
$CMD "$@"
fbset -yres "$F_FILES_FBSET_YRES"
[ -p /tmp/fbp.fifo ] && echo -e "\v" > /tmp/fbp.fifo
eval "$CLEANUP"
clear
return
fi
done
[ -p /tmp/fbp.fifo ] && echo -e "\r" > /tmp/fbp.fifo
}
promptfolder() {
local FOLDER OPTIONS PICKED
FOLDER="$1"
echo "Listing folder: $PWD"
ls "$FOLDER"
[ -p /tmp/fbp.fifo ] && { echo -e '\b\f\r../\n*'; ls -1p "$FOLDER"; } > /tmp/fbp.fifo
read -p "Select: " PICKED; PICKED="$(echo "$PICKED" | tr -d '[:cntrl:]')"
echo "$PICKED" | grep -E '^[*]$' && handle ./*
[ -d "$PICKED" ] && cd "$PICKED" && return
[ -f "$PICKED" ] && handle "$PICKED"
}
main() {
trap terminate HUP TERM INT
env | grep -q "^$(basename "$0" | tr '[a-z]' '[A-Z]')=" || eval "$VAR"
PWD="$HOME"
while true; do
promptfolder "$PWD"
done
}
if [ -n "$1" ]; then "$@"; else main; fi