Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local gpu = component.gpu
- local keyboard = component.keyboard
- print("foo")
- -- Create a window with position, size, and buffers
- local function createWindow(x, y, width, height)
- local buffer = {}
- local colorBuffer = {}
- for i = 1, height do
- buffer[i] = {}
- colorBuffer[i] = {}
- for j = 1, width do
- buffer[i][j] = " "
- colorBuffer[i][j] = 0xFFFFFF -- White text
- end
- end
- return {
- x = x,
- y = y,
- width = width,
- height = height,
- buffer = buffer,
- colorBuffer = colorBuffer,
- cursorX = 1,
- cursorY = 1,
- inputBuffer = {},
- isWaitingForInput = false,
- co = nil, -- Coroutine will be set later
- term = nil -- Custom term object will be set later
- }
- end
- -- Write text to a window’s buffer at the cursor position
- local function windowWrite(window, text)
- for char in text:gmatch(".") do
- if window.cursorX <= window.width and window.cursorY <= window.height then
- window.buffer[window.cursorY][window.cursorX] = char
- window.colorBuffer[window.cursorY][window.cursorX] = 0xFFFFFF
- window.cursorX = window.cursorX + 1
- end
- end
- end
- -- Create a custom term object for a window
- local function createCustomTerm(window)
- return {
- write = function(text)
- windowWrite(window, tostring(text))
- end,
- clear = function()
- for i = 1, window.height do
- for j = 1, window.width do
- window.buffer[i][j] = " "
- window.colorBuffer[i][j] = 0xFFFFFF
- end
- end
- window.cursorX, window.cursorY = 1, 1
- end,
- setCursorPos = function(cx, cy)
- window.cursorX = math.min(math.max(cx, 1), window.width)
- window.cursorY = math.min(math.max(cy, 1), window.height)
- end,
- getCursor = function()
- return window.cursorX, window.cursorY
- end,
- read = function()
- window.isWaitingForInput = true
- local line = coroutine.yield("waiting_for_input")
- window.isWaitingForInput = false
- return line
- end,
- -- Minimal implementation; add more term functions as needed
- clearLine = function()
- local cx, cy = window.cursorX, window.cursorY
- for j = 1, window.width do
- window.buffer[cy][j] = " "
- window.colorBuffer[cy][j] = 0xFFFFFF
- end
- window.cursorX = 1
- end
- }
- end
- -- Create a terminal window with a shell coroutine
- local function createTerminalWindow(x, y, width, height)
- local window = createWindow(x, y, width, height)
- local customTerm = createCustomTerm(window)
- window.term = customTerm
- -- Load the OpenComputers shell with the custom term
- local env = {term = customTerm, _G = _G}
- local shellFile = io.open("/lib/shell.lua", "r")
- if not shellFile then
- error("Cannot find /lib/shell.lua. Ensure OpenOS is installed.")
- end
- local shellCode = shellFile:read("*a")
- shellFile:close()
- local shellFunc = load(shellCode, "/lib/shell.lua", "t", env)
- window.co = coroutine.create(shellFunc)
- return window
- end
- -- Render all windows to the screen
- local function renderDesktop(windows)
- gpu.setBackground(0x000000) -- Black background
- gpu.fill(1, 1, screenWidth, screenHeight, " ") -- Clear screen
- for _, win in ipairs(windows) do
- for i = 1, win.height do
- for j = 1, win.width do
- local char = win.buffer[i][j]
- local color = win.colorBuffer[i][j]
- gpu.setForeground(color)
- gpu.set(win.x + j - 1, win.y + i - 1, char)
- end
- end
- end
- end
- -- Initialize the desktop
- local screenWidth, screenHeight = gpu.getResolution()
- local windows = {
- createTerminalWindow(1, 1, screenWidth // 2, screenHeight), -- Left terminal
- createTerminalWindow(screenWidth // 2 + 1, 1, screenWidth // 2, screenHeight) -- Right terminal
- }
- local focusedWindow = windows[1]
- -- Start each terminal’s coroutine
- for _, win in ipairs(windows) do
- local status, result = coroutine.resume(win.co)
- if not status then
- error("Coroutine error: " .. result)
- elseif result == "waiting_for_input" then
- win.isWaitingForInput = true
- end
- end
- -- Main event loop
- while true do
- local eventData = {event.pull()}
- local eventType = eventData[1]
- if eventType == "key_down" and focusedWindow and focusedWindow.isWaitingForInput then
- local char, key = eventData[3], eventData[4]
- if key == 28 then -- Enter (key code 28)
- local line = table.concat(focusedWindow.inputBuffer)
- focusedWindow.inputBuffer = {}
- local cx, cy = focusedWindow.term.getCursor()
- focusedWindow.term.setCursorPos(1, cy + 1) -- Move to next line
- local status, err = coroutine.resume(focusedWindow.co, line)
- if not status then
- focusedWindow.term.write("Error: " .. err .. "\n")
- end
- elseif key == 14 then -- Backspace (key code 14)
- if #focusedWindow.inputBuffer > 0 then
- table.remove(focusedWindow.inputBuffer)
- local cx, cy = focusedWindow.term.getCursor()
- if cx > 1 then
- focusedWindow.term.setCursorPos(cx - 1, cy)
- focusedWindow.term.write(" ")
- focusedWindow.term.setCursorPos(cx - 1, cy)
- end
- end
- elseif char and char ~= "" then
- table.insert(focusedWindow.inputBuffer, char)
- focusedWindow.term.write(char)
- end
- elseif eventType == "touch" then -- Mouse click to change focus
- local x, y = eventData[3], eventData[4]
- for _, win in ipairs(windows) do
- if x >= win.x and x < win.x + win.width and y >= win.y and y < win.y + win.height then
- focusedWindow = win
- break
- end
- end
- end
- renderDesktop(windows)
- end
Advertisement
Add Comment
Please, Sign In to add comment