Orion09

Untitled

Mar 30th, 2025 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local gpu = component.gpu
  4. local keyboard = component.keyboard
  5. print("foo")
  6. -- Create a window with position, size, and buffers
  7. local function createWindow(x, y, width, height)
  8. local buffer = {}
  9. local colorBuffer = {}
  10. for i = 1, height do
  11. buffer[i] = {}
  12. colorBuffer[i] = {}
  13. for j = 1, width do
  14. buffer[i][j] = " "
  15. colorBuffer[i][j] = 0xFFFFFF -- White text
  16. end
  17. end
  18. return {
  19. x = x,
  20. y = y,
  21. width = width,
  22. height = height,
  23. buffer = buffer,
  24. colorBuffer = colorBuffer,
  25. cursorX = 1,
  26. cursorY = 1,
  27. inputBuffer = {},
  28. isWaitingForInput = false,
  29. co = nil, -- Coroutine will be set later
  30. term = nil -- Custom term object will be set later
  31. }
  32. end
  33.  
  34. -- Write text to a window’s buffer at the cursor position
  35. local function windowWrite(window, text)
  36. for char in text:gmatch(".") do
  37. if window.cursorX <= window.width and window.cursorY <= window.height then
  38. window.buffer[window.cursorY][window.cursorX] = char
  39. window.colorBuffer[window.cursorY][window.cursorX] = 0xFFFFFF
  40. window.cursorX = window.cursorX + 1
  41. end
  42. end
  43. end
  44.  
  45. -- Create a custom term object for a window
  46. local function createCustomTerm(window)
  47. return {
  48. write = function(text)
  49. windowWrite(window, tostring(text))
  50. end,
  51. clear = function()
  52. for i = 1, window.height do
  53. for j = 1, window.width do
  54. window.buffer[i][j] = " "
  55. window.colorBuffer[i][j] = 0xFFFFFF
  56. end
  57. end
  58. window.cursorX, window.cursorY = 1, 1
  59. end,
  60. setCursorPos = function(cx, cy)
  61. window.cursorX = math.min(math.max(cx, 1), window.width)
  62. window.cursorY = math.min(math.max(cy, 1), window.height)
  63. end,
  64. getCursor = function()
  65. return window.cursorX, window.cursorY
  66. end,
  67. read = function()
  68. window.isWaitingForInput = true
  69. local line = coroutine.yield("waiting_for_input")
  70. window.isWaitingForInput = false
  71. return line
  72. end,
  73. -- Minimal implementation; add more term functions as needed
  74. clearLine = function()
  75. local cx, cy = window.cursorX, window.cursorY
  76. for j = 1, window.width do
  77. window.buffer[cy][j] = " "
  78. window.colorBuffer[cy][j] = 0xFFFFFF
  79. end
  80. window.cursorX = 1
  81. end
  82. }
  83. end
  84.  
  85. -- Create a terminal window with a shell coroutine
  86. local function createTerminalWindow(x, y, width, height)
  87. local window = createWindow(x, y, width, height)
  88. local customTerm = createCustomTerm(window)
  89. window.term = customTerm
  90. -- Load the OpenComputers shell with the custom term
  91. local env = {term = customTerm, _G = _G}
  92. local shellFile = io.open("/lib/shell.lua", "r")
  93. if not shellFile then
  94. error("Cannot find /lib/shell.lua. Ensure OpenOS is installed.")
  95. end
  96. local shellCode = shellFile:read("*a")
  97. shellFile:close()
  98. local shellFunc = load(shellCode, "/lib/shell.lua", "t", env)
  99. window.co = coroutine.create(shellFunc)
  100. return window
  101. end
  102.  
  103. -- Render all windows to the screen
  104. local function renderDesktop(windows)
  105. gpu.setBackground(0x000000) -- Black background
  106. gpu.fill(1, 1, screenWidth, screenHeight, " ") -- Clear screen
  107. for _, win in ipairs(windows) do
  108. for i = 1, win.height do
  109. for j = 1, win.width do
  110. local char = win.buffer[i][j]
  111. local color = win.colorBuffer[i][j]
  112. gpu.setForeground(color)
  113. gpu.set(win.x + j - 1, win.y + i - 1, char)
  114. end
  115. end
  116. end
  117. end
  118.  
  119. -- Initialize the desktop
  120. local screenWidth, screenHeight = gpu.getResolution()
  121. local windows = {
  122. createTerminalWindow(1, 1, screenWidth // 2, screenHeight), -- Left terminal
  123. createTerminalWindow(screenWidth // 2 + 1, 1, screenWidth // 2, screenHeight) -- Right terminal
  124. }
  125. local focusedWindow = windows[1]
  126.  
  127. -- Start each terminal’s coroutine
  128. for _, win in ipairs(windows) do
  129. local status, result = coroutine.resume(win.co)
  130. if not status then
  131. error("Coroutine error: " .. result)
  132. elseif result == "waiting_for_input" then
  133. win.isWaitingForInput = true
  134. end
  135. end
  136.  
  137. -- Main event loop
  138. while true do
  139. local eventData = {event.pull()}
  140. local eventType = eventData[1]
  141. if eventType == "key_down" and focusedWindow and focusedWindow.isWaitingForInput then
  142. local char, key = eventData[3], eventData[4]
  143. if key == 28 then -- Enter (key code 28)
  144. local line = table.concat(focusedWindow.inputBuffer)
  145. focusedWindow.inputBuffer = {}
  146. local cx, cy = focusedWindow.term.getCursor()
  147. focusedWindow.term.setCursorPos(1, cy + 1) -- Move to next line
  148. local status, err = coroutine.resume(focusedWindow.co, line)
  149. if not status then
  150. focusedWindow.term.write("Error: " .. err .. "\n")
  151. end
  152. elseif key == 14 then -- Backspace (key code 14)
  153. if #focusedWindow.inputBuffer > 0 then
  154. table.remove(focusedWindow.inputBuffer)
  155. local cx, cy = focusedWindow.term.getCursor()
  156. if cx > 1 then
  157. focusedWindow.term.setCursorPos(cx - 1, cy)
  158. focusedWindow.term.write(" ")
  159. focusedWindow.term.setCursorPos(cx - 1, cy)
  160. end
  161. end
  162. elseif char and char ~= "" then
  163. table.insert(focusedWindow.inputBuffer, char)
  164. focusedWindow.term.write(char)
  165. end
  166. elseif eventType == "touch" then -- Mouse click to change focus
  167. local x, y = eventData[3], eventData[4]
  168. for _, win in ipairs(windows) do
  169. if x >= win.x and x < win.x + win.width and y >= win.y and y < win.y + win.height then
  170. focusedWindow = win
  171. break
  172. end
  173. end
  174. end
  175. renderDesktop(windows)
  176. end
Advertisement
Add Comment
Please, Sign In to add comment