Advertisement
nonogamer9

test

Sep 15th, 2024 (edited)
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.03 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local term = require("term")
  4. local gpu = component.gpu
  5. local computer = require("computer")
  6. local shell = require("shell")
  7. local filesystem = require("filesystem")
  8. local unicode = require("unicode")
  9.  
  10. -- Screen setup
  11. local w, h = gpu.getResolution()
  12. gpu.setBackground(0x000000)
  13. gpu.setForeground(0xFFFFFF)
  14. gpu.fill(1, 1, w, h, " ")
  15.  
  16. -- Window management
  17. local windows = {}
  18. local activeWindow = nil
  19. local draggedWindow = nil
  20. local resizingWindow = nil
  21. local dragOffsetX, dragOffsetY = 0, 0
  22.  
  23. -- Start Menu
  24. local startMenuOpen = false
  25. local startMenuItems = {"File Explorer", "Terminal", "Settings", "Power"}
  26.  
  27. -- Processes
  28. local processes = {}
  29.  
  30. local function createWindow(x, y, width, height, title)
  31. local window = {
  32. x = x, y = y, width = width, height = height, title = title,
  33. content = {}, cursorX = 1, cursorY = 1, inputBuffer = "", history = {}, historyIndex = 0,
  34. minimized = false
  35. }
  36. table.insert(windows, window)
  37. return window
  38. end
  39.  
  40. local function drawWindow(window)
  41. if window.minimized then
  42. -- Draw minimized representation
  43. gpu.setBackground(0x808080)
  44. gpu.fill(window.x, h, 10, 1, " ")
  45. gpu.set(window.x, h, window.title:sub(1, 10))
  46. return
  47. end
  48.  
  49. -- Window border
  50. gpu.setBackground(0xCCCCCC)
  51. gpu.fill(window.x, window.y, window.width, window.height, " ")
  52. gpu.setBackground(0x0000AA)
  53. gpu.fill(window.x, window.y, window.width, 1, " ")
  54. gpu.setForeground(0xFFFFFF)
  55. gpu.set(window.x + 1, window.y, window.title)
  56.  
  57. -- Close button
  58. gpu.setBackground(0xAA0000)
  59. gpu.set(window.x + window.width - 1, window.y, "X")
  60.  
  61. -- Minimize button
  62. gpu.setBackground(0x00AA00)
  63. gpu.set(window.x + window.width - 2, window.y, "_")
  64.  
  65. -- Content
  66. gpu.setBackground(0x000000)
  67. gpu.setForeground(0xFFFFFF)
  68. for i, line in ipairs(window.content) do
  69. gpu.set(window.x + 1, window.y + i, unicode.sub(line, 1, window.width - 2))
  70. end
  71.  
  72. -- Input buffer and cursor for terminal window
  73. if window == terminalWindow then
  74. local prompt = "> "
  75. local inputLine = prompt .. window.inputBuffer
  76. gpu.setForeground(0xFFFFFF) -- Set text color to white
  77. gpu.setBackground(0x000000) -- Set background to black
  78. gpu.fill(window.x + 1, window.y + #window.content + 1, window.width - 2, 1, " ") -- Clear the input line
  79. gpu.set(window.x + 1, window.y + #window.content + 1, inputLine)
  80. if computer.uptime() % 1 > 0.5 then -- Blinking cursor
  81. gpu.set(window.x + #inputLine + 1, window.y + #window.content + 1, "_")
  82. end
  83. end
  84.  
  85. -- Resize handle
  86. gpu.setBackground(0x888888)
  87. gpu.set(window.x + window.width - 1, window.y + window.height - 1, "+")
  88. end
  89.  
  90. local function drawAllWindows()
  91. gpu.setBackground(0x000088) -- Set a dark blue background
  92. gpu.fill(1, 1, w, h, " ") -- Clear the screen with the background color
  93. for _, window in ipairs(windows) do
  94. drawWindow(window)
  95. end
  96. end
  97.  
  98. local function drawStartMenu()
  99. if startMenuOpen then
  100. gpu.setBackground(0x333333)
  101. gpu.fill(1, h-10, 20, 10, " ")
  102. for i, item in ipairs(startMenuItems) do
  103. gpu.set(2, h-10+i, item)
  104. end
  105. end
  106. end
  107.  
  108. local function toggleStartMenu()
  109. startMenuOpen = not startMenuOpen
  110. drawAllWindows()
  111. drawStartMenu()
  112. end
  113.  
  114. -- Terminal functionality
  115. local terminalWindow = createWindow(1, h - 15, w, 15, "NonoOS Terminal")
  116. activeWindow = terminalWindow
  117.  
  118. local function writeToTerminal(text)
  119. for line in text:gmatch("[^\r\n]+") do
  120. table.insert(terminalWindow.content, line)
  121. end
  122. while #terminalWindow.content > terminalWindow.height - 2 do
  123. table.remove(terminalWindow.content, 1)
  124. end
  125. end
  126.  
  127. local function captureOutput(f, ...)
  128. local oldWrite = io.write
  129. local buffer = ""
  130. io.write = function(...)
  131. local args = table.pack(...)
  132. for i = 1, args.n do
  133. buffer = buffer .. tostring(args[i])
  134. end
  135. end
  136. local result = table.pack(pcall(f, ...))
  137. io.write = oldWrite
  138. return buffer, table.unpack(result)
  139. end
  140.  
  141. local function createFileExplorer()
  142. local window = createWindow(10, 5, 40, 20, "File Explorer")
  143. window.currentPath = "/"
  144.  
  145. local function updateFileList()
  146. window.content = {}
  147. for file in filesystem.list(window.currentPath) do
  148. table.insert(window.content, file)
  149. end
  150. end
  151.  
  152. updateFileList()
  153. return window
  154. end
  155.  
  156. local function createSettingsWindow()
  157. local window = createWindow(20, 5, 40, 20, "Settings")
  158. window.content = {
  159. "1. Change background color",
  160. "2. Toggle start menu",
  161. "3. Adjust CPU usage calculation"
  162. }
  163. return window
  164. end
  165.  
  166. local function executeCommand(command)
  167. writeToTerminal("> " .. command)
  168. if command == "exit" then
  169. return false
  170. elseif command == "clear" then
  171. terminalWindow.content = {}
  172. elseif command == "explorer" then
  173. createFileExplorer()
  174. elseif command == "settings" then
  175. createSettingsWindow()
  176. elseif command:sub(-4) == ".lua" then
  177. -- Run .lua files in a new window
  178. local scriptWindow = createWindow(10, 5, w - 20, h - 10, "Script: " .. command)
  179. local scriptPath = shell.resolve(command)
  180. if filesystem.exists(scriptPath) then
  181. local env = setmetatable({}, {__index = _G})
  182. env.print = function(...)
  183. local args = table.pack(...)
  184. local line = ""
  185. for i = 1, args.n do
  186. line = line .. tostring(args[i]) .. "\t"
  187. end
  188. table.insert(scriptWindow.content, line)
  189. end
  190. local chunk, err = loadfile(scriptPath, "t", env)
  191. if chunk then
  192. local process = coroutine.create(chunk)
  193. table.insert(processes, {co = process, window = scriptWindow})
  194. else
  195. table.insert(scriptWindow.content, "Error loading file: " .. tostring(err))
  196. end
  197. else
  198. table.insert(scriptWindow.content, "File not found: " .. command)
  199. end
  200. else
  201. local output, success, result = captureOutput(shell.execute, command)
  202. writeToTerminal(output)
  203. if not success then
  204. writeToTerminal("Error: " .. tostring(result))
  205. end
  206. end
  207. return true
  208. end
  209.  
  210. -- CPU usage measurement
  211. local cpuUsage = 0
  212. local lastTime = computer.uptime()
  213. local function updateCPUUsage(elapsedTime)
  214. local currentTime = computer.uptime()
  215. local timeDiff = currentTime - lastTime
  216. lastTime = currentTime
  217.  
  218. -- Calculate CPU usage based on how long the main loop took compared to the event timeout
  219. cpuUsage = (elapsedTime / timeDiff) * 100
  220. cpuUsage = math.min(100, math.max(0, cpuUsage))
  221. end
  222.  
  223. -- Task Manager
  224. local taskManagerWindow = createWindow(w - 30, 1, 30, 7, "Task Manager")
  225. local function updateTaskManager()
  226. local totalMemory = computer.totalMemory()
  227. local freeMemory = computer.freeMemory()
  228. local usedMemory = totalMemory - freeMemory
  229.  
  230. taskManagerWindow.content = {
  231. string.format("OS: NonoOS"),
  232. string.format("CPU: %.1f%%", cpuUsage),
  233. string.format("RAM: %d/%d KB", math.floor(usedMemory / 1024), math.floor(totalMemory / 1024)),
  234. string.format("Free: %d KB", math.floor(freeMemory / 1024)),
  235. string.format("Windows: %d", #windows),
  236. string.format("Processes: %d", #processes)
  237. }
  238.  
  239. for i, process in ipairs(processes) do
  240. table.insert(taskManagerWindow.content, string.format("Process %d: %s", i, coroutine.status(process.co)))
  241. end
  242. end
  243.  
  244. local function startResizing(window, x, y)
  245. resizingWindow = window
  246. dragOffsetX = window.x + window.width - x
  247. dragOffsetY = window.y + window.height - y
  248. end
  249.  
  250. local function minimizeWindow(window)
  251. window.minimized = true
  252. end
  253.  
  254. local function maximizeWindow(window)
  255. window.x = 1
  256. window.y = 2
  257. window.width = w
  258. window.height = h - 1
  259. end
  260.  
  261. local function runProcesses()
  262. for i = #processes, 1, -1 do
  263. local process = processes[i]
  264. if coroutine.status(process.co) ~= "dead" then
  265. local ok, result = coroutine.resume(process.co)
  266. if not ok then
  267. table.insert(process.window.content, "Process error: " .. tostring(result))
  268. table.remove(processes, i)
  269. end
  270. else
  271. table.remove(processes, i)
  272. end
  273. end
  274. end
  275.  
  276. -- Main loop
  277. local running = true
  278. while running do
  279. local loopStartTime = computer.uptime()
  280.  
  281. drawAllWindows()
  282. drawStartMenu()
  283. updateTaskManager()
  284.  
  285. local eventData = {event.pull(0.05)} -- Reduced timeout for responsiveness
  286. local eventType = eventData[1]
  287. local _, x, y = eventData[2], eventData[3], eventData[4]
  288.  
  289. if eventType == "touch" then
  290. if x == 1 and y == h then
  291. toggleStartMenu()
  292. else
  293. local handled = false
  294. for i = #windows, 1, -1 do
  295. local window = windows[i]
  296. if x >= window.x and x < window.x + window.width and
  297. y >= window.y and y < window.y + window.height then
  298. if x == window.x + window.width - 1 and y == window.y then
  299. table.remove(windows, i) -- Close window
  300. elseif x == window.x + window.width - 2 and y == window.y then
  301. minimizeWindow(window) -- Minimize window
  302. elseif x == window.x + window.width - 1 and y == window.y + window.height - 1 then
  303. startResizing(window, x, y)
  304. else
  305. activeWindow = window
  306. draggedWindow = window
  307. dragOffsetX = x - window.x
  308. dragOffsetY = y - window.y
  309. -- Move window to top
  310. table.remove(windows, i)
  311. table.insert(windows, window)
  312. end
  313. handled = true
  314. break
  315. end
  316. end
  317. if not handled and activeWindow == terminalWindow then
  318. local command = terminalWindow.inputBuffer
  319. running = executeCommand(command)
  320. terminalWindow.inputBuffer = ""
  321. end
  322. end
  323. elseif eventType == "drag" then
  324. if draggedWindow then
  325. draggedWindow.x = math.max(1, math.min(w - draggedWindow.width, x - dragOffsetX))
  326. draggedWindow.y = math.max(1, math.min(h - draggedWindow.height, y - dragOffsetY))
  327. elseif resizingWindow then
  328. local newWidth = math.max(10, x - resizingWindow.x + dragOffsetX)
  329. local newHeight = math.max(5, y - resizingWindow.y + dragOffsetY)
  330. resizingWindow.width = math.min(newWidth, w - resizingWindow.x + 1)
  331. resizingWindow.height = math.min(newHeight, h - resizingWindow.y + 1)
  332. end
  333. elseif eventType == "drop" then
  334. draggedWindow = nil
  335. resizingWindow = nil
  336. elseif eventType == "key_down" then
  337. if activeWindow == terminalWindow then
  338. local char, code = eventData[3], eventData[4]
  339. if char == 13 then -- Enter key
  340. local command = terminalWindow.inputBuffer
  341. table.insert(terminalWindow.history, command)
  342. terminalWindow.historyIndex = #terminalWindow.history + 1
  343. running = executeCommand(command)
  344. terminalWindow.inputBuffer = ""
  345. elseif code == 14 then -- Backspace
  346. terminalWindow.inputBuffer = unicode.sub(terminalWindow.inputBuffer, 1, -2)
  347. elseif code == 200 then -- Up arrow (command history)
  348. if terminalWindow.historyIndex > 1 then
  349. terminalWindow.historyIndex = terminalWindow.historyIndex - 1
  350. terminalWindow.inputBuffer = terminalWindow.history[terminalWindow.historyIndex]
  351. end
  352. elseif code == 208 then -- Down arrow (command history)
  353. if terminalWindow.historyIndex < #terminalWindow.history then
  354. terminalWindow.historyIndex = terminalWindow.historyIndex + 1
  355. terminalWindow.inputBuffer = terminalWindow.history[terminalWindow.historyIndex]
  356. elseif terminalWindow.historyIndex == #terminalWindow.history then
  357. terminalWindow.historyIndex = terminalWindow.historyIndex + 1
  358. terminalWindow.inputBuffer = ""
  359. end
  360. else
  361. local char = unicode.char(char)
  362. terminalWindow.inputBuffer = terminalWindow.inputBuffer .. char
  363. end
  364. end
  365. end
  366.  
  367. runProcesses()
  368.  
  369. local loopEndTime = computer.uptime()
  370. local elapsedTime = loopEndTime - loopStartTime
  371. updateCPUUsage(elapsedTime)
  372. end
  373.  
  374. -- Clean up
  375. gpu.setBackground(0x000000)
  376. gpu.setForeground(0xFFFFFF)
  377. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement