configfiles/bspwm/.config/scripts/bar.sh

194 lines
4.3 KiB
Bash
Executable file

#!/bin/bash
FIFO="/tmp/bar.fifo"
FORMAT=" %(music)s %(services)s %(diskspace)s %%{c} %(debug)s %%{r} %(updates)s %(wifi)s %(batterysymbol)s %(battery)s %(clock)s "
icon_color="#AAAAAA"
icon() { echo -n "%{F$icon_color}"; printf '%b' "\ue$1"; echo -n "%{F-}"; }
icon_music=$(icon 05c)
icon_music_paused=$(icon 059)
icon_services=$(icon 040)
icon_diskspace=$(icon 0ab)
icon_wifi=$(icon 048)
icon_battery=$(icon 033)
icon_battery_charging=$(icon 042)
icon_clock=$(icon 018)
icon_updates=$(icon 060)
color_service_running="#EEEEEE"
color_service_stopped="#666666"
color_battery_good="#EEEEEE"
color_battery_low="#C37561"
music() {
while true; do
info=$(mpc current -f '[%artist%] - [%title%]')
icon=$icon_music
if [ -n "$info" ]; then
if [ -n "$(mpc status | grep paused)" ]; then
icon=$icon_music_paused
fi
echo music "$icon $info" > $FIFO;
else
echo music "$icon (-)" > $FIFO;
fi
mpc idle >/dev/null;
done;
}
serviceinfo() {
service=$1
text=$2
systemctl is-active -q $service
if [ "$?" -eq 0 ]; then
echo -n "%{F$color_service_running}$text%{F-}"
else
echo -n "%{F$color_service_stopped}$text%{F-}"
fi
}
services() {
while true; do
info="$icon_services $(serviceinfo zomnet z)"
echo services $info > $FIFO;
sleep 5;
done;
}
diskspace() {
while true; do
info=$(df -h / | awk '{if($6 == "/") { print $5 }}')
echo diskspace "$icon_diskspace $info" > $FIFO;
sleep 20;
done;
}
wifi_wicd() {
while true; do
info=$(wicd-cli --wireless --status | perl -e 'foreach $line (<>) { if($line =~ /Connected to (.+) at .+IP: ([0-9.]+)/ ) { print $1; break; } elsif($line =~ /Connecting to wireless network \"(.+)\"/) { print $1 . " - connecting"; break; } }')
if [ -z "$info" ]; then
info="(-)"
fi
echo wifi "$icon_wifi $info" > $FIFO;
sleep 1;
done;
}
wifi_nm() {
while true; do
info=$(nmcli -t -f state,connection d | awk -F: '{ if($1=="connected") { print $2; exit; } }')
if [ -z "$info" ]; then
info="(-)"
fi
echo wifi "$icon_wifi $info" > $FIFO;
sleep 2;
done;
}
battery() {
while true; do
level=$(acpi -b | awk -F, 'BEGIN { s=0 } { s+=$2 } END { print int(s/NR) }')
info="%{F$color_battery_good}$level%%{F-}"
if [ "$level" -lt 20 ]; then
info="%{F$color_battery_low}$level%%{F-}"
fi
echo battery "$info" > $FIFO
sleep 2m;
done;
}
batterysymbol() {
while true; do
icon=$icon_battery
[ -n "$(acpi -a | grep on-line)" ] && icon=$icon_battery_charging
echo batterysymbol "$icon" > $FIFO
sleep 5;
done
}
clock() {
while true; do
info=$(date +"%d.%m. (%V) %H:%M")
#Bad hack: convert to int to remove leading 0s
seconds_to_wait=$[ 61 - $(date +"%S" | awk '{print int($0)}') ];
if [ "$seconds_to_wait" -gt 10 ]; then
seconds_to_wait=10
fi
echo clock "$icon_clock $info" > $FIFO;
sleep $seconds_to_wait
done;
}
updates() {
pac_updates() {
checkupdates | wc -l
}
aur_updates() {
yaourt -Quaq | wc -l
}
useless_packages() {
pacman -Qtdq | wc -l
}
while true; do
internet=$(nmcli -t -f state d | grep connected)
if [ -z "$internet" ]; then
if [ -z "$pac" ]; then
pac=?
fi
if [ -z "$aur" ]; then
aur=?
fi
else
pac=$(pac_updates)
aur=$(aur_updates)
fi
useless=$(useless_packages)
updates="$pac+$aur-$useless"
echo updates "$icon_updates $updates" > $FIFO;
sleep 20
done
}
cd $(dirname $0)
#Fifo
[ -e "$FIFO" ] && rm -f $FIFO
mkfifo $FIFO
#start workers
clock &
battery &
batterysymbol &
music &
services &
diskspace &
wifi_nm &
updates &
#Trap magic
trap "kill -9 -$$" TERM INT
echo Trap set for $$
(
( while [ -e "$FIFO" ]; do cat $FIFO; done ) | python ./barformatter.py "$FORMAT" | lemonbar -g 1366x18 -f "Stlarch:size=6:style=regular" -f "Inconsolata:size=8" -B "#88000000"
) &
wait $!