dots/files/.local/bin/bar.sh
2026-03-25 14:14:08 +01:00

119 lines
2.9 KiB
Bash
Executable file

#!/bin/bash
# shellcheck disable=SC2059,SC2064,SC2086,SC2001,SC2016
# simple lightweight lemonbar script for use with dk
set -eE -o pipefail
bg="#000000" # background colour
fg="#ffffff" # foreground colour
hi="#00aaaa" # highlight colour
ul=2 # underline thickness
sep="┃" # separator text
fifo="/tmp/bar.fifo"
font="Iosevka:size=11"
clock()
{
date +"T %a %H:%M "
# sync up the clock to the minute mark
min=$(date +"%M")
while [[ $(date +"%M") == "$min" ]]; do
sleep 1
done
while :; do
date +"T %a %H:%M "
sleep 60
done
}
battery()
{
while :; do
perc="$(cat /sys/class/power_supply/BAT0/capacity)"
echo "B bat: ${perc}%"
sleep 60
done
}
volume()
{
vol="$(pactl get-sink-volume @DEFAULT_SINK@ | grep -oE '[1-9]+%' | head -n1)"
echo "V vol: ${vol}"
}
workspaces()
{
# these will be filled out once we eval each line
typeset name="" title="" layout="" focused=false active=false
WS=""
while read -r l; do
printf '%s\n\n' "$1" >> "$HOME/a" 2>&1
if [[ $l =~ ^title=* ]]; then
# default foreground, background, and underline colours
# changing the foreground and underline based on
# the active and focused state
f="$fg" b="$bg" u="$fg";
$active && u="$hi"
if $focused; then
# clicking the layout symbol will cycle the layout
lyt="%{A:dkcmd set layout cycle:}${layouts[$layout]}%{A}"
WIN="${title:0:50}"
f="$hi"
fi
# clicking on a workspace name will view it
WS="$WS%{F$f}%{B$b}%{+u}%{U$u}%{A:dkcmd ws $name:} $name %{A}%{-u}%{B-}%{F-}"
fi
# turn the dk JSON output into lines that can be `eval`ed one by one,
# filling out the following fields: name, focused, active, layout, title
done < <(dkcmd -p <<< "$1" | sed '/.*\[\|]/d; /[{}],\?/d; s/^\s*\|,$//g; /"monitor":\|"number":\|"id":/d; s/"\(.*\)": /\1=/; s/\$(/\\\$(/g; s/`/\\`/g')
WS="$WS $lyt"
}
parsefifo()
{
# globals to simplify the workspaces call
declare -g WS
local time vol bat net
while read -r line; do
case $line in
T*) time="${line#?}" ;;
V*) vol="${line#?}" ;;
B*) bat="${line#?}" ;;
N*) net="${line#?}" ;;
'{'*) workspaces "$line" ;;
esac
echo "%{l} ${WS}%{c}${time}%{r}${bat}${vol} "
done
}
# kill the process and cleanup if we exit or get killed
trap "trap - TERM; rm -f '$fifo'; kill 0" INT TERM QUIT EXIT PIPE
# make the fifo
[ -e "$fifo" ] && rm "$fifo"
mkfifo "$fifo"
# here we dump info into the FIFO, order does not matter things are parsed
# out using the first character of the line. Click commands for left button
# can be added by passing an argument containing the command (like below)
#
# comment a line to remove the "module"
clock > "$fifo" &
battery > "$fifo" &
volume > "$fifo" &
dkcmd status type=bar > "$fifo" &
# run the pipeline
if [[ $1 == '-b' ]]; then
parsefifo < "$fifo" | lemonbar -b -a 32 -u $ul -B "$bg" -F "$fg" -f "$font" | sh;
else
parsefifo < "$fifo" | lemonbar -a 32 -u $ul -B "$bg" -F "$fg" -f "$font" | sh
fi
# vim:ft=sh:fdm=marker:fmr={,}