dotfiles/vifm/scripts/vifm_bg_open.sh

94 lines
1.7 KiB
Bash
Raw Normal View History

2023-12-23 20:13:47 -07:00
#!/usr/bin/env bash
print_help() {
cat <<HELP
2023-12-23 20:13:53 -07:00
Open given files in backgrounded programs, preferably using GUI apps and tmux
2023-12-23 20:13:47 -07:00
2023-12-23 20:13:53 -07:00
Intended for use with vifm + tmux
2023-12-23 20:13:47 -07:00
HELP
}
2023-12-23 20:13:53 -07:00
test -z "${SWAYSOCK+x}"
declare -r is_gui=$?
requires_gui_error() {
printf 'Sway not running. Will not open gui apps\n' >&2
exit 1
}
open_pdf() {
2024-04-15 18:20:25 -06:00
zathura "$(sed 's#\\##g' <<<"$1")" &>/dev/null &
2023-12-23 20:13:53 -07:00
}
open_image() {
imv "$1" &
}
open_av_media() {
local has_video="$(ffprobe -hide_banner "$1" 2>&1 | awk '/Stream.+Video/')"
if [[ -n "$TMUX" && -z "$has_video" ]] || [[ ! $is_gui && -n "$TMUX" ]]; then
tmux split-window -h
sleep .2 # Let bash login, otherwise command won't get sent
tmux send-keys "mpv $1" Enter
elif [[ $is_gui ]]; then
mpv --force-window "$1" &>/dev/null &
fi
}
open_webpage() {
chromium "$1" &>/dev/null &
}
open_vim() {
tmux new-window -c "#{pane_current_path}"
sleep .2 # Let bash login, otherwise command won't get sent
tmux send-keys "vi $1" Enter
}
2023-12-23 20:13:47 -07:00
open_file() {
case "${1##*.}" in
pdf)
2023-12-23 20:13:53 -07:00
if [[ $is_gui ]]; then
open_pdf "$1"
else
requires_gui_error
fi
2023-12-23 20:13:47 -07:00
;;
2024-04-15 18:20:25 -06:00
avif|icns|jpeg|jpg|png|webp|svg)
2023-12-23 20:13:53 -07:00
if [[ $is_gui ]]; then
open_image "$1"
2023-12-23 20:13:47 -07:00
else
2023-12-23 20:13:53 -07:00
requires_gui_error
2023-12-23 20:13:47 -07:00
fi
;;
2023-12-23 20:13:53 -07:00
mkv|mp3|mp4|webm)
open_av_media "$1"
;;
2023-12-23 20:13:47 -07:00
html)
2023-12-23 20:13:53 -07:00
if [[ $is_gui ]]; then
open_webpage "$1"
else
requires_gui_error
fi
2023-12-23 20:13:47 -07:00
;;
*)
2023-12-23 20:13:53 -07:00
if [[ -n "$TMUX" && -f "$1" && "$(stat -c%s "$1")" -le 1000000 ]]; then
open_vim "$1"
fi
2023-12-23 20:13:53 -07:00
;;
2023-12-23 20:13:47 -07:00
esac
}
for arg in "$@"
do
case "$arg" in
-h|--help)
print_help
exit 0
;;
*)
open_file "$arg"
;;
esac
done