summaryrefslogtreecommitdiff
path: root/bin/dmenu/tmux_umpv
blob: 8818b0befca97c8703a85497c8a9f6c4b668981e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/env sh

# Open links using mpv in a new tmux window while setting the socket name to selection.
# If the chosen socket is already active, load the urls to the mpv instance instead.
# Make sure the urls are printed as they are added.

sock_dir="${MPV_SOCKET_DIR:-$XDG_RUNTIME_DIR/mpv/sockets}"
[ -d "$sock_dir" ] || mkdir -p "$sock_dir"
sock="$(find "$sock_dir" -maxdepth 1 -mindepth 1 -type s -printf "%f\n" | dmenu -p "Choose mpv instance:")"
[ -z "$sock" ] && exit 1

if printf '{ "command": ["get_property", "path"] }\n' | nc -NU "$sock_dir/$sock" >/dev/null 2>&1; then
  for link in "$@"; do
    [ -e "$link" ] && link="$(realpath -s "$link")"
    jq -cn --arg l "$link" '{"command":["loadfile",($l),"append"]},{"command":["print-text","LOADFILE:= "+($l)]}'
  done | nc -NU "$sock_dir/$sock" >/dev/null
else
  tmux neww -t mpv: mpv --volume=50 --script-opts=socket="$sock",print_filenames=yes "$@"
fi