Skip to content

Commit ea0f31f

Browse files
committed
Program fixes
Table fixes Added :selectItem to List
1 parent accb9c3 commit ea0f31f

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

src/elements/List.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,38 @@ function List:mouse_scroll(direction, x, y)
173173
return false
174174
end
175175

176+
--- Selects an item by index
177+
--- @shortDescription Selects an item by index
178+
--- @param index number The index of the item to select
179+
--- @return List self The List instance
180+
function List:selectItem(index)
181+
local items = self.get("items")
182+
183+
if not self.get("multiSelection") then
184+
for _, item in ipairs(items) do
185+
if type(item) == "table" then
186+
item.selected = false
187+
end
188+
end
189+
end
190+
191+
local item = items[index]
192+
if type(item) == "string" then
193+
item = {text = item}
194+
items[index] = item
195+
end
196+
197+
item.selected = true
198+
199+
if item.callback then
200+
item.callback(self)
201+
end
202+
203+
self:fireEvent("select", index, item)
204+
self:updateRender()
205+
return self
206+
end
207+
176208
--- Registers a callback for the select event
177209
--- @shortDescription Registers a callback for the select event
178210
--- @param callback function The callback function to register

src/elements/Program.lua

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ function BasaltProgram.new(program, env, addEnvironment)
3434
return self
3535
end
3636

37+
function BasaltProgram:setArgs(...)
38+
self.args = {...}
39+
end
40+
3741
local function createShellEnv(dir)
3842
local env = { shell = shell, multishell = multishell }
3943
env.require, env.package = newPackage(env, dir)
@@ -53,6 +57,7 @@ function BasaltProgram:run(path, width, height)
5357
local env = setmetatable(createShellEnv(fs.getDir(path)), { __index = _ENV })
5458
env.term = self.window
5559
env.term.current = term.current
60+
env.term.redirect = term.redirect
5661
env.term.native = function ()
5762
return self.window
5863
end
@@ -102,6 +107,7 @@ end
102107
---@private
103108
function BasaltProgram:resize(width, height)
104109
self.window.reposition(1, 1, width, height)
110+
self:resume("term_resize", width, height)
105111
end
106112

107113
---@private
@@ -165,6 +171,18 @@ end
165171
function Program:init(props, basalt)
166172
VisualElement.init(self, props, basalt)
167173
self.set("type", "Program")
174+
self:observe("width", function(self, width)
175+
local program = self.get("program")
176+
if program then
177+
program:resize(width, self.get("height"))
178+
end
179+
end)
180+
self:observe("height", function(self, height)
181+
local program = self.get("program")
182+
if program then
183+
program:resize(self.get("width"), height)
184+
end
185+
end)
168186
return self
169187
end
170188

@@ -174,12 +192,13 @@ end
174192
--- @param env? table The environment to run the program in
175193
--- @param addEnvironment? boolean Whether to add the environment to the program's environment (false = overwrite instead of adding)
176194
--- @return Program self The Program instance
177-
function Program:execute(path, env, addEnvironment)
195+
function Program:execute(path, env, addEnvironment, ...)
178196
self.set("path", path)
179197
self.set("running", true)
180198
local program = BasaltProgram.new(self, env, addEnvironment)
181199
self.set("program", program)
182-
program:run(path, self.get("width"), self.get("height"))
200+
program:setArgs(...)
201+
program:run(path, self.get("width"), self.get("height"), ...)
183202
self:updateRender()
184203
return self
185204
end

src/elements/Table.lua

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ function Table:render()
193193
currentX = currentX + col.width
194194
end
195195

196-
local visibleRows = height - 2
197196
for y = 2, height do
198197
local rowIndex = y - 2 + scrollOffset
199198
local rowData = data[rowIndex + 1]
@@ -209,11 +208,10 @@ function Table:render()
209208
paddedText = string.sub(paddedText, 1, col.width - 1) .. " "
210209
end
211210
local finalText = string.sub(paddedText, 1, col.width)
212-
local finalForeground = string.sub(string.rep(tHex[self.get("foreground")], col.width), 1, width-currentX+1)
213-
local finalBackground = string.sub(string.rep(tHex[bg], col.width), 1, width-currentX+1)
214-
self:blit(currentX, y, finalText,
215-
finalForeground,
216-
finalBackground)
211+
local finalForeground = string.rep(tHex[self.get("foreground")], #finalText)
212+
local finalBackground = string.rep(tHex[bg], #finalText)
213+
214+
self:blit(currentX, y, finalText, finalForeground, finalBackground)
217215
currentX = currentX + col.width
218216
end
219217
else

0 commit comments

Comments
 (0)