Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add macOS support #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions scripts/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if statement is always true because -n is used to determine if the length of a string is zero.

The correct statement should be if is_osx.

I did a quick fix sainnhe@2e94ad2, you can merge my commit if you would like to.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried if [[ is_osx ]] and it works well!

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
Expand All @@ -108,14 +113,29 @@ 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

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" ]
}
Expand Down
7 changes: 6 additions & 1 deletion tests/suites/helpers.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down