clean up code in Network.lua
This commit is contained in:
parent
509f7595ac
commit
84e18d97ba
|
@ -5,18 +5,16 @@ local ScalePlot = require 'ScalePlot'
|
||||||
local util = require 'util'
|
local util = require 'util'
|
||||||
local schema = require 'default_patterns'
|
local schema = require 'default_patterns'
|
||||||
|
|
||||||
local _STRING_GMATCH = string.gmatch
|
local __string_gmatch = string.gmatch
|
||||||
local _IO_POPEN = io.popen
|
|
||||||
|
|
||||||
--construction params
|
local _PLOT_SEC_BREAK_ = 20
|
||||||
local PLOT_SEC_BREAK = 20
|
local _PLOT_HEIGHT_ = 56
|
||||||
local PLOT_HEIGHT = 56
|
|
||||||
|
|
||||||
local SYSFS_NET = '/sys/class/net/'
|
local SYSFS_NET = '/sys/class/net/'
|
||||||
local STATS_RX = '/statistics/rx_bytes'
|
local STATS_RX = '/statistics/rx_bytes'
|
||||||
local STATS_TX = '/statistics/tx_bytes'
|
local STATS_TX = '/statistics/tx_bytes'
|
||||||
|
|
||||||
local __network_label_function = function(bytes)
|
local network_label_function = function(bytes)
|
||||||
local new_unit = util.get_unit(bytes)
|
local new_unit = util.get_unit(bytes)
|
||||||
|
|
||||||
local converted = util.convert_bytes(bytes, 'B', new_unit)
|
local converted = util.convert_bytes(bytes, 'B', new_unit)
|
||||||
|
@ -30,11 +28,10 @@ local header = Widget.Header{
|
||||||
x = _G_INIT_DATA_.CENTER_RIGHT_X,
|
x = _G_INIT_DATA_.CENTER_RIGHT_X,
|
||||||
y = _G_INIT_DATA_.TOP_Y,
|
y = _G_INIT_DATA_.TOP_Y,
|
||||||
width = _G_INIT_DATA_.SECTION_WIDTH,
|
width = _G_INIT_DATA_.SECTION_WIDTH,
|
||||||
header = "NETWORK"
|
header = 'NETWORK'
|
||||||
}
|
}
|
||||||
|
|
||||||
local RIGHT_X = _G_INIT_DATA_.CENTER_RIGHT_X + _G_INIT_DATA_.SECTION_WIDTH
|
local _RIGHT_X_ = _G_INIT_DATA_.CENTER_RIGHT_X + _G_INIT_DATA_.SECTION_WIDTH
|
||||||
local DOWNLOAD_PLOT_Y = header.bottom_y + PLOT_SEC_BREAK
|
|
||||||
|
|
||||||
local dnload = {
|
local dnload = {
|
||||||
label = Widget.Text{
|
label = Widget.Text{
|
||||||
|
@ -43,47 +40,46 @@ local dnload = {
|
||||||
text = 'Download',
|
text = 'Download',
|
||||||
},
|
},
|
||||||
speed = Widget.Text{
|
speed = Widget.Text{
|
||||||
x = RIGHT_X,
|
x = _RIGHT_X_,
|
||||||
y = header.bottom_y,
|
y = header.bottom_y,
|
||||||
x_align = 'right',
|
x_align = 'right',
|
||||||
text_color = schema.blue
|
text_color = schema.blue
|
||||||
},
|
},
|
||||||
plot = Widget.ScalePlot{
|
plot = Widget.ScalePlot{
|
||||||
x = _G_INIT_DATA_.CENTER_RIGHT_X,
|
x = _G_INIT_DATA_.CENTER_RIGHT_X,
|
||||||
y = DOWNLOAD_PLOT_Y,
|
y = header.bottom_y + _PLOT_SEC_BREAK_,
|
||||||
width = _G_INIT_DATA_.SECTION_WIDTH,
|
width = _G_INIT_DATA_.SECTION_WIDTH,
|
||||||
height = PLOT_HEIGHT,
|
height = _PLOT_HEIGHT_,
|
||||||
y_label_func = __network_label_function
|
y_label_func = network_label_function
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
local UPLOAD_Y = DOWNLOAD_PLOT_Y + PLOT_HEIGHT + PLOT_SEC_BREAK
|
local _UPLOAD_Y_ = header.bottom_y + _PLOT_HEIGHT_ + _PLOT_SEC_BREAK_ * 2
|
||||||
local UPLOAD_PLOT_Y = UPLOAD_Y + PLOT_SEC_BREAK
|
|
||||||
|
|
||||||
local upload = {
|
local upload = {
|
||||||
label = Widget.Text{
|
label = Widget.Text{
|
||||||
x = _G_INIT_DATA_.CENTER_RIGHT_X,
|
x = _G_INIT_DATA_.CENTER_RIGHT_X,
|
||||||
y = UPLOAD_Y,
|
y = _UPLOAD_Y_,
|
||||||
text = 'Upload',
|
text = 'Upload',
|
||||||
},
|
},
|
||||||
speed = Widget.Text{
|
speed = Widget.Text{
|
||||||
x = RIGHT_X,
|
x = _RIGHT_X_,
|
||||||
y = UPLOAD_Y,
|
y = _UPLOAD_Y_,
|
||||||
x_align = 'right',
|
x_align = 'right',
|
||||||
text_color = schema.blue
|
text_color = schema.blue
|
||||||
},
|
},
|
||||||
plot = Widget.ScalePlot{
|
plot = Widget.ScalePlot{
|
||||||
x = _G_INIT_DATA_.CENTER_RIGHT_X,
|
x = _G_INIT_DATA_.CENTER_RIGHT_X,
|
||||||
y = UPLOAD_PLOT_Y,
|
y = _UPLOAD_Y_ + _PLOT_SEC_BREAK_,
|
||||||
width = _G_INIT_DATA_.SECTION_WIDTH,
|
width = _G_INIT_DATA_.SECTION_WIDTH,
|
||||||
height = PLOT_HEIGHT,
|
height = _PLOT_HEIGHT_,
|
||||||
y_label_func = __network_label_function
|
y_label_func = network_label_function
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
local interfaces = {}
|
local interfaces = {}
|
||||||
|
|
||||||
local __add_interface = function(iface)
|
local add_interface = function(iface)
|
||||||
local rx_path = SYSFS_NET..iface..STATS_RX
|
local rx_path = SYSFS_NET..iface..STATS_RX
|
||||||
local tx_path = SYSFS_NET..iface..STATS_TX
|
local tx_path = SYSFS_NET..iface..STATS_TX
|
||||||
|
|
||||||
|
@ -97,21 +93,21 @@ local __add_interface = function(iface)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
for iface in _IO_POPEN('ls -1 '..SYSFS_NET):lines() do
|
for iface in io.popen('ls -1 '..SYSFS_NET):lines() do
|
||||||
__add_interface(iface)
|
add_interface(iface)
|
||||||
end
|
end
|
||||||
|
|
||||||
local __update = function(cr, update_frequency)
|
local update = function(cr, update_frequency)
|
||||||
local dspeed, uspeed = 0, 0
|
local dspeed, uspeed = 0, 0
|
||||||
local glob = util.execute_cmd('ip route show')
|
local glob = util.execute_cmd('ip route show')
|
||||||
|
|
||||||
local rx_bps, tx_bps
|
local rx_bps, tx_bps
|
||||||
|
|
||||||
for iface in _STRING_GMATCH(glob, 'default via %d+%.%d+%.%d+%.%d+ dev (%w+) ') do
|
for iface in __string_gmatch(glob, 'default via %d+%.%d+%.%d+%.%d+ dev (%w+) ') do
|
||||||
local current_iface = interfaces[iface]
|
local current_iface = interfaces[iface]
|
||||||
|
|
||||||
if not current_iface then
|
if not current_iface then
|
||||||
__add_interface(iface)
|
add_interface(iface)
|
||||||
current_iface = interfaces[iface]
|
current_iface = interfaces[iface]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -147,15 +143,13 @@ end
|
||||||
|
|
||||||
Widget = nil
|
Widget = nil
|
||||||
schema = nil
|
schema = nil
|
||||||
PLOT_SEC_BREAK = nil
|
_PLOT_SEC_BREAK_ = nil
|
||||||
PLOT_HEIGHT = nil
|
_PLOT_HEIGHT_ = nil
|
||||||
RIGHT_X = nil
|
_RIGHT_X_ = nil
|
||||||
DOWNLOAD_PLOT_Y = nil
|
_UPLOAD_Y_ = nil
|
||||||
UPLOAD_Y = nil
|
|
||||||
UPLOAD_PLOT_Y = nil
|
|
||||||
|
|
||||||
local draw = function(cr, current_interface, update_frequency)
|
local draw = function(cr, current_interface, update_frequency)
|
||||||
__update(cr, update_frequency)
|
update(cr, update_frequency)
|
||||||
|
|
||||||
if current_interface == 0 then
|
if current_interface == 0 then
|
||||||
Text.draw(header.text, cr)
|
Text.draw(header.text, cr)
|
||||||
|
|
Loading…
Reference in New Issue