From 8c49c70ee2e2d5465a7de12c013a48e0e01f97c2 Mon Sep 17 00:00:00 2001 From: Miroslav Zoricak Date: Tue, 26 Nov 2019 20:58:56 +0100 Subject: [PATCH] Add macOS support --- scripts/helpers.sh | 28 ++++++++++++++++++++++++---- tests/suites/helpers.test.sh | 7 ++++++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/scripts/helpers.sh b/scripts/helpers.sh index 6877eac..bdf5a4f 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -90,9 +90,14 @@ get_interfaces() local interfaces=$(get_tmux_option @net_speed_interfaces "") if [[ -z "$interfaces" ]] ; then - for interface in /sys/class/net/*; do - interfaces+=$(echo $(basename $interface) " "); - done + if [[ -n is_osx ]] + then + interfaces=$(netstat -i | tail +2 | sed -E 's/ +/ /g' | cut -d ' ' -f 1 | sort -u | grep "en") + else + for interface in /sys/class/net/*; do + interfaces+=$(echo $(basename $interface) " "); + done + fi fi # Do not quote the variable. This way will handle trailing whitespace @@ -108,7 +113,7 @@ sum_speed() local line="" local val=0 for intf in ${interfaces[@]} ; do - line=$(cat /proc/net/dev | grep "$intf" | cut -d':' -f 2) + line=$(get_speed $intf) speed="$(echo -n $line | cut -d' ' -f $column)" let val+=${speed:=0} done @@ -116,6 +121,21 @@ sum_speed() echo $val } +get_speed() +{ + local interface=$1 + + if [[ -n is_osx ]] + then + line="$(netstat -b -i -I "$interface" | tail +2 | sed -E 's/ +/ /g' | cut -d ' ' -f 1,7,10 | uniq )" + down=$(echo -n $line | cut -d ' ' -f 2) + up=$(echo -n $line | cut -d ' ' -f 3) + echo "$down 0 0 0 0 0 0 0 $up" + else + echo $(cat /proc/net/dev | grep "$interface" | cut -d':' -f 2) + fi +} + is_osx() { [ $(uname) == "Darwin" ] } diff --git a/tests/suites/helpers.test.sh b/tests/suites/helpers.test.sh index a68bbb1..e430758 100755 --- a/tests/suites/helpers.test.sh +++ b/tests/suites/helpers.test.sh @@ -116,7 +116,12 @@ test_get_interfaces() { setup_get_interfaces result=$(get_interfaces) - assertEqual "$result" "eth0 lo wlan0" "should output space-delimited list of interfaces" -v + if [[ -n is_osx ]] + then + assertEqual "$result" "en0 en1 en2 en3 en4 en5 en7" "should output space-delimited list of interfaces" -v + else + assertEqual "$result" "eth0 lo wlan0" "should output space-delimited list of interfaces" -v + fi cleanup_get_interfaces }