unixorn/tumult.plugin.zsh

View on GitHub
bin/wifi-signal-strength

Summary

Maintainability
Test Coverage
#!/bin/bash
#
# Use: Put something like this in your .tmux.conf file
# set -g status-right: '#(wifi-signal-strength)'
#
# from: https://github.com/ngs/dotfiles/blob/master/bin/wifi-signal-strength

if [[ "$(uname -s)" != 'Darwin' ]]; then
  echo 'Sorry, this script only works on macOS'
  exit 1
fi

all_signal=(▁ ▃ ▅ ▇ )

# egrep ships on macOS
# shellcheck disable=SC2196
current=$(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | egrep "agrCtlRSSI|state|lastTxRate| SSID" | tr "\\n" ";" | awk '{print $2,$4,$6,$8}')

if [ "$current" == "" ]
then
  echo "(WIFI off)"
fi

# shellcheck disable=SC2086
strength=$(echo $current | cut -d";" -f1-1 | cut -d" " -f2-)
# shellcheck disable=SC2086
state=$(echo $current | cut -d";" -f2-2 | cut -d" " -f2-)
# shellcheck disable=SC2086
bandwidth=$(echo $current | cut -d";" -f3-3 | cut -d" " -f2-)
# shellcheck disable=SC2086
name=$(echo $current | cut -d";" -f4-4 | cut -d" " -f2-)

if [ "$state" != "running" ]
then
  echo "(WIFI off)"
fi

signal="( "
for (( j = 0; j < 4; j++ ))
do
  if [[ $j -eq 0 && $strength -gt -94 ]] || [[ $j -eq 1 && $strength -gt -92 ]] ||
     [[ $j -eq 2 && $strength -gt -86 ]] || [[ $j -eq 3 && $strength -gt -79 ]]
  then
    signal="${signal}${all_signal[$j]} "
  else
    signal="${signal}  "
  fi
done
signal="${signal} )"

echo "${name} ${bandwidth}Mbs ${signal}"