Skip to content

Commit

Permalink
Format improvements, Doc update, unit tests!
Browse files Browse the repository at this point in the history
- Added unit tests and suites
- Updated the README
- Whitespace fixes
  • Loading branch information
Travis Goldie committed Jan 14, 2017
1 parent 8019483 commit c755d09
Show file tree
Hide file tree
Showing 8 changed files with 302 additions and 68 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
Tmux plugin to monitor upload and download speed of one or all interfaces.

## Usage

Add one of the following format string to `status-right` tmux option.
NOTE: Currently sums "eth0" and "wlan0" interfaces

## Special Credit
This plugin is roughly based on the various plugins in [https://github.com/tmux-plugins]("tmux-plugins").
Expand All @@ -15,27 +13,29 @@ Shows value in either MB/s, KB/s, or B/s.
- `#{download_speed}` - Shows only download speed,
- `#{upload_speed}` - Shows only upload speed,
- `#{net_speed}` - Shows both the upload and download speeds.
example: "D: 123 MB/s U: 25 MB/s"
**Example**: "D: 123 MB/s U: 25 MB/s"

NOTE: Shows value in either MB/s, KB/s, or B/s.
## Past Values
Since this is a difference, the old values are stored in files in `/tmp/`. The user must be able to
read and write to this directory.

### Set Interfaces
### Set Options

To change which interfaces to pull from, set the following options in your `.tmux.conf`.
Use a commaseperated list.
Use a comma separated list. If not set, grabs all the interfaces listed in "/sys/class/net/"

set -g @net_speed_inf "eth0,eth1"

- set -g @net_speed_inf "eth0,eth1"
To change the formatter sting passed to `printf`. Default is "D:%10s U:%10s"

**DEFAULT**: "eth0,wlan0"
set -g @net_speed_format "Down: %100s"

### Installation with [Tmux Plugin Manager](https://github.com/tmux-plugins/tpm) (recommended)

Add plugin to the list of TPM plugins in `.tmux.conf`:

set -g @plugin 'tmux-plugins/tmux-cpu'
set -g @plugin 'tmux-plugins/tmux-net-speed'

Hit `prefix + I` to fetch the plugin and source it.

Expand All @@ -51,9 +51,8 @@ Add this line to the bottom of `.tmux.conf`:

run-shell ~/clone/path/net_speed.tmux

Reload TMUX environment:
Reload TMUX environment (type this in terminal)

# type this in terminal
$ tmux source-file ~/.tmux.conf

If format strings are added to `status-right`, they should now be visible.
Expand All @@ -64,6 +63,7 @@ This plugin stores the total output for all the interfaces in a file in `/tmp/`.


### TODO
- Add unit tests
- Add error handling
- Configure which interfaces to calculate
- Configure format string for `#{net_speed}`
Expand Down
6 changes: 6 additions & 0 deletions run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash -
CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

for testSuite in $CURRENT_DIR/tests/suites/* ; do
$testSuite
done
1 change: 0 additions & 1 deletion scripts/download_speed.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ main()
local new_val=$(sum_download_speed)

write_file $file $new_val

get_velocity $new_val $old_val
}
main
Expand Down
115 changes: 61 additions & 54 deletions scripts/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ DOWNLOAD_FILE="/tmp/tmux_net_speed.download"
UPLOAD_FILE="/tmp/tmux_net_speed.upload"

get_tmux_option() {
local option="$1"
local default_value="$2"
local option_value="$(tmux show-option -gqv "$option")"
if [[ -z "$option_value" ]]; then
echo "$default_value"
else
echo "$option_value"
fi
local option=$1
local default_value=$2
local option_value="$(tmux show-option -gqv "$option")"

if [[ -z "$option_value" ]]; then
echo "$default_value"
else
echo "$option_value"
fi
}

set_tmux_option() {
local option=$1
local value=$2
tmux set-option -gq "$option" "$value"
local option=$1
local value=$2
tmux set-option -gq "$option" "$value"
}

get_velocity()
Expand All @@ -33,13 +34,13 @@ get_velocity()
local MILLION=1048576

local vel=$(( new_value - old_value ))
local velKB=$(( vel / THOUSAND ))
local velMB=$(( vel / MILLION ))
local vel_kb=$(( vel / THOUSAND ))
local vel_mb=$(( vel / MILLION ))

if [[ $velMB != 0 ]] ; then
echo -n "$velMB MB/s"
elif [[ $velKB != 0 ]] ; then
echo -n "$velKB KB/s";
if [[ $vel_mb != 0 ]] ; then
echo -n "$vel_mb MB/s"
elif [[ $vel_kb != 0 ]] ; then
echo -n "$vel_kb KB/s";
else
echo -n "$vel B/s";
fi
Expand All @@ -49,47 +50,55 @@ get_velocity()
# is empty, or not readable, starts back at 0
read_file()
{
local path="$1"
local val=0

if [[ ! -f "$path" ]] ; then
echo $val
return
elif [[ ! -r "$path" ]]; then
echo $val
return
fi

# Ok, file exists and is readdable. Check contents
tmp=$(< "$path")
if [[ "x${tmp}" == "x" ]] ; then
echo $val
return
fi

# else all good, echo value
echo $tmp
local path="$1"
local fallback_val=0

# File exists and is readdable?
if [[ ! -f "$path" ]] ; then
echo $fallback_val
return 1
elif [[ ! -r "$path" ]]; then
echo $fallback_val
return 1
fi


# Does the file have content?
tmp=$(< "$path")
if [[ "x${tmp}" == "x" ]] ; then
echo $fallback_val
return 1
fi

# Now return known value
echo $tmp
}

# Update values in file
write_file()
{
local path="$1"
local val="$2"
local path="$1"
local val="$2"

# TODO Add error checking
echo "$val" > "$path"
# TODO Add error checking
echo "$val" > "$path"
}

get_interfaces()
{
local interfaces=""
for interface in /sys/class/net/*; do
interfaces+=$(echo $(basename $interface) " ");
done

# Do not quote the variable. This way will handle trailing whitespace
echo -n $interfaces
}

sum_speed()
{
local column=$1

# TODO Make this a parameter option. Set through tmux config
declare -a interfaces=()
for interface in /sys/class/net/*; do
interfaces+=("$(basename $interface)");
done
declare -a interfaces=$(get_interfaces)

local line=""
local val=0
Expand All @@ -98,21 +107,19 @@ sum_speed()
speed="$(echo -n $line | cut -d' ' -f $column)"
let val+=${speed:=0}
done

echo $val
}

is_osx() {
[ $(uname) == "Darwin" ]
[ $(uname) == "Darwin" ]
}

is_cygwin() {
command -v WMIC > /dev/null
command -v WMIC > /dev/null
}

command_exists() {
local command="$1"
type "$command" >/dev/null 2>&1
local command="$1"
type "$command" >/dev/null 2>&1
}


4 changes: 2 additions & 2 deletions scripts/net_speed.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ main()
{
local download=$("$CURRENT_DIR/download_speed.sh")
local upload=$("$CURRENT_DIR/upload_speed.sh")
local format=$(get_tmux_option net_speed_format "D:%10s U:%10s")

# TODO - make the text format configurable
printf "D:%10s U:%10s" "$download" "$upload"
printf "$format" "$download" "$upload"
}
main
1 change: 0 additions & 1 deletion scripts/upload_speed.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ main()
local new_val=$(sum_upload_speed)

write_file $file $new_val

get_velocity $new_val $old_val
}
main
Expand Down
132 changes: 132 additions & 0 deletions tests/suites/helpers.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#!/bin/bash -

CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPTS="$CURRENT_DIR/../../scripts"
source "$CURRENT_DIR/../test_utils.sh"

##
# Description:
# General setup before each test
##
setup()
{
source "$SCRIPTS/helpers.sh"
}

##
# get_tmux_option
##
setup_get_tmux_option()
{
setup

## Set generic property
tmux set @get_tmux_option_name "get_tmux_option_val"
}

cleanup_get_tmux_option()
{
# Unset value
tmux set -u @get_tmux_option_name
}

test_get_tmux_option()
{
##
# Should return set value
##
setup_get_tmux_option

result=$( get_tmux_option "@get_tmux_option_name")
assertEqual "$result" "get_tmux_option_val" "should get tmux prop when given no default" -v

cleanup_get_tmux_option

##
# Should show set value
##
setup_get_tmux_option

result=$( get_tmux_option "@get_tmux_option_name_unset")
assertEqual "$result" "" "should get empty string when tmux prop unset"

cleanup_get_tmux_option

##
# Should show set value
##
setup_get_tmux_option

result=$( get_tmux_option @get_tmux_option_name_unset my_default)
assertEqual "$result" "my_default" "should get default if provided for unset prop"

cleanup_get_tmux_option
}

##
# get_velocity
##
setup_get_velocity()
{
setup
}

cleanup_get_velocity() { :; }

test_get_velocity()
{
setup_get_velocity
let input=200
result=$(get_velocity $input 0)
assertEqual "$result" "200 B/s" "should show B/s"
cleanup_get_velocity

setup_get_velocity
let input=2*1024
result=$(get_velocity $input 0)
assertEqual "$result" "2 KB/s" "should show KB/s"
cleanup_get_velocity

setup_get_velocity
let input=3*1048576
result=$(get_velocity $input 0)
assertEqual "$result" "3 MB/s" "should show MB/s"
cleanup_get_velocity


setup_get_velocity
let subtract_from=100*1048576
let subtract_with=50*1048576
result=$(get_velocity $subtract_from subtract_with)
assertEqual "$result" "50 MB/s" "should show MB/s if subraction done"
cleanup_get_velocity
}

##
# get_interfaces
##
setup_get_interfaces()
{
setup
}

cleanup_get_interfaces() { :; }

test_get_interfaces()
{
setup_get_interfaces
result=$(get_interfaces)
assertEqual "$result" "eth0 lo wlan0" "should output space-delimited list of interfaces" -v
cleanup_get_interfaces
}

suites()
{
#test_get_tmux_option
#test_get_velocity
test_get_interfaces
}

# Run tests
suites

Loading

0 comments on commit c755d09

Please sign in to comment.