-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifi-status.lua
103 lines (96 loc) · 4.13 KB
/
wifi-status.lua
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
local config_parser = require "parse-config"
local util = require "util.util"
local wezterm = require "wezterm"
local wifi_status = {}
function update_json(config)
local output = {
timestamp = nil,
interfaces = {}
}
needs_update = false
number, multiplier = util.get_number_and_multiplier(config["status_bar"]["wifi_status"]["interval"])
exists, err = util.file_exists(config["status_bar"]["wifi_status"]["data_file"])
if exists then
json_data = util.json_parse(config["status_bar"]["wifi_status"]["data_file"])
if json_data ~= nil then
if (util.get_timestamp() - json_data["timestamp"]) > (number * multiplier) then
needs_update = true
end
end
else
needs_update = true
end
if needs_update then
if config["os_name"] == "darwin" then
network_interface_list = config["status_bar"]["system_status"]["network_interface_list"]
success, stdout, stderr = wezterm.run_child_process({"/usr/sbin/system_profiler", "SPAirPortDataType", "-json", "-detailLevel", "basic"})
if success then
data = util.json_parse_string(stdout)
if data ~= nil then
interfaces = data["SPAirPortDataType"][1]["spairport_airport_interfaces"]
for _, interface in ipairs(interfaces) do
ifname = interface["_name"]
if util.has_value(network_interface_list, ifname) then
spairport_signal_noise = interface["spairport_current_network_information"]["spairport_signal_noise"]
signal_level = spairport_signal_noise:match("(-%d+) dBm")
if signal_level ~= nil then
output["interfaces"][ifname] = tonumber(signal_level)
end
end
end
end
end
elseif config["os_name"] == "linux" then
network_interface_list = config["status_bar"]["system_status"]["network_interface_list"]
if network_interface_list ~= nil then
for _, ifname in ipairs(network_interface_list) do
success, stdout, stderr = wezterm.run_child_process({"iwconfig", ifname})
if success then
signal_level = stdout:match("Signal level=-(-%d+) dBm")
if signal_level ~= nil then
output["interfaces"][ifname] = tonumber(signal_level)
end
end
end
end
end
output["timestamp"] = util.get_timestamp()
file = io.open(config["status_bar"]["wifi_status"]["data_file"], "w")
file:write(wezterm.json_encode(output))
file:close()
end
end
function get_wifi_status(config)
wifi_status = {}
exists, err = util.file_exists(config["status_bar"]["wifi_status"]["data_file"])
if exists then
json_data = util.json_parse(config["status_bar"]["wifi_status"]["data_file"])
if json_data ~= nil then
for interface, signal in pairs(json_data["interfaces"]) do
signal = tonumber(signal)
interface_data = util.pad_string(2, 2, string.format("%s %s %s dBm", get_icon(signal), interface, signal))
table.insert(wifi_status, interface_data)
end
end
return wifi_status
end
return nil
end
function get_icon(signal)
if signal >= -30 then
return wezterm.nerdfonts.md_wifi_strength_4
elseif signal >= -50 then
return wezterm.nerdfonts.md_wifi_strength_3
elseif signal >= -60 then
return wezterm.nerdfonts.md_wifi_strength_2
elseif signal >= -70 then
return wezterm.nerdfonts.md_wifi_strength_1
elseif signal >= -80 then
return wezterm.nerdfonts.md_wifi_strength_outline
elseif signal >= -90 then
return wezterm.nerdfonts.md_wifi_strength_alert_outline
end
end
wifi_status.get_wifi_status = get_wifi_status
wifi_status.update_json = update_json
return wifi_status