summaryrefslogtreecommitdiff
path: root/.script/dwmstatus
blob: 9f288e1e4d32b062a33f98c4dfa5e405465f1619 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/sh

# This script sets the statusbar with the xsetroot command at the end.
# Call it inside ~/.xinitrc or ~/.xprofile.

# Handle SIGTRAP signals sent by refbar to update the status bar immediately.
trap 'update' 5

# Set the deliminter character.
delim="|"

# testweather checks to see if the most recent forecast is up to date. If it isnt' it
# downloads a new weather forecast, then signals to update the statusbar. Gets weather report from wttr.in.
testweather() { \
	[ "$(stat -c %y "$HOME/.local/share/weatherreport" 2>/dev/null | cut -d' ' -f1)" != "$(date '+%Y-%m-%d')" ] &&
		ping -q -c 1 1.1.1.1 >/dev/null &&
		curl -s "wttr.in/$location" > "$HOME/.local/share/weatherreport" &&
		notify-send "🌞 Weather" "New weather forecast for today." &&
		$HOME/.script/refreshstatus
}

# Here is the cumulative function that outputs and formats the appearance of the statusbar.
# It can really be broken down into many submodules which are commented and explained.
status() { \
	# Get current mpd track filename or artist - title if possible.
	mpc -f "[[%artist% - ]%title%]|[%file%]" 2>/dev/null | grep -v "volume:" | head -n 1 && echo "$delim"

	# If the weather report is current, show daily precipitation chance, low and high.
	# Takes the weather report from wttr.in and formats it for the status bar.
	[ "$(stat -c %y "$HOME/.local/share/weatherreport" 2>/dev/null | cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ] &&
		sed '16q;d' "$HOME/.local/share/weatherreport" | grep -wo "[0-9]*%" | sort -n | sed -e '$!d' | sed -e "s/^/ /g" | tr -d '\n' &&
		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 "°"}' &&
		echo "$delim"

	# Get the volume of ALSA's master volume output. Show an icon if or not muted.
	amixer get Master | grep -o "[0-9]*%\|\[on\]\|\[off\]" | sed "s/\[on\]//;s/\[off\]//"

	echo "$delim"

	# Wifi quality percentage and icon if ethernet is connected.
	grep "^\s*w" /proc/net/wireless | awk '{ print "", int($3 * 100 / 70) "%" }'
	sed "s/down//;s/up//" /sys/class/net/e*/operstate

	# Will show all batteries with approximate icon for remaining power.
	for x in /sys/class/power_supply/BAT?/capacity;
	do
		case "$(cat $x)" in
			100|9[0-9])	echo "" ;;
			8[0-9]|7[0-9])	echo "" ;;
			6[0-9]|5[0-9])	echo "" ;;
			4[0-9]|3[0-9])	echo "" ;;
			*)		echo "" ;;
		esac
	done && echo "$delim"

	# Date and time.
	date '+%Y %b %d (%a) %I:%M%p'
}

update() { \
	# Have xsetroot set dwm status bar with our formatted input.
	# Note that the tr command replaces newlines with spaces. This is
	# to prevent some weird issues that cause significant slowing of
	# everything in dwm. Cause unknown.
	xsetroot -name "$(status | tr '\n' ' ')" & wait

	# Check to see if new weather report is needed.
	testweather & wait
}

while :; do
    update
	# Sleep for a minute after changing the status bar before updating it
	# again. We run sleep in the background and use wait until it finishes,
	# because traps can interrupt wait immediately, but they can't do that with sleep.
	sleep 1m &
    wait
done