speedwm-personal/modules/module_net
2022-11-01 20:38:29 +01:00

93 lines
2.3 KiB
Bash
Executable file

#!/bin/sh
# network module for status/stellar
# load config
. $HOME/.config/speedwm/statusrc
BINDIR="$(dirname $(command -v speedwm_status))/"
ARG1="$1"
# write
if [ ! -e "/tmp/module_net_tx" ]; then
R1="$(cat /sys/class/net/[ew]*/statistics/rx_bytes)"
T1="$(cat /sys/class/net/[ew]*/statistics/tx_bytes)"
sleep 0.1
R2="$(cat /sys/class/net/[ew]*/statistics/rx_bytes)"
T2="$(cat /sys/class/net/[ew]*/statistics/tx_bytes)"
TXPMS="$(expr $T2 - $T1)"
RXPMS="$(expr $R2 - $R1)"
TX="$(expr $TXPMS / 1024 "*" 10)"
RX="$(expr $RXPMS / 1024 "*" 10)"
printf "$TX" > /tmp/module_net_tx
printf "$RX" > /tmp/module_net_rx
fi
# click
CLICK() {
# values
NETWORK_TRANSMITTED="$(cat /tmp/module_net_tx) kb/s"
NETWORK_RECEIVED="$(cat /tmp/module_net_rx) kb/s"
ICON="$ITEM6_ICON"
BUTTON="$(cat /tmp/speedwm-button)"
case "$TERMINAL" in
"") TERMINAL=st
esac
case "$BUTTON" in
"3") $TERMINAL -e bmon ; exit ;;
esac
# no spacing if there's no icon
if [ "$ICON" != "" ]; then
ICONSPACING=" " # one character spacing
fi
# send the notification
SEND_NOTIF() {
FULL_NETWORK="${ICON}${ICONSPACING}${NETWORK_TRANSMITTED} transmitted, ${NETWORK_RECEIVED} received."
notify-send "$FULL_NETWORK"
echo "Notification sent successfully!"
echo "INFO: $FULL_NETWORK"
exit 0
}
command -v notify-send > /dev/null && SEND_NOTIF
echo "FATAL: libnotify not installed, can't send notification."
exit 1
}
# information itself
PRINT() {
if [ "$ENABLE_ITEM6" = "true" ]; then
if [ -e "${BINDIR}awk" ]; then
NETWORK_TRANSMITTED="$(cat /tmp/module_net_tx)"
NETWORK_RECEIVED="$(cat /tmp/module_net_rx)"
echo "$SEPARATOR $ITEM6_ICON $ITEM6_FORMAT" | sed "s|@t|$NETWORK_TRANSMITTED|g; s|@r|$NETWORK_RECEIVED|g"
fi
fi
}
# argument 1
case "$ARG1" in
"") CLICK ;;
"--print") PRINT > /tmp/module_net ;;
"--click") CLICK ;;
esac
R1="$(cat /sys/class/net/[ew]*/statistics/rx_bytes)"
T1="$(cat /sys/class/net/[ew]*/statistics/tx_bytes)"
sleep 0.1
R2="$(cat /sys/class/net/[ew]*/statistics/rx_bytes)"
T2="$(cat /sys/class/net/[ew]*/statistics/tx_bytes)"
TXPMS="$(expr $T2 - $T1)"
RXPMS="$(expr $R2 - $R1)"
TX="$(expr $TXPMS / 1024 "*" 10)"
RX="$(expr $RXPMS / 1024 "*" 10)"
printf "$TX" > /tmp/module_net_tx
printf "$RX" > /tmp/module_net_rx