1
0
mirror of https://github.com/dcarrillo/dotfiles.git synced 2024-12-22 18:38:00 +00:00

[polybar] Add custom network usage module

This commit is contained in:
Daniel Carrillo 2020-03-22 19:32:43 +01:00
parent c132945659
commit 0441ed825e
3 changed files with 68 additions and 4 deletions

View File

@ -67,7 +67,7 @@ font-4 = NotoSans-Regular:size=18:weight=bold;-7
modules-left = spotify
modules-center = custom_date
modules-right = updates cpu_bar memory_bar vpn wireless wired alsa_bar
modules-right = updates cpu_bar memory_bar vpn network_usage alsa_bar
cursor-click = pointer
cursor-scroll = ns-resize

View File

@ -80,14 +80,19 @@ label-connected = "%{A1:/usr/bin/bash ~/.config/polybar/scripts/nmcli_manager:}%
# keep speed meter for other devices
label-disconnected = "%{A1:/usr/bin/bash ~/.config/polybar/scripts/nmcli_manager:}%downspeed:8%  %upspeed:8% %{A-}"
[module/vpn]
[module/network_usage]
type = custom/script
exec = ~/.config/polybar/scripts/network_usage
click-left = /usr/bin/bash ~/.config/polybar/scripts/nmcli_manager &
double-click-left = /usr/bin/bash ~/.config/polybar/scripts/nmcli_manager &
tail = true
[module/vpn]
type = custom/script
exec = ~/.config/polybar/scripts/check_vpn
interval = 5
[module/spotify]
type = custom/script
exec = ~/.config/polybar/scripts/spotify.py
interval = 5
@ -107,7 +112,6 @@ time-alt = %H:%M:%S
label = %{F#f5a70a}%{T4}%time%%{F-}%{T-} | %date%
[module/custom_date]
type = custom/script
exec = ~/.config/polybar/scripts/custom_date
interval = 1

View File

@ -0,0 +1,60 @@
#!/usr/bin/env bash
COUNTER_FILE=/dev/shm/polybar_network_usage
REFRESH=2
get_icon_by_device()
{
device=$1
device_type=$(nmcli -t device status | grep -E -m 1 "^$device" | cut -f 2 -d ":")
case "$device_type" in
ethernet)
icon=""
;;
wifi)
ssid=$(nmcli -t device status | grep -E -m 1 "^$device" | cut -f 4 -d ":")
icon=" $ssid"
;;
*)
icon=""
;;
esac
echo $icon
}
while true; do
default_device=$(ip route list | grep -Fm 1 default | cut -d " " -f 5)
icon=$(get_icon_by_device "$default_device")
now=$(date +%s)
counter_age=$(stat --format %Z $COUNTER_FILE 2>/dev/null)
if [ -f $COUNTER_FILE ] && [ $((now-counter_age)) -lt $((REFRESH+1)) ]; then
last_value_in=$(cut -f 1 -d " " $COUNTER_FILE)
last_value_out=$(cut -f 2 -d " " $COUNTER_FILE)
fi
current_values=$(awk -v dev="${default_device}:" '{
if ($1 == dev) print $2,$10
}' < /proc/net/dev)
current_bytes_in=$(echo "$current_values" | cut -f 1 -d " ")
current_bytes_out=$(echo "$current_values" | cut -f 2 -d " ")
if [ -n "$last_value_in" ]; then
bits_in=$((((current_bytes_in-last_value_in) / REFRESH) * 8))
bits_out=$((((current_bytes_out-last_value_out) / REFRESH) * 8))
output=$(numfmt --to iec --format "%8.2f" -z "$bits_in" "$bits_out" 2>/dev/null)
echo "$icon $output"
else
echo "$icon -- --"
fi
echo "$current_values">$COUNTER_FILE
unset last_value
sleep $REFRESH
done