2022-06-11 16:21:14 +00:00
|
|
|
-- Data Indicator Graphics Element
|
|
|
|
|
|
|
|
local util = require("scada-common.util")
|
|
|
|
|
|
|
|
local element = require("graphics.element")
|
|
|
|
|
2022-06-18 06:14:48 +00:00
|
|
|
-- format a number string with commas as the thousands separator
|
|
|
|
--
|
|
|
|
-- subtracts from spaces at the start if present for each comma used
|
|
|
|
---@param num string number string
|
|
|
|
---@return string
|
|
|
|
local function comma_format(num)
|
|
|
|
local formatted = num
|
|
|
|
local commas = 0
|
|
|
|
local i = 1
|
|
|
|
|
|
|
|
while i > 0 do
|
|
|
|
formatted, i = formatted:gsub("^(%s-%d+)(%d%d%d)", '%1,%2')
|
|
|
|
if i > 0 then commas = commas + 1 end
|
|
|
|
end
|
|
|
|
|
|
|
|
local _, num_spaces = formatted:gsub(" %s-", "")
|
|
|
|
local remove = math.min(num_spaces, commas)
|
|
|
|
|
|
|
|
formatted = string.sub(formatted, remove + 1)
|
|
|
|
|
|
|
|
return formatted
|
|
|
|
end
|
|
|
|
|
2022-06-11 16:21:14 +00:00
|
|
|
---@class data_indicator_args
|
|
|
|
---@field label string indicator label
|
|
|
|
---@field unit? string indicator unit
|
|
|
|
---@field format string data format (lua string format)
|
2022-06-18 06:14:48 +00:00
|
|
|
---@field commas boolean whether to use commas if a number is given (default to false)
|
2022-06-16 15:19:32 +00:00
|
|
|
---@field lu_colors? cpair label foreground color (a), unit foreground color (b)
|
|
|
|
---@field value any default value
|
2022-06-11 16:21:14 +00:00
|
|
|
---@field parent graphics_element
|
2022-07-28 14:09:34 +00:00
|
|
|
---@field id? string element id
|
2022-06-11 16:21:14 +00:00
|
|
|
---@field x? integer 1 if omitted
|
|
|
|
---@field y? integer 1 if omitted
|
|
|
|
---@field width integer length
|
2022-06-11 21:06:32 +00:00
|
|
|
---@field fg_bg? cpair foreground/background colors
|
2022-06-11 16:21:14 +00:00
|
|
|
|
|
|
|
-- new data indicator
|
|
|
|
---@param args data_indicator_args
|
2022-07-28 14:09:34 +00:00
|
|
|
---@return graphics_element element, element_id id
|
2022-06-16 15:19:32 +00:00
|
|
|
local function data(args)
|
|
|
|
assert(type(args.label) == "string", "graphics.elements.indicators.data: label is a required field")
|
|
|
|
assert(type(args.format) == "string", "graphics.elements.indicators.data: format is a required field")
|
|
|
|
assert(args.value ~= nil, "graphics.elements.indicators.data: value is a required field")
|
|
|
|
assert(util.is_int(args.width), "graphics.elements.indicators.data: width is a required field")
|
|
|
|
|
|
|
|
-- single line
|
|
|
|
args.height = 1
|
2022-06-11 21:06:32 +00:00
|
|
|
|
2022-06-11 16:21:14 +00:00
|
|
|
-- create new graphics element base object
|
|
|
|
local e = element.new(args)
|
|
|
|
|
|
|
|
-- label color
|
2022-06-16 15:19:32 +00:00
|
|
|
if args.lu_colors ~= nil then
|
|
|
|
e.window.setTextColor(args.lu_colors.color_a)
|
2022-06-11 16:21:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- write label
|
2022-06-11 21:06:32 +00:00
|
|
|
e.window.setCursorPos(1, 1)
|
2022-06-11 16:21:14 +00:00
|
|
|
e.window.write(args.label)
|
|
|
|
|
|
|
|
local data_start = string.len(args.label) + 2
|
|
|
|
|
|
|
|
-- on state change
|
|
|
|
---@param value any new value
|
|
|
|
function e.on_update(value)
|
2022-09-12 16:59:28 +00:00
|
|
|
e.value = value
|
|
|
|
|
2022-06-11 16:21:14 +00:00
|
|
|
local data_str = util.sprintf(args.format, value)
|
|
|
|
|
|
|
|
-- write data
|
|
|
|
e.window.setCursorPos(data_start, 1)
|
2022-06-11 20:38:15 +00:00
|
|
|
e.window.setTextColor(e.fg_bg.fgd)
|
2022-06-18 06:14:48 +00:00
|
|
|
if args.commas then
|
|
|
|
e.window.write(comma_format(data_str))
|
|
|
|
else
|
|
|
|
e.window.write(data_str)
|
|
|
|
end
|
2022-06-11 16:21:14 +00:00
|
|
|
|
|
|
|
-- write label
|
|
|
|
if args.unit ~= nil then
|
2022-06-16 15:19:32 +00:00
|
|
|
if args.lu_colors ~= nil then
|
|
|
|
e.window.setTextColor(args.lu_colors.color_b)
|
2022-06-11 16:21:14 +00:00
|
|
|
end
|
|
|
|
e.window.write(" " .. args.unit)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-12 19:58:43 +00:00
|
|
|
-- set the value
|
|
|
|
---@param val any new value
|
2022-09-12 16:59:28 +00:00
|
|
|
function e.set_value(val) e.on_update(val) end
|
|
|
|
|
2022-06-11 16:21:14 +00:00
|
|
|
-- initial value draw
|
2022-06-16 15:19:32 +00:00
|
|
|
e.on_update(args.value)
|
2022-06-11 16:21:14 +00:00
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
return e.get()
|
2022-06-11 16:21:14 +00:00
|
|
|
end
|
|
|
|
|
2022-06-16 15:19:32 +00:00
|
|
|
return data
|