#!/usr/bin/env bash # bar: a second tmux status row assembled from independent modules. # # bar mount set status 2 and build status-format[1] from the modules file # bar on | off | status fan out to every bar- module on PATH # bar set publish a segment by hand (a hook, a script, an agent) # bar format print the status-format[1] string without touching tmux # # Modules file (~/.config/tmux/bar/modules, BAR_MODULES overrides): one # " " per line, # comments, order = display order. # A segment is #{?@bar_,...,} so an empty option hides it, separator # included. Daemons follow bin/bar-lib; anything else publishes with `bar set`. set -uo pipefail MODULES="${BAR_MODULES:-${XDG_CONFIG_HOME:-$HOME/.config}/tmux/bar/modules}" SEP='#[fg=#908caa] │ #[default]' die() { printf 'bar: %s\n' "$*" >&2; exit "${2:-1}"; } # Prints "align name" pairs; the default is a lone sys module on the right. modules() { [ -f "$MODULES" ] || { echo "right sys"; return; } local n=0 align name extra while read -r align name extra; do n=$((n + 1)) case "$align" in ''|'#'*) continue ;; esac [ -n "$name" ] && [ -z "$extra" ] || die "$MODULES:$n: expected ' '" 2 case "$align" in left|centre|right) ;; *) die "$MODULES:$n: align must be left, centre or right" 2 ;; esac [[ "$name" =~ ^[a-z0-9_]+$ ]] || die "$MODULES:$n: name must be lowercase [a-z0-9_]" 2 echo "$align $name" done <"$MODULES" } segment() { printf '#{?@bar_%s,%s#{@bar_%s},}' "$1" "$2" "$1"; } group() { local align=$1 first=1 a name printf '#[align=%s default]' "$align" while read -r a name; do [ "$a" = "$align" ] || continue segment "$name" "$([ $first = 1 ] || printf '%s' "$SEP")" first=0 done <<<"$2" } format() { local list; list=$(modules) || exit $? group left "$list"; group centre "$list"; group right "$list"; echo } each_module() { local a name while read -r a name; do command -v "bar-$name" >/dev/null 2>&1 && "bar-$name" "$1" done < <(modules) } case "${1:-}" in format) format ;; mount) fmt=$(format) || exit $? tmux set -g status 2 && tmux set -g 'status-format[1]' "$fmt" ;; on) each_module --on ;; off) each_module --off ;; status) while read -r a name; do printf '%-8s %-7s %s\n' "$name" "$a" "$(tmux show -gqv "@bar_$name" 2>/dev/null | sed 's/#\[[^]]*\]//g')" done < <(modules) ;; set) [[ "${2:-}" =~ ^[a-z0-9_]+$ ]] || die "set: name must be lowercase [a-z0-9_]" 2 BAR_MODULE=$2 . "$(dirname "$(readlink -f "$0")")/bar-lib" bar_publish "${3:-}" ;; *) sed -n '2,12p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;; esac