blob: 258ac8cedce98de69ce5b01ccbc9387a69213879 [file] [log] [blame]
Copybara botca5ce642024-11-08 17:38:08 +01001#!/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#------------------------------------------------------------------------
18if [[ -z "$INTERFACE" ]] ; then
19 INTERFACE="${BLOCK_INSTANCE:-wlan0}"
20fi
21#------------------------------------------------------------------------
22
23COLOR_GE80=${COLOR_GE80:-#00FF00}
24COLOR_GE60=${COLOR_GE60:-#FFF600}
25COLOR_GE40=${COLOR_GE40:-#FFAE00}
26COLOR_LOWR=${COLOR_LOWR:-#FF0000}
27COLOR_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.
34if [[ "$(cat /sys/class/net/$INTERFACE/operstate)" = 'down' ]]; then
35 echo "down"
36 echo "down"
37 echo $COLOR_DOWN
38 exit
39fi
40
41#------------------------------------------------------------------------
42
43QUALITY=$(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
47echo $QUALITY% # full text
48echo $QUALITY% # short text
49
50# color
51if [[ $QUALITY -ge 80 ]]; then
52 echo $COLOR_GE80
53elif [[ $QUALITY -ge 60 ]]; then
54 echo $COLOR_GE60
55elif [[ $QUALITY -ge 40 ]]; then
56 echo $COLOR_GE40
57else
58 echo $COLOR_LOWR
59fi