Advertisement
AdditionalPylons

ComputerCraft OS

Feb 9th, 2024 (edited)
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.15 KB | Source Code | 0 0
  1. term.clear()
  2. writepos, writecol, multchar, max, min, compare_coord, fill_color, render_folder, render_popup, render_time = table.unpack(require "os_library")
  3.  
  4. local function blue_screen(msg)
  5.     local w,h = term.getSize()
  6.     term.setBackgroundColor(colors.blue)
  7.     term.setTextColor(colors.white)
  8.     term.clear()
  9.     writepos(2,2,":( An unexpected error has occurred!")
  10.     writepos(2,3,"We are working on fixing the problem.")
  11.     writepos(2,5,"Error: "..msg)
  12.  
  13.     writepos(2,7,"\aOpen shell")
  14.     writepos(2,8,"\aRestart OS")
  15.     writepos(2,9,"\aShutdown")
  16.     local e, btn, cx, cy
  17.     repeat
  18.         e, btn, cx, cy = os.pullEvent("mouse_click")
  19.     until cy>=7 and cy<=9
  20.  
  21.     term.setCursorPos(1,h)
  22.     if cy==7 then
  23.         term.setBackgroundColor(colors.black)
  24.         term.clearLine()
  25.     elseif cy==8 then
  26.         shell.run("reboot")
  27.     elseif cy==9 then
  28.         shell.run("shutdown")
  29.     end
  30. end
  31.  
  32. local function is_lua_script(name)
  33.     local afterdot = string.match(name, "%.(.*)")
  34.     return afterdot == "lua" or afterdot == nil
  35. end
  36. local args = {...}
  37. local folder = args[1]==nil and "" or args[1]
  38. local shift = 1
  39. local last_click = {0,0}
  40.  
  41. local function main()
  42.     while true do
  43.         render_folder(folder,shift)
  44.         local event = {os.pullEvent()}
  45.         local w,h = term.getSize()
  46.         if event[1] == "timer" then
  47.             render_time()
  48.         elseif event[1] == "mouse_click" then
  49.             if event[2] == 1 then
  50.                 if event[3] == w then
  51.                     if event[4] == h then
  52.                         shift = min(shift + 1,#fs.list(folder)-h+3)
  53.                     elseif event[4] == 1 then
  54.                         shift = max(shift-1,1)
  55.                     elseif event[4] == 3 and term.isColour() then
  56.                         shell.run("fg shell")
  57.                     elseif event[4] == math.floor(h/2) then
  58.                         if folder~="" then folder = fs.combine(folder,"..") end
  59.                         shift = 1
  60.                         term.clear()
  61.                     elseif event[4] == math.floor(h/2)-1 then
  62.                         folder = "/UserFiles"
  63.                         shift = 1
  64.                         term.clear()
  65.                     elseif event[4] == 2 then
  66.                         shift = 1
  67.                     elseif event[4] == h-1 then
  68.                         shift = max(#fs.list(folder)-h+3,1)
  69.                     elseif event[4] == h-2 then
  70.                         local x,y = render_popup(3,"Create",true)
  71.                         local is_folder = false
  72.                         writepos(x,y,"\aFile")
  73.                         writepos(x,y+1,"\aFolder")
  74.                         local e, btn, cx, cy
  75.                         repeat
  76.                             e, btn, cx, cy = os.pullEvent("mouse_click")
  77.                         until cy == y or cy == y+1 or cy == y-1
  78.                         if cy == y then
  79.                             writecol(x,y,"\aFile",colors.cyan,colors.white)
  80.                         elseif cy == y+1 then
  81.                             writecol(x,y+1,"\aFolder",colors.cyan,colors.white)
  82.                             is_folder = true
  83.                         end
  84.                         if cy ~= y-1 then
  85.                             term.setCursorPos(x,y+2)
  86.                             write("> ")
  87.                             local rd = read()
  88.                             if not is_folder and rd~="" then fs.open(fs.combine(folder,rd),"w").close() else fs.makeDir(fs.combine(folder,rd)) end
  89.                         end
  90.                         term.clear()
  91.                     end
  92.                 elseif compare_coord({event[3],event[4]},{1,h}) then
  93.                     term.setCursorPos(1,h)
  94.                     term.setBackgroundColor(colors.gray)
  95.                     term.clearLine()
  96.                     write("> ")
  97.                     shell.setDir(folder)
  98.                     shell.run(read())
  99.                     shell.setDir("/")
  100.                     term.setCursorPos(1,h)
  101.                     write("Execution finished, any key to continue")
  102.                     os.pullEvent("key")
  103.                     term.setBackgroundColor(colors.black)
  104.                     term.clear()
  105.                 else
  106.                     if compare_coord({event[3],event[4]},last_click) then
  107.                         last_click = {0,0}
  108.                         local sel = fs.list(folder)[event[4]+shift-2]
  109.                         if sel ~= nil and event[4] ~= 1 and event[4] ~= h then
  110.                             if fs.isDir(fs.combine(folder,sel)) then
  111.                                 folder = fs.combine(folder,sel)
  112.                                 shift = 1
  113.                             else
  114.                                 if is_lua_script(sel) then
  115.                                     term.setBackgroundColor(colors.gray)
  116.                                     paintutils.drawLine(1,h,w,h,colors.gray)
  117.                                     term.setCursorPos(1,h)
  118.                                     shell.run(fs.combine(folder,sel))
  119.                                     shell.setDir("/")
  120.                                     term.setBackgroundColor(colors.gray)
  121.                                     paintutils.drawLine(1,h,w,h,colors.gray)
  122.                                     term.setCursorPos(1,h)
  123.                                     write("Execution finished, any key to continue")
  124.                                     os.pullEvent("key")
  125.                                     term.setBackgroundColor(colors.black)
  126.                                 else
  127.                                     shell.run("edit "..fs.combine(folder,sel))
  128.                                 end
  129.                             end
  130.                             term.clear()
  131.                         end
  132.                     else
  133.                         last_click = {event[3],event[4]}
  134.                     end
  135.                 end
  136.             elseif event[2] == 2 then
  137.                 if event[3] ~= w and event[4] ~= 1 and event[4] ~= h then
  138.                     local sel = fs.list(folder)[event[4]+shift-2]
  139.                     if sel ~= nil then
  140.                         local x,y = render_popup(6,sel,true)
  141.                         if not fs.isDir(fs.combine(folder,sel)) then
  142.                             if not is_lua_script(sel) then writepos(x,y,"\aExecute")
  143.                             else writepos(x,y,"\aEdit") end
  144.                         else writepos(x,y,"(Folder)") end
  145.                         writepos(x,y+1,"\aRename")
  146.                         writepos(x,y+2,"\aDelete")
  147.                         writepos(x,y+3,"\aCopy as..")
  148.                         writepos(x,y+4,"\aMove to..")
  149.                         local e, btn, cx, cy
  150.                         repeat
  151.                             e, btn, cx, cy = os.pullEvent("mouse_click")
  152.                         until cy>=y-1 and cy<=y+4
  153.                         if cy==y and not fs.isDir(fs.combine(folder,sel)) then
  154.                             if not is_lua_script(sel) then
  155.                                 term.clear()
  156.                                 render_folder(folder,shift)
  157.                                 fill_color(1,h,w-1,h,colors.gray)
  158.                                 term.setBackgroundColor(colors.gray)
  159.                                 term.setCursorPos(1,h)
  160.                                 shell.setDir(folder)
  161.                                 shell.run(sel)
  162.                                 shell.setDir("/")
  163.                                 term.setCursorPos(1,h)
  164.                                 write("Execution finished, any key to continue")
  165.                                 os.pullEvent("key")
  166.                                 term.setBackgroundColor(colors.black)
  167.                             else
  168.                                 shell.run("edit "..fs.combine(folder,sel))
  169.                             end
  170.                             term.clear()
  171.                         elseif cy==y+1 then
  172.                             writecol(x,y+1,"\aRename",colors.cyan,colors.white)
  173.                             term.setCursorPos(x,y+5)
  174.                             write("> ")
  175.                             local newname = read()
  176.                             if newname ~= "" and newname ~= sel then
  177.                                 fs.copy(fs.combine(folder,sel),fs.combine(folder,newname))
  178.                                 fs.delete(fs.combine(folder,sel))
  179.                             end
  180.                         elseif cy==y+2 then
  181.                             writecol(x,y+2,"\aDelete",colors.cyan,colors.white)
  182.                             term.setCursorPos(x,y+5)
  183.                             write("Sure? Y/N ")
  184.                             if read() == "Y" then
  185.                                 fs.delete(fs.combine(folder,sel))
  186.                             end
  187.                         elseif cy==y+3 then
  188.                             writecol(x,y+3,"\aCopy as..",colors.cyan,colors.white)
  189.                             term.setCursorPos(x,y+5)
  190.                             write("> ")
  191.                             local newname = read()
  192.                             if newname ~= "" and newname ~= sel then
  193.                                 fs.copy(fs.combine(folder,sel),fs.combine(folder,newname))
  194.                             end
  195.                         elseif cy==y+4 then
  196.                             writecol(x,y+4,"\aMove to..",colors.cyan,colors.white)
  197.                             term.setCursorPos(x,y+5)
  198.                             write("> ")
  199.                             local newname = read()
  200.                             if newname ~= "" and fs.combine(folder,newname) ~= fs.combine(folder,sel) then
  201.                                 fs.copy(fs.combine(folder,sel),fs.combine(folder,newname,sel))
  202.                                 fs.delete(fs.combine(folder,sel))
  203.                             end
  204.                         end
  205.                         term.clear()
  206.                     end
  207.                 end
  208.             end
  209.         elseif event[1] == "mouse_scroll" then
  210.             shift = max(shift+event[2],1)
  211.             if shift+h-3>#fs.list(folder) then shift = max(#fs.list(folder)-h+3,1) end
  212.             --if #fs.list(folder)>h and shift>#fs.list(folder)-h+3 then shift = #fs.list(folder)-h+3 end
  213.         elseif event[1] == "term_resize" or event[1] == "disk_eject" or event[1] == "disk" then
  214.             term.clear()
  215.         end
  216.     end
  217. end
  218.  
  219. local success, msg = pcall(main)
  220. if not success then blue_screen(msg) end
Tags: lua
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement