dotfiles

My dotfiles used for LARBS
Log | Files | Refs | README | LICENSE

dwmbar (3631B)


      1 #!/usr/bin/env sh
      2 
      3 # This script sets the statusbar with the xsetroot command at the end. Have it
      4 # started by ~/.xinitrc or ~/.xprofile.
      5 
      6 # Handle SIGTRAP signals sent by refbar to update the status bar immediately.
      7 trap 'update' 5
      8 
      9 # Set the deliminter character.
     10 delim="|"
     11 
     12 # testweather checks to see if the most recent forecast is up to date.  If it
     13 # isn't, it downloads a new weather forecast, then signals to update the
     14 # statusbar. Gets weather report from wttr.in.
     15 testweather() { \
     16 	[ "$(stat -c %y "$HOME/.local/share/weatherreport" 2>/dev/null | cut -d' ' -f1)" != "$(date '+%Y-%m-%d')" ] &&
     17 		ping -q -c 1 1.1.1.1 >/dev/null &&
     18 		curl -s "wttr.in/$location" > "$HOME/.local/share/weatherreport" &&
     19 		notify-send "🌞 Weather" "New weather forecast for today." &&
     20 		refbar
     21 		}
     22 
     23 # Here is the (big) function that outputs the appearance of the statusbar. It
     24 # can really be broken down into many submodules which I've commented and
     25 # explained.
     26 status() { \
     27 
     28 	# Get current mpd track filename or artist - title if possible.
     29 	mpc -f "[[%artist% - ]%title%]|[%file%]" 2>/dev/null | grep -v "volume:" | head -n 1 && echo "$delim"
     30 
     31 	# If the weather report is current, show daily precipitation chance,
     32 	# low and high.  Don't even bother to understand this one unless you
     33 	# have all day. Takes the weather report format from wttr.in and makes
     34 	# it look like how I want it for the status bar.
     35 	[ "$(stat -c %y "$HOME/.local/share/weatherreport" 2>/dev/null | cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ] &&
     36 		sed '16q;d' "$HOME/.local/share/weatherreport" | grep -wo "[0-9]*%" | sort -n | sed -e '$!d' | sed -e "s/^/ /g" | tr -d '\n' &&
     37 		sed '13q;d' "$HOME/.local/share/weatherreport" | grep -o "m\\(-\\)*[0-9]\\+" | sort -n -t 'm' -k 2n | sed -e 1b -e '$!d' | tr '\n|m' ' ' | awk '{print " ",$1 "°","",$2 "°"}' &&
     38 		echo "$delim"
     39 
     40 	# Get the volume of ALSA's master volume output.  Show an icon if or
     41 	# not muted.
     42 	amixer get Master | grep -o "[0-9]*%\|\[on\]\|\[off\]" | sed "s/\[on\]//;s/\[off\]//"
     43 
     44 	echo "$delim"
     45 
     46 	# Wifi quality percentage and  icon if ethernet is connected.
     47 	grep "^\s*w" /proc/net/wireless | awk '{ print "", int($3 * 100 / 70) "%" }'
     48 	sed "s/down//;s/up//" /sys/class/net/e*/operstate
     49 
     50 	# Show unread mail if mutt-wizard is installed.
     51 	command -v mw >/dev/null 2>&1 &&
     52 		echo "$delim" &&
     53 		du -a ~/.local/share/mail/*/INBOX/new/* 2>/dev/null | wc -l | sed 's/^/:/'
     54 		echo "$delim"
     55 
     56 	# Will show all batteries with approximate icon for remaining power.
     57 	for x in /sys/class/power_supply/BAT?/capacity;
     58 	do
     59 	case "$(cat $x)" in
     60 		100|9[0-9])	echo "" ;;
     61 		8[0-9]|7[0-9])	echo "" ;;
     62 		6[0-9]|5[0-9])	echo "" ;;
     63 		4[0-9]|3[0-9])	echo "" ;;
     64 		*)		echo "" ;;
     65 	esac
     66 	done && echo "$delim"
     67 
     68 	# Date and time.
     69 	date '+%Y %b %d (%a) %I:%M%p'
     70 	}
     71 
     72 update() { \
     73 	# So all that big status function was just a command that quicking gets
     74 	# what we want to be the statusbar. This xsetroot command is what sets
     75 	# it. Note that the tr command replaces newlines with spaces. This is
     76 	# to prevent some weird issues that cause significant slowing of
     77 	# everything in dwm. Note entirely sure of the cause, but again, the tr
     78 	# command easily avoids it.
     79 	xsetroot -name "$(status | tr '\n' ' ')" &
     80     wait
     81 
     82 	# Check to see if new weather report is needed.
     83 	testweather &
     84     wait
     85     }
     86 
     87 
     88 while :; do
     89     update
     90 	# Sleep for a minute after changing the status bar before updating it
     91 	# again. We run sleep in the background and use wait until it finishes,
     92     # because traps can interrupt wait immediately, but they can't do that
     93     # with sleep.
     94 	sleep 1m &
     95     wait
     96 done