Project

General

Profile

single script for cmus

nona - 17 days ago -

Hi!

I want to share a script to control cmus. I could not find a better way to leave it in the background and control it with a single script.

#!/bin/sh
# Copyright 2024 nona (labs.parabola.nu)
#
# This program is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation,
# version 3 of the License.
#
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public
# License along with this program. If not, see
# <https://www.gnu.org/licenses/>.
#
# Runs cmus with qterminal
#
# Requires cmus, cmus-remote, tmux, qterminal
# The following assumes that you called it mycmus
#
# 1. Start cmus by calling this script without options:
#        mycmus
#    This will open a qterminal with cmus (read the manual
#    or tutorial of cmus to know how to use it; try pressing
#    [5] to navigate through files)
#
# 2. If you wish, close the terminal (do not press [q])
#
# 3. Instead of typing cmus-remote <something>, just
#    call this script again with <something>. Example:
#        mycmus --play
#
# Loosely based on
# - https://wiki.archlinux.org/title/Cmus#Remote_Control
# - https://github.com/cmus/cmus/wiki/detachable-cmus
# - https://unix.stackexchange.com/a/726532
# (how to fork, send to background, keep alive ?? !!)

if ! pgrep -x cmus ; then
  # xterm -e 'tmux new-session -A -D -s cmus "$(which --skip-alias cmus)"' &

  # Thanks to -d the client will start a new session
  # (dispatch the job), but it won't attach the new session
  # to the terminal. The client won't put you into the
  # session, it will exit almost immediately, this will make
  # the shell interpreting the script continue.
  # (https://unix.stackexchange.com/a/726532)
  tmux new-session -d -s cmus "$(which --skip-alias cmus)";
fi;

if [[ x$1 == x ]]; then
     qterminal -e "tmux attach-session -t cmus";
elif [[ x$1 != x ]]; then
     cmus-remote $@;
fi