93 lines
2.3 KiB
Lua
93 lines
2.3 KiB
Lua
local monitor = peripheral.find("monitor")
|
|
local programs = {}
|
|
local isRunning = false
|
|
|
|
local function addProgram(name, path)
|
|
table.insert(programs, {name = name, path = path})
|
|
end
|
|
|
|
local function runProgram(path)
|
|
if fs.exists(path) then
|
|
local w, h = monitor.getSize()
|
|
|
|
monitor.setBackgroundColor(colors.black)
|
|
monitor.clear()
|
|
|
|
-- Draw X button
|
|
monitor.setCursorPos(w, 1)
|
|
monitor.setBackgroundColor(colors.red)
|
|
monitor.setTextColor(colors.white)
|
|
monitor.write("X")
|
|
monitor.setBackgroundColor(colors.black)
|
|
|
|
-- Create window for program
|
|
local win = window.create(monitor, 1, 2, w, h - 1)
|
|
local oldTerm = term.redirect(win)
|
|
|
|
local function run()
|
|
shell.run(path)
|
|
end
|
|
|
|
local function watchExit()
|
|
while true do
|
|
local _, _, x, y = os.pullEvent("monitor_touch")
|
|
if x == w and y == 1 then
|
|
return
|
|
end
|
|
end
|
|
end
|
|
|
|
parallel.waitForAny(run, watchExit)
|
|
|
|
term.redirect(oldTerm)
|
|
return true
|
|
else
|
|
print("Program not found: " .. path)
|
|
return false
|
|
end
|
|
end
|
|
|
|
local function exitProgram()
|
|
isRunning = false
|
|
end
|
|
|
|
local function drawList()
|
|
monitor.clear()
|
|
monitor.setCursorPos(1, 1)
|
|
monitor.write("Available Programs:")
|
|
|
|
for i, program in ipairs(programs) do
|
|
monitor.setCursorPos(1, i + 2) -- Start from line 3
|
|
monitor.write(i .. ". " .. program.name)
|
|
end
|
|
|
|
monitor.setCursorPos(1, #programs + 4)
|
|
monitor.write("Click to run")
|
|
end
|
|
|
|
local function listPrograms()
|
|
isRunning = true
|
|
while isRunning do
|
|
drawList()
|
|
|
|
local event, side, x, y = os.pullEvent("monitor_touch")
|
|
local programIndex = y - 2
|
|
|
|
if programIndex > 0 and programIndex <= #programs then
|
|
local program = programs[programIndex]
|
|
monitor.clear()
|
|
monitor.setCursorPos(1,1)
|
|
monitor.write("Running " .. program.name .. "...")
|
|
sleep(1)
|
|
runProgram(program.path)
|
|
end
|
|
end
|
|
end
|
|
|
|
return {
|
|
addProgram = addProgram,
|
|
runProgram = runProgram,
|
|
exitProgram = exitProgram,
|
|
listPrograms = listPrograms
|
|
}
|