This repository has been archived on 2024-09-01. You can view files and clone it, but cannot push or open issues or pull requests.
al-skel/dk-home/.local/bin/bar

186 lines
4.5 KiB
Plaintext
Raw Normal View History

2021-12-24 19:15:44 -06:00
#!/bin/bash
# shellcheck disable=SC2059,SC2064,SC2086
# simple lightweight lemonbar script for use with dk
2022-01-14 12:53:53 -06:00
set -eE -o pipefail
2021-12-24 19:15:44 -06:00
bg="#111111"
fg="#666666"
highlight="#6699ee"
underline=3
separator="┃"
2022-01-09 04:20:37 -06:00
# xfonts (adjust size based on resolution)
2022-08-21 11:47:51 -05:00
px=$(xrandr | grep ' connected' | tail -n1 | grep -o '[0-9]\+x[0-9]\+' | cut -d'x' -f2)
mm=$(xrandr | grep ' connected' | tail -n1 | grep -o '[0-9]\+mm' | tail -n1 | sed 's/mm//')
2022-08-12 18:35:35 -05:00
dpi=$(( (px / mm) * 25 ))
if (( dpi >= 140 )); then
2022-01-02 13:54:13 -06:00
font0="-xos4-terminus-medium-r-normal--24-240-72-72-c-120-iso10646-1"
2022-08-12 18:35:35 -05:00
elif (( dpi >= 120 )); then
2022-01-02 13:54:13 -06:00
font0="-xos4-terminus-medium-r-normal--18-240-72-72-c-120-iso10646-1"
2022-08-12 18:35:35 -05:00
elif (( dpi >= 100 )); then
font0="-xos4-terminus-medium-r-normal--14-240-72-72-c-120-iso10646-1"
2022-01-02 13:54:13 -06:00
else
font0="-xos4-terminus-medium-r-normal--12-240-72-72-c-120-iso10646-1"
2022-01-02 13:54:13 -06:00
fi
2021-12-24 19:15:44 -06:00
font1=""
font2=""
font3=""
# xft fonts
# font0="monospace:pixelsize=24"
# font1="Font Awesome 5 Brands:pixelsize=20"
# font2="icomoon:pixelsize=18"
# font3="Anonymice Nerd Font Mono:pixelsize=18"
fifo="/tmp/bar.fifo"
# mimic dwm style layout symbols
typeset -A layouts=(
[tile]="[]="
[mono]="[M]"
[none]="><>"
[grid]="###"
[spiral]="(@)"
[dwindle]="[\\]"
[tstack]="F^F"
)
clock()
{
if [[ $1 ]]; then
while :; do
date +"T%%{A1:$1:} %a %H:%M %%{A}"
sleep 10
done
else
while :; do
date +"T %a %H:%M "
sleep 10
done
fi
}
battery()
{
if [[ $1 ]]; then
while :; do
2022-08-20 14:44:07 -05:00
printf 'B%s\n' "%{A1:$1:} Bat: $(acpi --battery 2>/dev/null | grep -v 'Unknown\| 0%' | cut -d, -f2 | tr -d '[:space:]') %{A}${separator}"
2021-12-24 19:15:44 -06:00
sleep 10
done
else
while :; do
2022-08-20 14:44:07 -05:00
printf 'B%s\n' " Bat: $(acpi --battery 2>/dev/null | grep -v 'Unknown\| 0%' | cut -d, -f2 | tr -d '[:space:]') ${separator}"
2021-12-24 19:15:44 -06:00
sleep 10
done
fi
}
volume()
{
if [[ $1 ]]; then
while :; do
2022-08-20 14:44:07 -05:00
printf 'V%s\n' "%{A1:$1:} Vol: $(pamixer --get-volume-human) %{A}${separator}"
2021-12-24 19:15:44 -06:00
sleep 0.2
done
else
while :; do
2022-08-20 14:44:07 -05:00
printf 'V%s\n' " Vol: $(pamixer --get-volume-human) ${separator}"
2021-12-24 19:15:44 -06:00
sleep 0.2
done
fi
}
2022-08-12 18:35:35 -05:00
network()
{
check()
{
if hash nm-online > /dev/null 2>&1 && [[ $(systemctl is-active NetworkManager.service) == "active" ]]; then
nm-online > /dev/null 2>&1
else
ping -qc1 'archlinux.org' > /dev/null 2>&1
fi
}
if [[ $1 ]]; then
until check; do
2022-08-20 14:44:07 -05:00
printf 'N%s\n' "%{A1:$1:} disconnected %{A}${separator}"
2022-08-12 18:35:35 -05:00
sleep 5
done
while :; do
2022-08-20 14:44:07 -05:00
printf 'N%s\n' "%{A1:$1:} connected %{A}${separator}"
2022-08-12 18:35:35 -05:00
sleep 100
done
else
until check; do
2022-08-20 14:44:07 -05:00
printf 'N%s\n' " disconnected %{A}${separator}"
2022-08-12 18:35:35 -05:00
sleep 5
done
while :; do
2022-08-20 14:44:07 -05:00
printf 'N%s\n' " connected %{A}${separator}"
2022-08-12 18:35:35 -05:00
sleep 100
done
fi
}
2021-12-24 19:15:44 -06:00
parsefifo()
{
2022-08-20 14:44:07 -05:00
typeset f='' b='' u='' wm='' time='' net='' bat='' vol='' title='' layout=''
2021-12-24 19:15:44 -06:00
while read -r line; do
case $line in
T*) time="${line#?}" ;;
V*) vol="${line#?}" ;;
B*) bat="${line#?}" ;;
2022-08-12 18:35:35 -05:00
N*) net="${line#?}" ;;
2022-08-20 14:44:07 -05:00
A*) title="${line#?}"; title="${title:0:50}";;
2021-12-24 19:15:44 -06:00
L*) l="${line#?}"; layout="${layouts[$l]}" ;;
W*)
wm='' IFS=':' # set the internal field separator to ':'
set -- ${line#?} # split the line into arguments ($@) based on the field separator
for item in "$@"; do
name=${item#?}
case $item in
A*) f="$highlight" b="$bg" u="$highlight" ;; # occupied - focused
a*) f="$fg" b="$bg" u="$highlight" ;; # occupied - unfocused
I*) f="$highlight" b="$bg" u="$fg" ;; # unoccupied - focused
i*) f="$fg" b="$bg" u="$fg" ;; # unoccupied - unfocused
esac
wm="$wm%{F$f}%{B$b}%{+u}%{U$u}%{A:dkcmd ws $name:} $name %{A}%{-u}%{B-}%{F-}"
done
;;
esac
2022-08-20 14:44:07 -05:00
printf "%s\n" "%{l}$wm $separator $layout%{c}$title%{r}${net}${bat}${vol}${time}"
2021-12-24 19:15:44 -06:00
done
}
# kill the process and cleanup if we exit or get killed
2022-01-14 12:53:53 -06:00
trap "trap - TERM; kill 0; rm -f '$fifo'" INT TERM QUIT EXIT PIPE
2021-12-24 19:15:44 -06:00
# 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 volume below)
2022-08-20 14:44:07 -05:00
network 'al-terminal -e nmtui-connect' > "$fifo" &
2022-01-03 13:59:59 -06:00
clock 'gsimplecal' > "$fifo" &
2022-08-12 18:35:35 -05:00
battery '' > "$fifo" &
2021-12-24 19:15:44 -06:00
volume 'pavucontrol' > "$fifo" &
dkcmd status type=bar > "$fifo" &
# run the pipeline
if [[ $1 == '-b' ]]; then
parsefifo < "$fifo" | lemonbar -b -a 32 -u $underline -B "$bg" -F "$fg" -f "$font0" -f "$font1" -f "$font2" -f "$font3" | sh
else
parsefifo < "$fifo" | lemonbar -a 32 -u $underline -B "$bg" -F "$fg" -f "$font0" -f "$font1" -f "$font2" -f "$font3" | sh
fi
# vim:ft=sh:fdm=marker:fmr={,}