Sway bar with Cmus and PulseAudio

Last Update: 2023-04-16

The following shows script to create a simple, text-based bar for sway. It shows the current track in cmus, the current kernel version, network default gateway, keyboard layout, sound volume, mounts, CPU utilisation and time.

This works with pipewire, too (assuming you have the pipewire pulseaudio compatibility layer installed).

#!/usr/bin/env bash
export DELIM='|'

CURRENT_SINK=$(pactl list short sinks \
  | grep RUNNING \
  | sed -e 's,^\([0-9][0-9]*\)[^0-9].*,\1,')
CURRENT_NOW=$(pactl list sinks \
  | grep '^[[:space:]]Volume:' \
  | head -n $(( $CURRENT_SINK + 1 )) \
  | tail -n 1 \
  | sed -e 's,.* \([0-9][0-9]*\)%.*,\1,')

function cmus_metadata_title() {
  local CURRENT_CMUS="$(cmus-remote -Q)"
  local TITLE="$(echo "$CURRENT_CMUS" | grep "^tag title " | cut -d ' ' -f 3-)"
  local ARTIST="$(echo "$CURRENT_CMUS" | grep "^tag artist " | cut -d ' ' -f 3-)"
  if [[ -z "$ARTIST" ]] ; then
    echo $TITLE
  elif [[ -z "$TITLE" ]] ; then
    echo $ARTIST
  else
    echo $ARTIST - $TITLE
  fi
}

CMUS_STATUS="$(cmus_metadata_title)"

function not_empty_delim() {
  if ! [[ -z "$1" ]] ; then 
    echo $DELIM 
  fi
}

echo \
$CMUS_STATUS $(not_empty_delim "$CMUS_STATUS") \
Version: $(uname -r) $DELIM \
Gateway: $(ip route  | grep ^default | cut -d ' ' -f 3-3) $DELIM \
Layout: $(swaymsg -t get_inputs \
  | jq -r '[.[]|select(.type == "keyboard")][0] \
  | .xkb_active_layout_name') $DELIM \
Volume: $CURRENT_NOW $DELIM \
$(lsblk -o MOUNTPOINT | grep -v '^$' | tail -n +2) $DELIM \
$(cat /proc/loadavg | cut -d ' ' -f 3-3) $DELIM \
$(date +'%Y-%m-%d %k:%M:%S') \

And change the bar setting in ~/.config/sway/config to

bar {
    position top

    # When the status_command prints a new line to stdout, swaybar updates.
    # The default just shows the current date and time.
    status_command while true ; ~/.config/sway/bar.sh; do sleep 1; done
	#status_command waybar -l off
	#status_command swaybar
	font Noto Sans Mono 8
	tray_output *
	icon_theme Adwaita

    colors {
        statusline #ffffff
        background #323232
        inactive_workspace #32323200 #32323200 #5c5c5c
    }
}