From b056804a8c63fa8ce95f49b4635a015acb360ad9 Mon Sep 17 00:00:00 2001 From: Valentin Gehrke Date: Wed, 10 Jun 2015 08:33:16 +0200 Subject: [PATCH] Multiprocess bar :D --- scripts/bar.sh | 123 +++++++++++++++++++++++++++------------------ scripts/barinfo.sh | 9 +++- 2 files changed, 82 insertions(+), 50 deletions(-) diff --git a/scripts/bar.sh b/scripts/bar.sh index db82110..72b6ff0 100755 --- a/scripts/bar.sh +++ b/scripts/bar.sh @@ -2,7 +2,7 @@ FIFO="/tmp/bar.fifo" -FORMAT_LEFT="%(music)s %(services)s %(diskspace)s" +FORMAT_LEFT=" %(music)s %(services)s %(diskspace)s" FORMAT_RIGHT="%(wifi)s %(battery)s %(clock)s " icon_color="#A0A57E" @@ -24,16 +24,20 @@ color_battery_good="white" color_battery_low="#C37561" music() { - info=$(mpc current -f '[%title%]') - icon=$icon_music - if [ -n "$info" ]; then - if [ -n "$(mpc status | grep paused)" ]; then - icon=$icon_music_paused + 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 - echo -n "%{A:I "music 20":}$icon $info%{A}" - else - echo -n "$icon (-)" - fi + + mpc idle >/dev/null; + done; } serviceinfo() { @@ -48,46 +52,71 @@ serviceinfo() { } services() { - echo -n "$icon_services $(serviceinfo zomnet z) $(serviceinfo docker d)" + while true; do + info="$icon_services $(serviceinfo zomnet z)" + echo services $info > $FIFO; + sleep 5; + done; } diskspace() { - info=$(./cmdcache.sh 10 "df -h /" | awk '{if($6 == "/") { print $5 }}') - echo -n "%{A:I "filesystem 30":}$icon_diskspace $info%{A}" + 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() { - 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 -n "%{A:I "network 1061":}$icon_wifi $info%{A}" + 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() { - level=$(./cmdcache.sh 5 "acpi -b" | awk -F, 'BEGIN { s=0 } { s+=$2 } END { print int(s/NR) }') - charging=$(acpi -a | grep on-line) + 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 + 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 -n "%{A:I 'battery 1061':}$icon $info%{A}" + echo battery "%{A:I battery 1061:}$icon $info%{A}" > $FIFO + + sleep 5; + done; } clock() { - info=$(date +"%H:%M") - echo -n "%{A:I "clock 1061":}$icon_clock $info%{A}" + 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() { - updates=$(./cmdcache.sh 20 ./pkginfo.sh) - echo -n "$icon_updates $updates" + while true; do + updates=$(pkginfo.sh) + echo -n "$icon_updates $updates" + sleep 20 + done } run_handler() { @@ -101,26 +130,22 @@ run_handler() { done } -run_worker() { - SLEEP=$1 - TAG=$2 - COMMAND=$3 - ( while true; do - echo -n "$TAG " - $COMMAND - echo - sleep $SLEEP - done > $FIFO ) & -} - cd $(dirname $0) +#Trap magic +trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT + +#Fifo [ -e "$FIFO" ] && rm -f $FIFO mkfifo $FIFO +#start workers +clock & +battery & +music & +services & +diskspace & +wifi & -run_worker 1 clock clock -run_worker 5 battery battery - -( while true; do cat $FIFO; done ) | python ./barformatter.py "$FORMAT_LEFT" "$FORMAT_RIGHT" # | lemonbar -g 1366x15 -f "Stlarch:size=6:style=regular" -f "Terminus:size=8" -B "#88000000" | run_handler +( while true; do cat $FIFO; done ) | python ./barformatter.py "$FORMAT_LEFT" "$FORMAT_RIGHT" | lemonbar -g 1366x15 -f "Stlarch:size=6:style=regular" -f "Terminus:size=8" -B "#88000000" | run_handler diff --git a/scripts/barinfo.sh b/scripts/barinfo.sh index 469e98d..5f9de1d 100755 --- a/scripts/barinfo.sh +++ b/scripts/barinfo.sh @@ -6,6 +6,10 @@ header(){ echo "%{F$HEADER_COLOR}$1%{F-}" } +tag() { + echo -n "%{F#HEADER_COLOR}$1:${F-}%{r}" +} + ### Filesystem info filesystem() { @@ -24,7 +28,10 @@ network() { ### Music info music() { header "Music" - mpc current -f "$(header Artist:) %artist%\n$(header Album:) %album%\n$(header Title:) %title%" + #mpc current -f "$(header Artist:) %artist%\n$(header Album:) %album%\n$(header Title:) %title%" + tag "Artist"; mpc current -f "%artist%" + tag "Album"; mpc current -f "%album%" + tag "Title"; mpc current -f "%title%" } ### Battery info