Debian Codegraph @

vicious/widgets/net_freebsd.lua

raw symbols SCIP
1 -- network status and usage widget type for FreeBSD
2 -- Copyright (C) 2017,2019 mutlusun <[email protected]>
3 -- Copyright (C) 2019 Nguyễn Gia Phong <[email protected]m>
4 --
5 -- This file is part of Vicious.
6 --
7 -- Vicious is free software: you can redistribute it and/or modify
8 -- it under the terms of the GNU General Public License as
9 -- published by the Free Software Foundation, either version 2 of the
10 -- License, or (at your option) any later version.
11 --
12 -- Vicious is distributed in the hope that it will be useful,
13 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
14 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 -- GNU General Public License for more details.
16 --
17 -- You should have received a copy of the GNU General Public License
18 -- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
19
20 -- {{{ Grab environment
21 local tonumber = tonumber
22 local os = { time = os.time }
23 local string = {
24 match = string.match,
25 gmatch = string.gmatch
26 }
27
28 local helpers = require("vicious.helpers")
29 local spawn = require("vicious.spawn")
30 -- }}}
31
32
33 -- Net: provides state and usage statistics of all network interfaces
34 -- vicious.widgets.net
35 local net_freebsd = {}
36
37
38 -- Initialize function tables
39 local nets = {}
40 -- Variable definitions
41 local unit = { ["b"] = 1, ["kb"] = 1024,
42 ["mb"] = 1024^2, ["gb"] = 1024^3
43 }
44
45 -- {{{ Net widget type
46 local function parse(stdout, stderr, exitreason, exitcode)
47
48 local args = {}
49 local buffer = nil
50 local now = os.time()
51
52 for line in string.gmatch(stdout, "[^\n]+") do
53 if not (line:find("<Link") or line:find("Name")) then -- skipping missleading lines
54 local split = { line:match(("([^%s]*)%s*"):rep(12)) }
55
56 if buffer == nil then
57 buffer = { tonumber(split[8]), tonumber(split[11]) } -- recv (field 8) and send (field 11)
58 else
59 buffer = { buffer[1] + tonumber(split[8]),
60 buffer[2] + tonumber(split[11]) }
61 end
62 end
63 end
64
65 if buffer == nil then
66 args["{carrier}"] = 0
67 helpers.uformat(args, "rx", 0, unit)
68 helpers.uformat(args, "tx", 0, unit)
69 helpers.uformat(args, "down", 0, unit)
70 helpers.uformat(args, "up", 0, unit)
71 else
72 args["{carrier}"] = 1
73 helpers.uformat(args, "rx", buffer[1], unit)
74 helpers.uformat(args, "tx", buffer[2], unit)
75
76 if next(nets) == nil then
77 helpers.uformat(args, "down", 0, unit)
78 helpers.uformat(args, "up", 0, unit)
79 else
80 local interval = now - nets["time"]
81 if interval <= 0 then interval = 1 end
82
83 local down = (buffer[1] - nets[1]) / interval
84 local up = (buffer[2] - nets[2]) / interval
85
86 helpers.uformat(args, "down", down, unit)
87 helpers.uformat(args, "up", up, unit)
88 end
89
90 nets["time"] = now
91
92 -- Store totals
93 nets[1] = buffer[1]
94 nets[2] = buffer[2]
95 end
96
97 return args
98
99 end
100
101 function net_freebsd.async(format, warg, callback)
102 if not warg then return callback{} end
103 spawn.easy_async("netstat -n -b -I " .. helpers.shellquote(warg),
104 function (...) callback(parse(...)) end)
105 end
106 -- }}}
107
108 return helpers.setasyncall(net_freebsd)