configfiles/scripts/bar.sh

152 lines
3.6 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 %(battery)s %(clock)s "
icon_color="#A0A57E"
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="#A0A57E"
color_service_stopped="#C37561"
color_battery_good="white"
color_battery_low="#C37561"
music() {
while true; do
info=$(mpc current -f '[%title%]')
icon=$icon_music
if [ -n "$info" ]; then
if [ -n "$(mpc status | grep paused)" ]; then
icon=$icon_music_paused
fi
echo music "%{A:I music 20:}$icon $info%{A}" > $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 "%{A:I filesystem 30:}$icon_diskspace $info%{A}" > $FIFO;
sleep 20;
done;
}
wifi() {
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 "%{A:I network 1061:}$icon_wifi $info%{A}" > $FIFO;
sleep 1;
done;
}
battery() {
while true; do
level=$(acpi -b | awk -F, 'BEGIN { s=0 } { s+=$2 } END { print int(s/NR) }')
charging=$(acpi -a | grep on-line)
icon=$icon_battery
if [ -n "$charging" ]; then
icon=$icon_battery_charging
fi
info="%{F$color_battery_good}$level%%{F-}"
if [ "$level" -lt 20 ]; then
info="%{F$color_battery_low}$level%%{F-}"
fi
echo battery "%{A:I battery 1061:}$icon $info%{A}" > $FIFO
sleep 5;
done;
}
clock() {
while true; do
info=$(date +"%H:%M")
#Bad hack: convert to int to remove leading 0s
seconds_to_wait=$[ 61 - $(date +"%S" | awk '{print int($0)}') ];
echo clock "%{A:I "clock 1061":}$icon_clock $info%{A}" > $FIFO;
sleep $seconds_to_wait
done;
}
updates() {
while true; do
updates=$(./pkginfo.sh)
echo updates "$icon_updates $updates" > $FIFO;
sleep 20
done
}
run_handler() {
while read type param; do
case $type in
I)
./barinfo.sh kill
./barinfo.sh $param &
;;
esac
done
}
cd $(dirname $0)
#Trap magic
trap "trap - SIGTERM && kill -9 -- -$$" SIGINT SIGTERM EXIT
#Fifo
[ -e "$FIFO" ] && rm -f $FIFO
mkfifo $FIFO
#start workers
clock &
battery &
music &
services &
diskspace &
wifi &
updates &
( while true; do cat $FIFO; done ) | python ./barformatter.py "$FORMAT" | lemonbar -g 1366x15 -f "Stlarch:size=6:style=regular" -f "Terminus:size=8" -B "#88000000" | run_handler
exit 0