Skip to content

Commit

Permalink
Add Linux support
Browse files Browse the repository at this point in the history
  • Loading branch information
xamut committed Oct 18, 2019
1 parent ddc0c52 commit 666b558
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
20 changes: 20 additions & 0 deletions scripts/helpers.sh
Expand Up @@ -20,3 +20,23 @@ set_tmux_option() {
$(tmux set-option -gq $option_name "$option_value")
}

os_type() {
local os_name="unknown"

case $(uname | tr '[:upper:]' '[:lower:]') in
linux*)
os_name="linux"
;;
darwin*)
os_name="osx"
;;
msys*)
os_name="windows"
;;
freebsd*)
os_name="freebsd"
;;
esac

echo -n $os_name
}
44 changes: 38 additions & 6 deletions scripts/network-bandwidth.sh
Expand Up @@ -3,7 +3,8 @@
CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$CURRENT_DIR/helpers.sh"

get_bandwidth() {

get_bandwidth_for_osx() {
netstat -ibn | awk 'FNR > 1 {
interfaces[$1 ":bytesReceived"] = $(NF-4);
interfaces[$1 ":bytesSent"] = $(NF-1);
Expand All @@ -19,24 +20,55 @@ get_bandwidth() {
}'
}

get_bandwidth_for_linux() {
netstat -ie | awk '$0 ~ /(RX|TX) packets/ {
if ($1 == "RX") {
sum["bytesReceived"] += $5
} else {
sum["bytesSent"] += $5
}
} END {
print sum["bytesReceived"], sum["bytesSent"]
}'
}

get_bandwidth() {
local os="$1"

case $os in
osx)
echo -n $(get_bandwidth_for_osx)
return 0
;;
linux)
echo -n $(get_bandwidth_for_linux)
return 0
;;
*)
echo -n "0 0"
return 1
;;
esac
}

format_speed() {
local str=`numfmt -z --to=iec-i --suffix "B/s" --format "%.2f" $1`
local str=`numfmt --to=iec-i --suffix "B/s" --format "%.2f" $1`
echo -n ${str/".00"/""}
}

main() {
local sleep_time=$(get_tmux_option "status-interval")
local old_value=$(get_tmux_option "@network-bandwidth-previous-value")

if [ -z "$old_value"]
then
if [ -z "$old_value" ]; then
$(set_tmux_option "@network-bandwidth-previous-value" "-")
echo -n "Please wait..."
return 0
else
local first_measure=( $(get_bandwidth) )
local os=$(os_type)
local first_measure=( $(get_bandwidth $os) )
sleep $sleep_time
local second_measure=( $(get_bandwidth) )
local second_measure=( $(get_bandwidth $os) )
local download_speed=$(((${second_measure[0]} - ${first_measure[0]}) / $sleep_time))
local upload_speed=$(((${second_measure[1]} - ${first_measure[1]}) / $sleep_time))
$(set_tmux_option "@network-bandwidth-previous-value" "$(format_speed $download_speed) • ↑$(format_speed $upload_speed)")
Expand Down

0 comments on commit 666b558

Please sign in to comment.