feat(network_bandwidth): add more configuration option

- Can auto detect used interface (only with linux ip for now)
- Can show the name of used interface
- A waiting interval can be set between each update

Signed-off-by: Adrien Kara <adrien@iglou.eu>
This commit is contained in:
Adrien Kara 2023-01-06 19:46:04 +01:00
parent ac95b3e069
commit 4bb5e56053
No known key found for this signature in database
GPG key ID: 605B69551C56DB62
2 changed files with 52 additions and 6 deletions

View file

@ -152,6 +152,18 @@ Customize label
set -g @dracula-ram-usage-label "RAM" set -g @dracula-ram-usage-label "RAM"
``` ```
#### network-bandwidth
You can configure which network interface you want to view the bandwidth,
Displaying of the interface name, The interval between each bandwidth update.
The most common interfaces name are `eth0` for a wired connection and `wlan0` for a wireless connection.
```bash
set -g @dracula-network-bandwidth eth0
set -g @dracula-network-bandwidth-interval 0
set -g @dracula-network-bandwidth-show-interface true
```
#### network-ping options #### network-ping options
You can configure which server (hostname, IP) you want to ping and at which rate (in seconds). Default is google.com at every 5 seconds. You can configure which server (hostname, IP) you want to ping and at which rate (in seconds). Default is google.com at every 5 seconds.

View file

@ -15,7 +15,22 @@ readonly SIZE=(
[1073741824]='GB/s' [1073741824]='GB/s'
) )
network_name=$(tmux show-option -gqv "@dracula-network-bandwidth") # interface_get try to automaticaly get the used interface if network_name is empty
interface_get() {
name="$(tmux show-option -gqv "@dracula-network-bandwidth")"
if [[ -z $name ]]; then
case "$(uname -s)" in
Linux)
if type ip >/dev/null; then
name="$(ip -o route get 192.168.0.0 | awk '{print $5}')"
fi
;;
esac
fi
echo "$name"
}
# interface_bytes give interface name and signal tx/rx return Bytes # interface_bytes give interface name and signal tx/rx return Bytes
interface_bytes() { interface_bytes() {
@ -24,14 +39,14 @@ interface_bytes() {
# get_bandwidth return the number of bytes exchanged for tx and rx # get_bandwidth return the number of bytes exchanged for tx and rx
get_bandwidth() { get_bandwidth() {
upload="$(interface_bytes "$network_name" "tx")" upload="$(interface_bytes "$1" "tx")"
download="$(interface_bytes "$network_name" "rx")" download="$(interface_bytes "$1" "rx")"
#wait the interval for Wait for interval to calculate the difference #wait the interval for Wait for interval to calculate the difference
sleep "$INTERVAL" sleep "$INTERVAL"
upload="$(bc <<<"$(interface_bytes "$network_name" "tx") - $upload")" upload="$(bc <<<"$(interface_bytes "$1" "tx") - $upload")"
download="$(bc <<<"$(interface_bytes "$network_name" "rx") - $download")" download="$(bc <<<"$(interface_bytes "$1" "rx") - $download")"
#set to 0 by default useful for non-existent interface #set to 0 by default useful for non-existent interface
echo "${upload:-0} ${download:-0}" echo "${upload:-0} ${download:-0}"
@ -57,11 +72,30 @@ bandwidth_to_unit() {
} }
main() { main() {
counter=0
bandwidth=() bandwidth=()
network_name=""
show_interface="$(tmux show-option -gqv "@dracula-network-bandwidth-show-interface")"
interval_update="$(tmux show-option -gqv "@dracula-network-bandwidth-interval")"
if [[ -z $interval_update ]]; then
interval_update=0
fi
while true; do while true; do
IFS=" " read -ra bandwidth <<<"$(get_bandwidth)" if ((counter == 0)); then
counter=60
network_name="$(interface_get)"
fi
IFS=" " read -ra bandwidth <<<"$(get_bandwidth "$network_name")"
if [[ $show_interface == "true" ]]; then echo -n "[$network_name] "; fi
echo "$(bandwidth_to_unit "${bandwidth[$DOWNLOAD]}") • ↑ $(bandwidth_to_unit "${bandwidth[$UPLOAD]}")" echo "$(bandwidth_to_unit "${bandwidth[$DOWNLOAD]}") • ↑ $(bandwidth_to_unit "${bandwidth[$UPLOAD]}")"
((counter = counter - 1))
sleep "$interval_update"
done done
} }