Advertisement
DOGGYWOOF

Untitled

Sep 7th, 2024 (edited)
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.51 KB | None | 0 0
  1. -- Initial configurations and setup
  2. term.setBackgroundColour(bgColour)
  3. term.clear()
  4. term.setCursorPos(x, y)
  5. term.setCursorBlink(true)
  6.  
  7. -- Configuration File Paths
  8. local configPath = "/disk/os/gui.config"
  9. local menuItems = {"Save", "Print", "Run", "Exit"}
  10.  
  11. -- Load Configuration
  12. local function loadConfig()
  13. if fs.exists(configPath) then
  14. local file = fs.open(configPath, "r")
  15. local config = file.readAll()
  16. file.close()
  17. -- Parse config if needed
  18. end
  19. end
  20.  
  21. -- Utility Functions
  22. local function set_status(message, isSuccess)
  23. -- Implementation of setting status messages
  24. end
  25.  
  26. local function redrawMenu()
  27. -- Implementation of menu redraw
  28. end
  29.  
  30. local function redrawText()
  31. -- Implementation of text redraw
  32. end
  33.  
  34. local function setCursor(newX, newY)
  35. local _, oldY = x, y
  36. x, y = newX, newY
  37. local screenX = x - scrollX
  38. local screenY = y - scrollY
  39.  
  40. local bRedraw = false
  41. if screenX < 1 then
  42. scrollX = x - 1
  43. screenX = 1
  44. bRedraw = true
  45. elseif screenX > w then
  46. scrollX = x - w
  47. screenX = w
  48. bRedraw = true
  49. end
  50.  
  51. if screenY < 1 then
  52. scrollY = y - 1
  53. screenY = 1
  54. bRedraw = true
  55. elseif screenY > h - 1 then
  56. scrollY = y - (h - 1)
  57. screenY = h - 1
  58. bRedraw = true
  59. end
  60.  
  61. recomplete()
  62. if bRedraw then
  63. redrawText()
  64. elseif y ~= oldY then
  65. redrawLine(oldY)
  66. redrawLine(y)
  67. else
  68. redrawLine(y)
  69. end
  70. term.setCursorPos(screenX, screenY)
  71.  
  72. redrawMenu()
  73. end
  74.  
  75. -- Menu Functions
  76. local tMenuFuncs = {
  77. Save = function()
  78. if bReadOnly then
  79. set_status("Access denied", false)
  80. else
  81. local ok, _, fileerr = save(sPath, function(file)
  82. for _, sLine in ipairs(tLines) do
  83. file.write(sLine .. "\n")
  84. end
  85. end)
  86. if ok then
  87. set_status("Saved to " .. sPath)
  88. else
  89. if fileerr then
  90. set_status("Error saving: " .. fileerr, false)
  91. else
  92. set_status("Error saving to " .. sPath, false)
  93. end
  94. end
  95. end
  96. redrawMenu()
  97. end,
  98. Print = function()
  99. local printer = peripheral.find("printer")
  100. if not printer then
  101. set_status("No printer attached", false)
  102. return
  103. end
  104.  
  105. local nPage = 0
  106. local sName = fs.getName(sPath)
  107. if printer.getInkLevel() < 1 then
  108. set_status("Printer out of ink", false)
  109. return
  110. elseif printer.getPaperLevel() < 1 then
  111. set_status("Printer out of paper", false)
  112. return
  113. end
  114.  
  115. local screenTerminal = term.current()
  116. local printerTerminal = {
  117. getCursorPos = printer.getCursorPos,
  118. setCursorPos = printer.setCursorPos,
  119. getSize = printer.getPageSize,
  120. write = printer.write,
  121. }
  122. printerTerminal.scroll = function()
  123. if nPage == 1 then
  124. printer.setPageTitle(sName .. " (page " .. nPage .. ")")
  125. end
  126.  
  127. while not printer.newPage() do
  128. if printer.getInkLevel() < 1 then
  129. set_status("Printer out of ink, please refill", false)
  130. elseif printer.getPaperLevel() < 1 then
  131. set_status("Printer out of paper, please refill", false)
  132. else
  133. set_status("Printer output tray full, please empty", false)
  134. end
  135.  
  136. term.redirect(screenTerminal)
  137. redrawMenu()
  138. term.redirect(printerTerminal)
  139.  
  140. sleep(0.5)
  141. end
  142.  
  143. nPage = nPage + 1
  144. if nPage == 1 then
  145. printer.setPageTitle(sName)
  146. else
  147. printer.setPageTitle(sName .. " (page " .. nPage .. ")")
  148. end
  149. end
  150.  
  151. bMenu = false
  152. term.redirect(printerTerminal)
  153. local ok, error = pcall(function()
  154. term.scroll()
  155. for _, sLine in ipairs(tLines) do
  156. print(sLine)
  157. end
  158. end)
  159. term.redirect(screenTerminal)
  160. if not ok then
  161. print(error)
  162. end
  163.  
  164. while not printer.endPage() do
  165. set_status("Printer output tray full, please empty")
  166. redrawMenu()
  167. sleep(0.5)
  168. end
  169. bMenu = true
  170.  
  171. if nPage > 1 then
  172. set_status("Printed " .. nPage .. " Pages")
  173. else
  174. set_status("Printed 1 Page")
  175. end
  176. redrawMenu()
  177. end,
  178. Exit = function()
  179. bRunning = false
  180. end,
  181. Run = function()
  182. local sTitle = fs.getName(sPath)
  183. if sTitle:sub(-4) == ".lua" then
  184. sTitle = sTitle:sub(1, -5)
  185. end
  186. local sTempPath = bReadOnly and ".temp." .. sTitle or fs.combine(fs.getDir(sPath), ".temp." .. sTitle)
  187. if fs.exists(sTempPath) then
  188. set_status("Error saving to " .. sTempPath, false)
  189. return
  190. end
  191. local ok = save(sTempPath, function(file)
  192. file.write(runHandler:format(sTitle, table.concat(tLines, "\n"), "@/" .. sPath))
  193. end)
  194. if ok then
  195. local nTask = shell.openTab("/" .. sTempPath)
  196. if nTask then
  197. shell.switchTab(nTask)
  198. else
  199. set_status("Error starting Task", false)
  200. end
  201. fs.delete(sTempPath)
  202. else
  203. set_status("Error saving to " .. sTempPath, false)
  204. end
  205. redrawMenu()
  206. end,
  207. }
  208.  
  209. -- Menu Item Handler
  210. local function doMenuItem(_n)
  211. tMenuFuncs[tMenuItems[_n]]()
  212. if bMenu then
  213. bMenu = false
  214. term.setCursorBlink(true)
  215. end
  216. redrawMenu()
  217. end
  218.  
  219. -- Main Loop
  220. local function acceptCompletion()
  221. if nCompletion then
  222. local sCompletion = tCompletions[nCompletion]
  223. tLines[y] = tLines[y] .. sCompletion
  224. setCursor(x + #sCompletion , y)
  225. end
  226. end
  227.  
  228. -- Handle input
  229. while bRunning do
  230. local sEvent, param, param2, param3 = os.pullEvent()
  231. if sEvent == "key" then
  232. if param == keys.up then
  233. if not bMenu then
  234. if nCompletion then
  235. nCompletion = nCompletion - 1
  236. if nCompletion < 1 then
  237. nCompletion = #tCompletions
  238. end
  239. redrawLine(y)
  240. elseif y > 1 then
  241. setCursor(math.min(x, #tLines[y - 1] + 1), y - 1)
  242. end
  243. end
  244.  
  245. elseif param == keys.down then
  246. if not bMenu then
  247. if nCompletion then
  248. nCompletion = nCompletion + 1
  249. if nCompletion > #tCompletions then
  250. nCompletion = 1
  251. end
  252. redrawLine(y)
  253. elseif y < #tLines then
  254. setCursor(math.min(x, #tLines[y + 1] + 1), y + 1)
  255. end
  256. end
  257.  
  258. elseif param == keys.tab then
  259. if not bMenu and not bReadOnly then
  260. if nCompletion and x == #tLines[y] + 1 then
  261. acceptCompletion()
  262. else
  263. local sLine = tLines[y]
  264. tLines[y] = string.sub(sLine, 1, x - 1) .. " " .. string.sub(sLine, x)
  265. setCursor(x + 4, y)
  266. end
  267. end
  268.  
  269. elseif param == keys.pageUp then
  270. if not bMenu then
  271. local newY = y - (h - 1)
  272. if newY < 1 then newY = 1 end
  273. setCursor(math.min(x, #tLines[newY] + 1), newY)
  274. end
  275.  
  276. elseif param == keys.pageDown then
  277. if not bMenu then
  278. local newY = y + (h - 1)
  279. if newY > #tLines then newY = #tLines end
  280. local newX = math.min(x, #tLines[newY] + 1)
  281. setCursor(newX, newY)
  282. end
  283.  
  284. elseif param == keys.home then
  285. if not bMenu then
  286. setCursor(1, y)
  287. end
  288.  
  289. elseif param == keys.end then
  290. if not bMenu then
  291. setCursor(#tLines[y] + 1, y)
  292. end
  293.  
  294. elseif param == keys.delete then
  295. if not bMenu and not bReadOnly then
  296. local sLine = tLines[y]
  297. tLines[y] = string.sub(sLine, 1, x - 1) .. string.sub(sLine, x + 1)
  298. redrawLine(y)
  299. end
  300.  
  301. elseif param == keys.backspace then
  302. if not bMenu and not bReadOnly then
  303. if x > 1 then
  304. local sLine = tLines[y]
  305. tLines[y] = string.sub(sLine, 1, x - 2) .. string.sub(sLine, x)
  306. setCursor(x - 1, y)
  307. end
  308. end
  309.  
  310. elseif param == keys.enter then
  311. if not bMenu and not bReadOnly then
  312. table.insert(tLines, y + 1, "")
  313. setCursor(1, y + 1)
  314. end
  315.  
  316. elseif param == keys.left then
  317. if not bMenu then
  318. if x > 1 then
  319. setCursor(x - 1, y)
  320. end
  321. end
  322.  
  323. elseif param == keys.right then
  324. if not bMenu then
  325. if x < #tLines[y] + 1 then
  326. setCursor(x + 1, y)
  327. end
  328. end
  329.  
  330. elseif param == keys.f1 then
  331. if not bMenu then
  332. bMenu = true
  333. term.setCursorBlink(false)
  334. end
  335.  
  336. elseif param == keys.f2 then
  337. if bMenu then
  338. doMenuItem(1)
  339. end
  340.  
  341. elseif param == keys.f3 then
  342. if bMenu then
  343. doMenuItem(2)
  344. end
  345.  
  346. elseif param == keys.f4 then
  347. if bMenu then
  348. doMenuItem(3)
  349. end
  350.  
  351. elseif param == keys.f5 then
  352. if bMenu then
  353. doMenuItem(4)
  354. end
  355. end
  356.  
  357. elseif sEvent == "mouse_click" then
  358. if bMenu then
  359. -- Handle menu clicks
  360. else
  361. -- Handle text area clicks
  362. end
  363.  
  364. elseif sEvent == "mouse_scroll" then
  365. -- Handle mouse scroll
  366. elseif sEvent == "term_resize" then
  367. -- Handle terminal resize
  368. end
  369. end
  370.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement