feat: add network name support

This commit is contained in:
Abhishek Keshri 2023-03-29 14:52:04 +05:30
parent 3837498239
commit 54ecf3dc0b
No known key found for this signature in database
GPG key ID: 62BD6AA2E913B3D3

View file

@ -1,23 +1,28 @@
#!/usr/bin/env bash #!/bin/bash
INTERVAL="1" # update interval in seconds INTERVAL="1" # update interval in seconds
# Network interface to monitor
current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$current_dir"/utils.sh
# Network interface to monitor # Network interface to monitor
network_name="en0" network_name="en0"
if [[ $(uname -s) == "Darwin" ]]; then if [[ $(uname -s) == "Darwin" ]]; then
network_name="en0" # en0 is wifi, TODO: Update network_name network_name=$(get_tmux_option "@tmux2k-network-name" "en0")
elif [[ $(uname -s) == "Linux" ]]; then elif [[ $(uname -s) == "Linux" ]]; then
network_name=$(tmux show-option -gqv "@dracula-network-bandwidth") network_name=$(get_tmux_option "@tmux2k-network-name" "wlo1")
else else
# update this later for window # TODO: update this for windows
echo "Unknown operating system" echo "Unknown operating system"
exit 1 exit 1
fi fi
network_name=$(get_tmux_option "@tmux2k-network-name" "en0")
main() { main() {
while true while true; do
do
output_download="" output_download=""
output_upload="" output_upload=""
output_download_unit="" output_download_unit=""
@ -41,13 +46,13 @@ main() {
final_upload=$(netstat -I "$network_name" -b | tail -n 1 | awk '{print $10}') final_upload=$(netstat -I "$network_name" -b | tail -n 1 | awk '{print $10}')
fi fi
total_download_bps=$(expr $final_download - $initial_download) total_download_bps=$(expr "$final_download" - "$initial_download")
total_upload_bps=$(expr $final_upload - $initial_upload) total_upload_bps=$(expr "$final_upload" - "$initial_upload")
if [ $total_download_bps -gt 1073741824 ]; then if [ "$total_download_bps" -gt 1073741824 ]; then
output_download=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2 * $2)}') output_download=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2 * $2)}')
output_download_unit="gB/s" output_download_unit="gB/s"
elif [ $total_download_bps -gt 1048576 ]; then elif [ "$total_download_bps" -gt 1048576 ]; then
output_download=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2)}') output_download=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2)}')
output_download_unit="mB/s" output_download_unit="mB/s"
else else
@ -55,10 +60,10 @@ main() {
output_download_unit="kB/s" output_download_unit="kB/s"
fi fi
if [ $total_upload_bps -gt 1073741824 ]; then if [ "$total_upload_bps" -gt 1073741824 ]; then
output_upload=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2 * $2)}') output_upload=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2 * $2)}')
output_upload_unit="gB/s" output_upload_unit="gB/s"
elif [ $total_upload_bps -gt 1048576 ]; then elif [ "$total_upload_bps" -gt 1048576 ]; then
output_upload=$(echo "$total_upload_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2)}') output_upload=$(echo "$total_upload_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2)}')
output_upload_unit="mB/s" output_upload_unit="mB/s"
else else