Copybara bot | ca5ce64 | 2024-11-08 17:38:08 +0100 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | # Copyright (C) 2014 Alexander Keller <github@nycroth.com> |
| 3 | |
| 4 | # This program is free software: you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU General Public License as published by |
| 6 | # the Free Software Foundation, either version 3 of the License, or |
| 7 | # (at your option) any later version. |
| 8 | |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU General Public License for more details. |
| 13 | |
| 14 | # You should have received a copy of the GNU General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | #------------------------------------------------------------------------ |
| 18 | if [[ -z "$INTERFACE" ]] ; then |
| 19 | INTERFACE="${BLOCK_INSTANCE:-wlan0}" |
| 20 | fi |
| 21 | #------------------------------------------------------------------------ |
| 22 | |
| 23 | COLOR_GE80=${COLOR_GE80:-#00FF00} |
| 24 | COLOR_GE60=${COLOR_GE60:-#FFF600} |
| 25 | COLOR_GE40=${COLOR_GE40:-#FFAE00} |
| 26 | COLOR_LOWR=${COLOR_LOWR:-#FF0000} |
| 27 | COLOR_DOWN=${COLOR_DOWN:-#FF0000} |
| 28 | |
| 29 | # As per #36 -- It is transparent: e.g. if the machine has no battery or wireless |
| 30 | # connection (think desktop), the corresponding block should not be displayed. |
| 31 | [[ ! -d /sys/class/net/${INTERFACE}/wireless ]] && exit |
| 32 | |
| 33 | # If the wifi interface exists but no connection is active, "down" shall be displayed. |
| 34 | if [[ "$(cat /sys/class/net/$INTERFACE/operstate)" = 'down' ]]; then |
| 35 | echo "down" |
| 36 | echo "down" |
| 37 | echo $COLOR_DOWN |
| 38 | exit |
| 39 | fi |
| 40 | |
| 41 | #------------------------------------------------------------------------ |
| 42 | |
| 43 | QUALITY=$(iw dev ${INTERFACE} link | grep 'dBm$' | grep -Eoe '-[0-9]{2}' | awk '{print ($1 > -50 ? 100 :($1 < -100 ? 0 : ($1+100)*2))}') |
| 44 | |
| 45 | #------------------------------------------------------------------------ |
| 46 | |
| 47 | echo $QUALITY% # full text |
| 48 | echo $QUALITY% # short text |
| 49 | |
| 50 | # color |
| 51 | if [[ $QUALITY -ge 80 ]]; then |
| 52 | echo $COLOR_GE80 |
| 53 | elif [[ $QUALITY -ge 60 ]]; then |
| 54 | echo $COLOR_GE60 |
| 55 | elif [[ $QUALITY -ge 40 ]]; then |
| 56 | echo $COLOR_GE40 |
| 57 | else |
| 58 | echo $COLOR_LOWR |
| 59 | fi |