aboutsummaryrefslogtreecommitdiff
path: root/.config/luastatus-scripts-dwm/wireless.lua
blob: 34300ad034a6b4623b12265bab5f9793fffb0a33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
local MIN_DBM, MAX_DBM = -90, -20
local NGAUGE = 5
local COLOR_DIM = '#709080'

local function round(x)
    return math.floor(x + 0.5)
end

local function make_wifi_gauge(dbm)
    if dbm < MIN_DBM then dbm = MIN_DBM end
    if dbm > MAX_DBM then dbm = MAX_DBM end
    local nbright = round(NGAUGE * (1 - 0.7 * (MAX_DBM - dbm) / (MAX_DBM - MIN_DBM)))

    if nbright == 0 then
      return "󰤭"
    elseif nbright == 1 then
      return "󰤯"
    elseif nbright == 2 then
      return "󰤟"
    elseif nbright == 3 then
      return "󰤢"
    elseif nbright == 4 then
      return "󰤥"
    else
      return "󰤨"
    end
end

widget = {
    plugin = 'network-linux',
    opts = {
        wireless = true,
        timeout = 10,
    },
    cb = function(t)
        if not t then
            return nil
        end
        local r = {}
        for iface, params in pairs(t) do
            if params.wireless then
                local x = ""
                if params.wireless.signal_dbm then
                    x = x .. make_wifi_gauge(params.wireless.signal_dbm) .. "  "
                end
                if params.wireless.ssid then
                    x = x .. params.wireless.ssid
                end
                return x
            elseif iface ~= 'lo' and (params.ipv4 or params.ipv6) then
                r[#r + 1] = string.format('[%s]', iface)
            end
        end
        return r
    end,
}