Program fixes
Table fixes Added :selectItem to List
This commit is contained in:
@@ -173,6 +173,38 @@ function List:mouse_scroll(direction, x, y)
|
|||||||
return false
|
return false
|
||||||
end
|
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
|
--- Registers a callback for the select event
|
||||||
--- @shortDescription Registers a callback for the select event
|
--- @shortDescription Registers a callback for the select event
|
||||||
--- @param callback function The callback function to register
|
--- @param callback function The callback function to register
|
||||||
|
|||||||
@@ -34,6 +34,10 @@ function BasaltProgram.new(program, env, addEnvironment)
|
|||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function BasaltProgram:setArgs(...)
|
||||||
|
self.args = {...}
|
||||||
|
end
|
||||||
|
|
||||||
local function createShellEnv(dir)
|
local function createShellEnv(dir)
|
||||||
local env = { shell = shell, multishell = multishell }
|
local env = { shell = shell, multishell = multishell }
|
||||||
env.require, env.package = newPackage(env, dir)
|
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 })
|
local env = setmetatable(createShellEnv(fs.getDir(path)), { __index = _ENV })
|
||||||
env.term = self.window
|
env.term = self.window
|
||||||
env.term.current = term.current
|
env.term.current = term.current
|
||||||
|
env.term.redirect = term.redirect
|
||||||
env.term.native = function ()
|
env.term.native = function ()
|
||||||
return self.window
|
return self.window
|
||||||
end
|
end
|
||||||
@@ -102,6 +107,7 @@ end
|
|||||||
---@private
|
---@private
|
||||||
function BasaltProgram:resize(width, height)
|
function BasaltProgram:resize(width, height)
|
||||||
self.window.reposition(1, 1, width, height)
|
self.window.reposition(1, 1, width, height)
|
||||||
|
self:resume("term_resize", width, height)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@private
|
---@private
|
||||||
@@ -165,6 +171,18 @@ end
|
|||||||
function Program:init(props, basalt)
|
function Program:init(props, basalt)
|
||||||
VisualElement.init(self, props, basalt)
|
VisualElement.init(self, props, basalt)
|
||||||
self.set("type", "Program")
|
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
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -174,12 +192,13 @@ end
|
|||||||
--- @param env? table The environment to run the program in
|
--- @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)
|
--- @param addEnvironment? boolean Whether to add the environment to the program's environment (false = overwrite instead of adding)
|
||||||
--- @return Program self The Program instance
|
--- @return Program self The Program instance
|
||||||
function Program:execute(path, env, addEnvironment)
|
function Program:execute(path, env, addEnvironment, ...)
|
||||||
self.set("path", path)
|
self.set("path", path)
|
||||||
self.set("running", true)
|
self.set("running", true)
|
||||||
local program = BasaltProgram.new(self, env, addEnvironment)
|
local program = BasaltProgram.new(self, env, addEnvironment)
|
||||||
self.set("program", program)
|
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()
|
self:updateRender()
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -193,7 +193,6 @@ function Table:render()
|
|||||||
currentX = currentX + col.width
|
currentX = currentX + col.width
|
||||||
end
|
end
|
||||||
|
|
||||||
local visibleRows = height - 2
|
|
||||||
for y = 2, height do
|
for y = 2, height do
|
||||||
local rowIndex = y - 2 + scrollOffset
|
local rowIndex = y - 2 + scrollOffset
|
||||||
local rowData = data[rowIndex + 1]
|
local rowData = data[rowIndex + 1]
|
||||||
@@ -209,11 +208,10 @@ function Table:render()
|
|||||||
paddedText = string.sub(paddedText, 1, col.width - 1) .. " "
|
paddedText = string.sub(paddedText, 1, col.width - 1) .. " "
|
||||||
end
|
end
|
||||||
local finalText = string.sub(paddedText, 1, col.width)
|
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 finalForeground = string.rep(tHex[self.get("foreground")], #finalText)
|
||||||
local finalBackground = string.sub(string.rep(tHex[bg], col.width), 1, width-currentX+1)
|
local finalBackground = string.rep(tHex[bg], #finalText)
|
||||||
self:blit(currentX, y, finalText,
|
|
||||||
finalForeground,
|
self:blit(currentX, y, finalText, finalForeground, finalBackground)
|
||||||
finalBackground)
|
|
||||||
currentX = currentX + col.width
|
currentX = currentX + col.width
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
|||||||
Reference in New Issue
Block a user