diff --git a/src/elements/Graph.lua b/src/elements/Graph.lua new file mode 100644 index 0000000..2952678 --- /dev/null +++ b/src/elements/Graph.lua @@ -0,0 +1,64 @@ +local elementManager = require("elementManager") +local VisualElement = elementManager.getElement("elements/VisualElement") +---@configDescription +---@configDefault false + +---@class Graph : VisualElement +local Graph = setmetatable({}, VisualElement) +Graph.__index = Graph + +Graph.defineProperty(Graph, "data", {default = {}, type = "table", canTriggerRender = true}) +Graph.defineProperty(Graph, "minValue", {default = 0, type = "number", canTriggerRender = true}) +Graph.defineProperty(Graph, "maxValue", {default = 100, type = "number", canTriggerRender = true}) +Graph.defineProperty(Graph, "graphColor", {default = colors.yellow, type = "color", canTriggerRender = true}) +Graph.defineProperty(Graph, "graphSymbol", {default = "\127", type = "string", canTriggerRender = true}) -- Default: "|" + +function Graph.new() + local self = setmetatable({}, Graph):__init() + return self +end + +function Graph:init(props, basalt) + VisualElement.init(self, props, basalt) + self.set("type", "Graph") + return self +end + +function Graph:setPoint(index, value) + local data = self.get("data") + data[index] = value + self:updateRender() +end + +function Graph:addPoint(value) + local data = self.get("data") + table.insert(data, value) + while #data > self.get("width") do + table.remove(data, 1) + end + self:updateRender() +end + +function Graph:render() + VisualElement.render(self) + + local data = self.get("data") + local width = self.get("width") + local height = self.get("height") + local minVal = self.get("minValue") + local maxVal = self.get("maxValue") + local symbol = self.get("graphSymbol") + local graphColor = self.get("graphColor") + + for x = 1, width do + if data[x] then + local normalizedValue = (data[x] - minVal) / (maxVal - minVal) + local y = math.floor(height - (normalizedValue * (height-1))) + y = math.max(1, math.min(y, height)) + + self:textFg(x, y, symbol, graphColor) + end + end +end + +return Graph diff --git a/src/elements/Image.lua b/src/elements/Image.lua index cd532ff..6ff35d7 100644 --- a/src/elements/Image.lua +++ b/src/elements/Image.lua @@ -325,11 +325,13 @@ end --- @return Image self The Image instance function Image:addFrame() local frames = self.get("bimg") + local width = frames.width or #frames[1][1][1] + local height = frames.height or #frames[1] local frame = {} - local text = string.rep(" ", self.get("width")) - local fg = string.rep("f", self.get("width")) - local bg = string.rep("0", self.get("width")) - for y = 1, self.get("height") do + local text = string.rep(" ", width) + local fg = string.rep("f", width) + local bg = string.rep("0", width) + for y = 1, height do frame[y] = {text, fg, bg} end table.insert(frames, frame) diff --git a/src/elements/List.lua b/src/elements/List.lua index a010c80..3cf7363 100644 --- a/src/elements/List.lua +++ b/src/elements/List.lua @@ -99,6 +99,19 @@ function List:getSelectedItems() return selected end +--- Gets first selected item +--- @shortDescription Gets first selected item +--- @return table? selected The first item +function List:getSelectedItem() + local items = self.get("items") + for i, item in ipairs(items) do + if type(item) == "table" and item.selected then + return item + end + end + return nil +end + --- @shortDescription Handles mouse click events --- @param button number The mouse button that was clicked --- @param x number The x-coordinate of the click diff --git a/src/main.lua b/src/main.lua index 1c008eb..52de918 100644 --- a/src/main.lua +++ b/src/main.lua @@ -244,6 +244,14 @@ function basalt.run(isActive) end end +--- Returns an element class +--- @shortDescription Returns an element class +--- @param name string The name of the element +--- @return table Element The element class +function basalt.getElementClass(name) + return elementManager.getElement(name) +end + --- Returns a Plugin API --- @shortDescription Returns a Plugin API --- @param name string The name of the plugin