aboutsummaryrefslogtreecommitdiffstats
path: root/dwm-statusbar
blob: 6ebd11c75a12b38518b8d4a385087a159ce727ca (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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env bash
#
# Status bar for dwm. Adapted from:
# https://github.com/w0ng/bin/blob/master/dwm-statusbar
#
# The functions are fairly system-dependent, so it is recommended to copy this file to your ~/bin (or equivalent) and make the adjustments there.

# Colour codes from dwm/config.h
colour_faded="\x01" # grey on black
colour_neutral="\x02" # blue on black
colour_warning="\x03" # yellow on black
colour_critical="\x04" # red on black
colour_normal="\x05" # grey on black

# The amount of spacing to use (or a spacer character, like |)
space="  "

# Time to wait between refreshes, in seconds
refresh="5"

# Name of the network adapters, like enp3s0
eth_adapter=""
wlan_adapter="wlp1s0"

print_date() {
  echo -ne "${colour_normal}$(date "+%d.%m.%y")"
}

print_time() {
  time_local="$(date "+%H:%M")"
  #time_us="$(TZ='US/Central' date "+%H:%M")"
  time_br="$(TZ='America/Sao_Paulo' date "+%H:%M")"

  echo -ne "${colour_neutral}\uE3D2${colour_normal}${time_local}${colour_faded}${time_br}"
  #echo -ne "${colour_neutral}\uE3D2${colour_normal}${time_local}"
}

print_volume() {
  # empty E022, low E023, full E024, mid E03B
  volume=$(amixer get Master -M | tail -n1 | sed -r 's/.*\[(.*)%\].*/\1/')
  mute="$(amixer get Master | tail -n1 | sed -r 's/.*\[(on|off)\].*/\1/')"
  
  if [ "$mute" == "off" ]; then
    vol_colour=$colour_warning
    icon_colour=$colour_warning
    icon="\uE022"
  else
    vol_colour=$colour_normal
    icon_colour=$colour_neutral
    if [ $volume -eq 0 ]; then
      icon="\uE022"
    elif [ $volume -lt 33 ]; then
      icon="\uE023"
    elif [ $volume -lt 66 ]; then
      icon="\uE03B"
    else
      icon="\uE024"
    fi
  fi
  
  echo -ne "${icon_colour}${icon}${vol_colour}${volume}%"
}

# This is very system-dependent
print_backlight() {
  # E3D8
  max_backlight=$(cat /sys/class/backlight/amdgpu_bl1/max_brightness)
  let backlight="100 * $(cat /sys/class/backlight/amdgpu_bl1/brightness) / $max_backlight"

  echo -ne "${colour_neutral}\uE3D8${colour_normal}${backlight}%"
}

# This will probably need tweaking
print_battery() {
  # charging E20D, empty E20E, full E20F, 2/3 E210, 1/3 E211
  # 1 or 0
  ac_status=$(cat /sys/class/power_supply/ACAD/online)
  
  if [ $ac_status -eq 1 ]; then
    icon_colour=$colour_neutral
    if [ -d "/sys/class/power_supply/BAT1" ]; then
      icon="\uE20D"
      bat_colour=$colour_normal
      bat_capacity="$(cat /sys/class/power_supply/BAT1/capacity)%"
    else
      icon="\uE257"
    fi
  else
    bat_capacity=$(cat /sys/class/power_supply/BAT1/capacity)
    if [ $bat_capacity -lt 10 ]; then
      bat_colour=$colour_critical
      icon_colour=$colour_critical
      icon="\uE20E"
    elif [ $bat_capacity -lt 35 ]; then
      bat_colour=$colour_warning
      icon_colour=$colour_warning
      icon="\uE211"
    elif [ $bat_capacity -lt 65 ]; then
      bat_colour=$colour_normal
      icon_colour=$colour_neutral
      icon="\uE1FB"
    elif [ $bat_capacity -lt 85 ]; then
      bat_colour=$colour_normal
      icon_colour=$colour_neutral
      icon="\uE210"
    else
      bat_colour=$colour_normal
      icon_colour=$colour_neutral
      icon="\uE20F"
    fi
    bat_capacity="${bat_capacity}%"
  fi
  
  echo -ne "${icon_colour}${icon}${bat_colour}${bat_capacity}"
}

# This should work out of the box
print_root_free() {
  # floppy E0C3 
  root_free=$(df -kh / | awk 'NR==2 {print $4}')
  root_percent=$(df -kh / | awk 'NR==2 {print $5}' | sed 's/%//g')
  
  if [ $root_percent -gt 90 ]; then
    icon_colour=$colour_critical
    root_colour=$colour_critical
  elif [ $root_percent -gt 75 ]; then
    icon_colour=$colour_warning
    root_colour=$colour_warning
  else
    icon_colour=$colour_neutral
    root_colour=$colour_normal
  fi
  
  echo -ne "${icon_colour}\uE0C3${root_colour}/: ${root_free} (${root_percent}%)"
}

# This should also work out of the box, but only makes sense if your /home is on a different partition
print_home_free() {
  # floppy E0C3 
  home_free=$(df -kh /home | awk 'NR==2 {print $4}')
  home_percent=$(df -kh /home | awk 'NR==2 {print $5}' | sed 's/%//g')
  
  if [ $home_percent -gt 90 ]; then
    icon_colour=$colour_critical
    home_colour=$colour_critical
  elif [ $home_percent -gt 75 ]; then
    icon_colour=$colour_warning
    home_colour=$colour_warning
  else
    icon_colour=$colour_neutral
    home_colour=$colour_normal
  fi
  
  echo -ne "${icon_colour}\uE0C3${home_colour}/home: ${home_free} (${home_percent}%)"
}

# This is highly system-dependent, so use it as a template only
print_shared_free() {
  # floppy E0C3 
  shared_free=$(df -kh /home/eddy/shared | awk 'NR==2 {print $4}')
  shared_percent=$(df -kh /home/eddy/shared | awk 'NR==2 {print $5}' | sed 's/%//g')
  
  if [ $shared_percent -gt 90 ]; then
    icon_colour=$colour_critical
    shared_colour=$colour_critical
  elif [ $shared_percent -gt 75 ]; then
    icon_colour=$colour_warning
    shared_colour=$colour_warning
  else
    icon_colour=$colour_neutral
    shared_colour=$colour_normal
  fi
  
  echo -ne "${icon_colour}\uE0C3${shared_colour}/mnt/shared: ${shared_free} (${shared_percent}%)"
}

# This should work out of the box
print_mem_free() {
  mem_free="$(free -m | awk '/Mem:/ {print $7}')"
  
  mem_colour=$colour_normal
  icon_colour=$colour_neutral
  if [ $mem_free -lt 250 ]; then
    mem_colour=$colour_critical
    icon_colour=$colour_critical
  elif [ $mem_free -lt 500 ]; then
    mem_colour=$colour_warning
    icon_colour=$colour_warning
  fi
  
  echo -ne "${icon_colour}\uE28B${mem_colour}${mem_free}M"
}

# This depends on lm_sensors and will likely need configuring
print_cpu_temp() {
  cpu_temp=$(sensors -u | awk '/edge/ {getline; print $2}' | sed -r 's/([0-9]*)\.[0-9]*/\1/')
  
  temp_colour=$colour_normal
  icon_colour=$colour_neutral
  if [ $cpu_temp -gt 83 ]; then
    temp_colour=$colour_critical
    icon_colour=$colour_critical
  elif [ $cpu_temp -gt 99 ]; then
    temp_colour=$colour_warning
    icon_colour=$colour_warning
  fi
  
  echo -ne "${icon_colour}\uE3B2${temp_colour}${cpu_temp}°"
}

# cpu (from: https://bbs.archlinux.org/viewtopic.php?pid=661641#p661641)
print_cpu_used() {
  echo -ne "${colour_neutral}\uE3AF${colour_normal}${cpu_used}%"
}

# Configure the adapter to display above
print_eth() {
  eth_status=$(ip addr show dev ${eth_adapter} | awk 'NR==1 {print $9}')
  
  if [ "$eth_status" == "UP" ]; then
    eth_ip=$(ip addr show dev ${eth_adapter} | awk 'NR==3 {print $2}' | sed -r 's/\/.*//')
    icon_colour=${colour_neutral}
    eth_colour=${colour_normal}
  else
    icon_colour=${colour_faded}
  fi

  echo -ne "${icon_colour}\uE0E4${eth_colour}${eth_ip}"
}

# Configure the adapter to display above, strength might need to be tweaked
print_wlan() {
  wlan_status=$(ip addr show dev ${wlan_adapter} | awk 'NR==1 {print $9}')
  
  if [ "$wlan_status" == "UP" ]; then
    wlan_ip=$(ip addr show dev ${wlan_adapter} | awk 'NR==3 {print $2}' | sed -r 's/\/.*//')
    
    let wlan_strength="10 * $(cat /proc/net/wireless | awk 'NR==3 {print substr($3, 1, 2)}') / 7"
    if [ $wlan_strength -lt 25 ]; then
      icon_colour=${colour_critical}
      wlan_colour=${colour_critical}
    elif [ $wlan_strength -lt 50 ]; then
      icon_colour=${colour_warning}
      wlan_colour=${colour_warning}
    else
      icon_colour=${colour_neutral}
      wlan_colour=${colour_normal}
    fi
    
    wlan_strength="${wlan_strength}%"

  else
    icon_colour=${colour_faded}
  fi

  echo -ne "${icon_colour}\uE401${wlan_colour}${wlan_ip} ${wlan_strength}"
}

while true; do
  # get new cpu idle and total usage
  eval $(awk '/^cpu /{print "cpu_idle_now=" $5 "; cpu_total_now=" $2+$3+$4+$5 }' /proc/stat)
  cpu_interval=$((cpu_total_now-${cpu_total_old:-0}))
  # calculate cpu usage (%)
  let cpu_used="100 * ($cpu_interval - ($cpu_idle_now-${cpu_idle_old:-0})) / $cpu_interval"

  # Pipe to status bar
  # This is where you can adjust what gets shown by adding the desired function calls
  xsetroot -name "$(print_wlan)${space}$(print_root_free)${space}$(print_home_free)${space}$(print_cpu_used)${space}$(print_cpu_temp)${space}$(print_mem_free)${space}$(print_battery)${space}$(print_backlight)${space}$(print_volume)${space}$(print_time)$(print_date)"

  # reset old rate
  cpu_idle_old=$cpu_idle_now
  cpu_total_old=$cpu_total_now
  
  # loop stats every so many seconds
  sleep "${refresh}"
done