-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdwm_statusBar.sh
144 lines (126 loc) · 3.74 KB
/
dwm_statusBar.sh
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
# Screenshot: http://s.natalian.org/2013-08-17/dwm_status.png
# Network speed stuff stolen from http://linuxclues.blogspot.sg/2009/11/shell-script-show-network-speed.html
# This function parses /proc/net/dev file searching for a line containing $interface data.
# Within that line, the first and ninth numbers after ':' are respectively the received and transmited bytes.
function get_bytes {
# Find active network interface
interface=$(ip route get 8.8.8.8 2>/dev/null| awk '{print $5}')
line=$(grep $interface /proc/net/dev | cut -d ':' -f 2 | awk '{print "received_bytes="$1, "transmitted_bytes="$9}')
eval $line
now=$(date +%s%N)
}
# Function which calculates the speed using actual and old byte number.
# Speed is shown in KByte per second when greater or equal than 1 KByte per second.
# This function should be called each second.
function get_velocity {
value=$1
old_value=$2
now=$3
timediff=$(($now - $old_time))
velKB=$(echo "1000000000*($value-$old_value)/1024/$timediff" | bc)
if test "$velKB" -gt 1024
then
echo $(echo "scale=2; $velKB/1024" | bc)MB/s
else
echo ${velKB}KB/s
fi
}
getMusic() {
msc="$(ncmpcpp --current-song '{%a - %t}|{%f}')"
}
# Get initial values
get_bytes
old_received_bytes=$received_bytes
old_transmitted_bytes=$transmitted_bytes
old_time=$now
print_volume() {
volume="$(amixer get Master | tail -n1 | sed -r 's/.*\[(.*)%\].*/\1/')"
if test "$volume" -gt 0
then
echo -e "\uE05D${volume}"
else
echo -e "Mute"
fi
}
print_wifi() {
ip=$(ip route get 8.8.8.8 2>/dev/null|grep -Eo 'src [0-9.]+'|grep -Eo '[0-9.]+')
# if=wlp3s0
# while IFS=$': \t' read -r label value
# do
# case $label in SSID) SSID=$value
# ;;
# signal) SIGNAL=$value
# ;;
# esac
# done < <(iw "$if" link)
#
echo -e "$SSID$SIGNAL$ip"
}
print_mem(){
memfree=$(($(grep -m1 'MemAvailable:' /proc/meminfo | awk '{print $2}') / 1024))
echo -e "$memfree"
}
print_temp(){
test -f /sys/class/thermal/thermal_zone0/temp || return 0
echo $(head -c 2 /sys/class/thermal/thermal_zone0/temp)C
}
print_bat(){
hash acpi || return 0
onl="$(grep "on-line" <(acpi -V))"
charge="$(awk '{ sum += $1 } END { print sum }' /sys/class/power_supply/BAT*/capacity)"
if test -z "$onl"
then
# suspend when we close the lid
#systemctl --user stop inhibit-lid-sleep-on-battery.service
echo -e "${charge}"
else
# On mains! no need to suspend
#systemctl --user start inhibit-lid-sleep-on-battery.service
echo -e "${charge}"
fi
}
print_date(){
#date "+%a %m-%d %T"
date "+%a %m-%d %I:%M:%S %p"
}
show_record(){
test -f /tmp/r2d2 || return
rp=$(cat /tmp/r2d2 | awk '{print $2}')
size=$(du -h $rp | awk '{print $1}')
echo " $size $(basename $rp)"
}
fetchmail --check 2>/dev/null | while read line; do
new=`echo $line | sed 's/(//' | awk '{print $1-$3}'`
if [ $new != 0 ] && [ ! -e ~/.message ]; then
echo "New mail($new)" > ~/.message
echo "!!! !!! !!!" >> ~/.message
sleep 60
if grep '^New mail' ~/.message >/dev/null 2>/dev/null; then
rm -f ~/.message
fi
fi
done &
while true; do
if [ -r ~/.message ]; then
while read line; do
xsetroot -name "$line"
sleep 1
done < ~/.message
else
# Get new transmitted, received byte number values and current time
get_bytes
getMusic
print_volume
#print_wifi
# Calculates speeds
vel_recv=$(get_velocity $received_bytes $old_received_bytes $now)
vel_trans=$(get_velocity $transmitted_bytes $old_transmitted_bytes $now)
xsetroot -name "$vel_recv $vel_trans $volume $msc $(print_temp) $(print_wifi) $(print_bat)% $(show_record) $(print_date)"
# Update old values to perform new calculations
old_received_bytes=$received_bytes
old_transmitted_bytes=$transmitted_bytes
old_time=$now
fi
done &
rm -f ~/.message