Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Initial configurations and setup
- term.setBackgroundColour(bgColour)
- term.clear()
- term.setCursorPos(x, y)
- term.setCursorBlink(true)
- -- Configuration File Paths
- local configPath = "/disk/os/gui.config"
- local menuItems = {"Save", "Print", "Run", "Exit"}
- -- Load Configuration
- local function loadConfig()
- if fs.exists(configPath) then
- local file = fs.open(configPath, "r")
- local config = file.readAll()
- file.close()
- -- Parse config if needed
- end
- end
- -- Utility Functions
- local function set_status(message, isSuccess)
- -- Implementation of setting status messages
- end
- local function redrawMenu()
- -- Implementation of menu redraw
- end
- local function redrawText()
- -- Implementation of text redraw
- end
- local function setCursor(newX, newY)
- local _, oldY = x, y
- x, y = newX, newY
- local screenX = x - scrollX
- local screenY = y - scrollY
- local bRedraw = false
- if screenX < 1 then
- scrollX = x - 1
- screenX = 1
- bRedraw = true
- elseif screenX > w then
- scrollX = x - w
- screenX = w
- bRedraw = true
- end
- if screenY < 1 then
- scrollY = y - 1
- screenY = 1
- bRedraw = true
- elseif screenY > h - 1 then
- scrollY = y - (h - 1)
- screenY = h - 1
- bRedraw = true
- end
- recomplete()
- if bRedraw then
- redrawText()
- elseif y ~= oldY then
- redrawLine(oldY)
- redrawLine(y)
- else
- redrawLine(y)
- end
- term.setCursorPos(screenX, screenY)
- redrawMenu()
- end
- -- Menu Functions
- local tMenuFuncs = {
- Save = function()
- if bReadOnly then
- set_status("Access denied", false)
- else
- local ok, _, fileerr = save(sPath, function(file)
- for _, sLine in ipairs(tLines) do
- file.write(sLine .. "\n")
- end
- end)
- if ok then
- set_status("Saved to " .. sPath)
- else
- if fileerr then
- set_status("Error saving: " .. fileerr, false)
- else
- set_status("Error saving to " .. sPath, false)
- end
- end
- end
- redrawMenu()
- end,
- Print = function()
- local printer = peripheral.find("printer")
- if not printer then
- set_status("No printer attached", false)
- return
- end
- local nPage = 0
- local sName = fs.getName(sPath)
- if printer.getInkLevel() < 1 then
- set_status("Printer out of ink", false)
- return
- elseif printer.getPaperLevel() < 1 then
- set_status("Printer out of paper", false)
- return
- end
- local screenTerminal = term.current()
- local printerTerminal = {
- getCursorPos = printer.getCursorPos,
- setCursorPos = printer.setCursorPos,
- getSize = printer.getPageSize,
- write = printer.write,
- }
- printerTerminal.scroll = function()
- if nPage == 1 then
- printer.setPageTitle(sName .. " (page " .. nPage .. ")")
- end
- while not printer.newPage() do
- if printer.getInkLevel() < 1 then
- set_status("Printer out of ink, please refill", false)
- elseif printer.getPaperLevel() < 1 then
- set_status("Printer out of paper, please refill", false)
- else
- set_status("Printer output tray full, please empty", false)
- end
- term.redirect(screenTerminal)
- redrawMenu()
- term.redirect(printerTerminal)
- sleep(0.5)
- end
- nPage = nPage + 1
- if nPage == 1 then
- printer.setPageTitle(sName)
- else
- printer.setPageTitle(sName .. " (page " .. nPage .. ")")
- end
- end
- bMenu = false
- term.redirect(printerTerminal)
- local ok, error = pcall(function()
- term.scroll()
- for _, sLine in ipairs(tLines) do
- print(sLine)
- end
- end)
- term.redirect(screenTerminal)
- if not ok then
- print(error)
- end
- while not printer.endPage() do
- set_status("Printer output tray full, please empty")
- redrawMenu()
- sleep(0.5)
- end
- bMenu = true
- if nPage > 1 then
- set_status("Printed " .. nPage .. " Pages")
- else
- set_status("Printed 1 Page")
- end
- redrawMenu()
- end,
- Exit = function()
- bRunning = false
- end,
- Run = function()
- local sTitle = fs.getName(sPath)
- if sTitle:sub(-4) == ".lua" then
- sTitle = sTitle:sub(1, -5)
- end
- local sTempPath = bReadOnly and ".temp." .. sTitle or fs.combine(fs.getDir(sPath), ".temp." .. sTitle)
- if fs.exists(sTempPath) then
- set_status("Error saving to " .. sTempPath, false)
- return
- end
- local ok = save(sTempPath, function(file)
- file.write(runHandler:format(sTitle, table.concat(tLines, "\n"), "@/" .. sPath))
- end)
- if ok then
- local nTask = shell.openTab("/" .. sTempPath)
- if nTask then
- shell.switchTab(nTask)
- else
- set_status("Error starting Task", false)
- end
- fs.delete(sTempPath)
- else
- set_status("Error saving to " .. sTempPath, false)
- end
- redrawMenu()
- end,
- }
- -- Menu Item Handler
- local function doMenuItem(_n)
- tMenuFuncs[tMenuItems[_n]]()
- if bMenu then
- bMenu = false
- term.setCursorBlink(true)
- end
- redrawMenu()
- end
- -- Main Loop
- local function acceptCompletion()
- if nCompletion then
- local sCompletion = tCompletions[nCompletion]
- tLines[y] = tLines[y] .. sCompletion
- setCursor(x + #sCompletion , y)
- end
- end
- -- Handle input
- while bRunning do
- local sEvent, param, param2, param3 = os.pullEvent()
- if sEvent == "key" then
- if param == keys.up then
- if not bMenu then
- if nCompletion then
- nCompletion = nCompletion - 1
- if nCompletion < 1 then
- nCompletion = #tCompletions
- end
- redrawLine(y)
- elseif y > 1 then
- setCursor(math.min(x, #tLines[y - 1] + 1), y - 1)
- end
- end
- elseif param == keys.down then
- if not bMenu then
- if nCompletion then
- nCompletion = nCompletion + 1
- if nCompletion > #tCompletions then
- nCompletion = 1
- end
- redrawLine(y)
- elseif y < #tLines then
- setCursor(math.min(x, #tLines[y + 1] + 1), y + 1)
- end
- end
- elseif param == keys.tab then
- if not bMenu and not bReadOnly then
- if nCompletion and x == #tLines[y] + 1 then
- acceptCompletion()
- else
- local sLine = tLines[y]
- tLines[y] = string.sub(sLine, 1, x - 1) .. " " .. string.sub(sLine, x)
- setCursor(x + 4, y)
- end
- end
- elseif param == keys.pageUp then
- if not bMenu then
- local newY = y - (h - 1)
- if newY < 1 then newY = 1 end
- setCursor(math.min(x, #tLines[newY] + 1), newY)
- end
- elseif param == keys.pageDown then
- if not bMenu then
- local newY = y + (h - 1)
- if newY > #tLines then newY = #tLines end
- local newX = math.min(x, #tLines[newY] + 1)
- setCursor(newX, newY)
- end
- elseif param == keys.home then
- if not bMenu then
- setCursor(1, y)
- end
- elseif param == keys.end then
- if not bMenu then
- setCursor(#tLines[y] + 1, y)
- end
- elseif param == keys.delete then
- if not bMenu and not bReadOnly then
- local sLine = tLines[y]
- tLines[y] = string.sub(sLine, 1, x - 1) .. string.sub(sLine, x + 1)
- redrawLine(y)
- end
- elseif param == keys.backspace then
- if not bMenu and not bReadOnly then
- if x > 1 then
- local sLine = tLines[y]
- tLines[y] = string.sub(sLine, 1, x - 2) .. string.sub(sLine, x)
- setCursor(x - 1, y)
- end
- end
- elseif param == keys.enter then
- if not bMenu and not bReadOnly then
- table.insert(tLines, y + 1, "")
- setCursor(1, y + 1)
- end
- elseif param == keys.left then
- if not bMenu then
- if x > 1 then
- setCursor(x - 1, y)
- end
- end
- elseif param == keys.right then
- if not bMenu then
- if x < #tLines[y] + 1 then
- setCursor(x + 1, y)
- end
- end
- elseif param == keys.f1 then
- if not bMenu then
- bMenu = true
- term.setCursorBlink(false)
- end
- elseif param == keys.f2 then
- if bMenu then
- doMenuItem(1)
- end
- elseif param == keys.f3 then
- if bMenu then
- doMenuItem(2)
- end
- elseif param == keys.f4 then
- if bMenu then
- doMenuItem(3)
- end
- elseif param == keys.f5 then
- if bMenu then
- doMenuItem(4)
- end
- end
- elseif sEvent == "mouse_click" then
- if bMenu then
- -- Handle menu clicks
- else
- -- Handle text area clicks
- end
- elseif sEvent == "mouse_scroll" then
- -- Handle mouse scroll
- elseif sEvent == "term_resize" then
- -- Handle terminal resize
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement