2022-06-11 16:21:14 +00:00
|
|
|
-- Data Indicator Graphics Element
|
|
|
|
|
|
|
|
local util = require("scada-common.util")
|
|
|
|
|
|
|
|
local element = require("graphics.element")
|
|
|
|
|
|
|
|
---@class data_indicator_args
|
|
|
|
---@field label string indicator label
|
|
|
|
---@field unit? string indicator unit
|
|
|
|
---@field format string data format (lua string format)
|
2022-11-11 21:32:14 +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
|
2023-07-10 03:42:44 +00:00
|
|
|
---@field y? integer auto incremented if omitted
|
2022-06-11 16:21:14 +00:00
|
|
|
---@field width integer length
|
2022-06-11 21:06:32 +00:00
|
|
|
---@field fg_bg? cpair foreground/background colors
|
2023-05-25 21:40:16 +00:00
|
|
|
---@field hidden? boolean true to hide on initial draw
|
2022-06-11 16:21:14 +00:00
|
|
|
|
|
|
|
-- new data indicator
|
2023-02-25 00:50:01 +00:00
|
|
|
---@nodiscard
|
2022-06-11 16:21:14 +00:00
|
|
|
---@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)
|
2023-09-29 23:34:10 +00:00
|
|
|
assert(type(args.label) == "string", "indicators.data: label is a required field")
|
|
|
|
assert(type(args.format) == "string", "indicators.data: format is a required field")
|
|
|
|
assert(args.value ~= nil, "indicators.data: value is a required field")
|
|
|
|
assert(util.is_int(args.width), "indicators.data: width is a required field")
|
2022-06-16 15:19:32 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-09-29 23:34:10 +00:00
|
|
|
e.value = args.value
|
2022-06-11 16:21:14 +00:00
|
|
|
|
2023-06-03 21:31:06 +00:00
|
|
|
local value_color = e.fg_bg.fgd
|
|
|
|
local label_len = string.len(args.label)
|
|
|
|
local data_start = 1
|
2022-12-06 16:40:13 +00:00
|
|
|
local clear_width = args.width
|
|
|
|
|
|
|
|
if label_len > 0 then
|
|
|
|
data_start = data_start + (label_len + 1)
|
|
|
|
clear_width = args.width - (label_len + 1)
|
|
|
|
end
|
2022-06-11 16:21:14 +00:00
|
|
|
|
|
|
|
-- 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-12-06 16:40:13 +00:00
|
|
|
-- clear old data and label
|
2023-08-31 01:11:57 +00:00
|
|
|
e.w_set_cur(data_start, 1)
|
|
|
|
e.w_write(util.spaces(clear_width))
|
2022-06-11 16:21:14 +00:00
|
|
|
|
|
|
|
-- write data
|
2022-12-06 16:40:13 +00:00
|
|
|
local data_str = util.sprintf(args.format, value)
|
2023-08-31 01:11:57 +00:00
|
|
|
e.w_set_cur(data_start, 1)
|
|
|
|
e.w_set_fgd(value_color)
|
2022-06-18 06:14:48 +00:00
|
|
|
if args.commas then
|
2023-08-31 01:11:57 +00:00
|
|
|
e.w_write(util.comma_format(data_str))
|
2022-06-18 06:14:48 +00:00
|
|
|
else
|
2023-08-31 01:11:57 +00:00
|
|
|
e.w_write(data_str)
|
2022-06-18 06:14:48 +00:00
|
|
|
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
|
2023-08-31 01:11:57 +00:00
|
|
|
e.w_set_fgd(args.lu_colors.color_b)
|
2022-06-11 16:21:14 +00:00
|
|
|
end
|
2023-08-31 01:11:57 +00:00
|
|
|
e.w_write(" " .. args.unit)
|
2022-06-11 16:21:14 +00:00
|
|
|
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
|
|
|
|
|
2023-05-23 23:22:22 +00:00
|
|
|
-- change the foreground color of the value, or all text if no label/unit colors provided
|
|
|
|
---@param c color
|
|
|
|
function e.recolor(c)
|
|
|
|
value_color = c
|
|
|
|
e.on_update(e.value)
|
|
|
|
end
|
|
|
|
|
2023-09-29 23:34:10 +00:00
|
|
|
-- element redraw
|
|
|
|
function e.redraw()
|
|
|
|
if args.lu_colors ~= nil then e.w_set_fgd(args.lu_colors.color_a) end
|
|
|
|
e.w_set_cur(1, 1)
|
|
|
|
e.w_write(args.label)
|
|
|
|
|
|
|
|
e.on_update(e.value)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- initial draw
|
|
|
|
e.redraw()
|
2022-06-11 16:21:14 +00:00
|
|
|
|
2023-05-30 23:51:10 +00:00
|
|
|
return e.complete()
|
2022-06-11 16:21:14 +00:00
|
|
|
end
|
|
|
|
|
2022-06-16 15:19:32 +00:00
|
|
|
return data
|