Program fixes

Table fixes
Added :selectItem to List
This commit is contained in:
Robert Jelic
2025-06-01 17:59:02 +02:00
parent accb9c37a6
commit ea0f31fe37
3 changed files with 57 additions and 8 deletions

View File

@@ -173,6 +173,38 @@ function List:mouse_scroll(direction, x, y)
return false
end
--- Selects an item by index
--- @shortDescription Selects an item by index
--- @param index number The index of the item to select
--- @return List self The List instance
function List:selectItem(index)
local items = self.get("items")
if not self.get("multiSelection") then
for _, item in ipairs(items) do
if type(item) == "table" then
item.selected = false
end
end
end
local item = items[index]
if type(item) == "string" then
item = {text = item}
items[index] = item
end
item.selected = true
if item.callback then
item.callback(self)
end
self:fireEvent("select", index, item)
self:updateRender()
return self
end
--- Registers a callback for the select event
--- @shortDescription Registers a callback for the select event
--- @param callback function The callback function to register

View File

@@ -34,6 +34,10 @@ function BasaltProgram.new(program, env, addEnvironment)
return self
end
function BasaltProgram:setArgs(...)
self.args = {...}
end
local function createShellEnv(dir)
local env = { shell = shell, multishell = multishell }
env.require, env.package = newPackage(env, dir)
@@ -53,6 +57,7 @@ function BasaltProgram:run(path, width, height)
local env = setmetatable(createShellEnv(fs.getDir(path)), { __index = _ENV })
env.term = self.window
env.term.current = term.current
env.term.redirect = term.redirect
env.term.native = function ()
return self.window
end
@@ -102,6 +107,7 @@ end
---@private
function BasaltProgram:resize(width, height)
self.window.reposition(1, 1, width, height)
self:resume("term_resize", width, height)
end
---@private
@@ -165,6 +171,18 @@ end
function Program:init(props, basalt)
VisualElement.init(self, props, basalt)
self.set("type", "Program")
self:observe("width", function(self, width)
local program = self.get("program")
if program then
program:resize(width, self.get("height"))
end
end)
self:observe("height", function(self, height)
local program = self.get("program")
if program then
program:resize(self.get("width"), height)
end
end)
return self
end
@@ -174,12 +192,13 @@ end
--- @param env? table The environment to run the program in
--- @param addEnvironment? boolean Whether to add the environment to the program's environment (false = overwrite instead of adding)
--- @return Program self The Program instance
function Program:execute(path, env, addEnvironment)
function Program:execute(path, env, addEnvironment, ...)
self.set("path", path)
self.set("running", true)
local program = BasaltProgram.new(self, env, addEnvironment)
self.set("program", program)
program:run(path, self.get("width"), self.get("height"))
program:setArgs(...)
program:run(path, self.get("width"), self.get("height"), ...)
self:updateRender()
return self
end

View File

@@ -193,7 +193,6 @@ function Table:render()
currentX = currentX + col.width
end
local visibleRows = height - 2
for y = 2, height do
local rowIndex = y - 2 + scrollOffset
local rowData = data[rowIndex + 1]
@@ -209,11 +208,10 @@ function Table:render()
paddedText = string.sub(paddedText, 1, col.width - 1) .. " "
end
local finalText = string.sub(paddedText, 1, col.width)
local finalForeground = string.sub(string.rep(tHex[self.get("foreground")], col.width), 1, width-currentX+1)
local finalBackground = string.sub(string.rep(tHex[bg], col.width), 1, width-currentX+1)
self:blit(currentX, y, finalText,
finalForeground,
finalBackground)
local finalForeground = string.rep(tHex[self.get("foreground")], #finalText)
local finalBackground = string.rep(tHex[bg], #finalText)
self:blit(currentX, y, finalText, finalForeground, finalBackground)
currentX = currentX + col.width
end
else