diff --git a/consumer/chat.lua b/consumer/chat.lua new file mode 100644 index 0000000..b95f18c --- /dev/null +++ b/consumer/chat.lua @@ -0,0 +1 @@ +print("Chat.lua loaded") \ No newline at end of file diff --git a/consumer/main.lua b/consumer/main.lua index 5e2b502..fa1e85a 100644 --- a/consumer/main.lua +++ b/consumer/main.lua @@ -1,6 +1,8 @@ local kernel = require("kernel") local monitor = kernel.addDriver("monitor-driver") local data = kernel.addDriver("disk-driver") +local taskManager = kernel.addDriver("program-driver") +local chatProgram = kernel.addDriver("chat") local function run() monitor.initialize() @@ -11,6 +13,9 @@ local function run() computerName = data.getRead("computer-name") os.setComputerLabel(computerName) monitor.writeLine("Computer name: " .. computerName) + + taskManager.addProgram("Coms", "chat.lua"); + taskManager.listPrograms() end return { run = run } \ No newline at end of file diff --git a/program-driver.lua b/program-driver.lua new file mode 100644 index 0000000..5b43b4e --- /dev/null +++ b/program-driver.lua @@ -0,0 +1,92 @@ +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 +}