Advertisement
AdditionalPylons

ComputerCraft OS

Mar 18th, 2024 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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, press to continue")
  102.                     os.pullEvent("mouse_click")
  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 sel ~= nil then
  111.                                 local x,y = render_popup(7,sel,true)
  112.                                 if not fs.isDir(fs.combine(folder,sel)) then
  113.                                     writepos(x,y,"\aExecute")
  114.                                     writepos(x,y+1,"\aEdit")
  115.                                 else
  116.                                     writepos(x,y,"(Folder)")
  117.                                     writepos(x,y+1,"(No op.)")
  118.                                 end
  119.                                 writepos(x,y+2,"\aRename")
  120.                                 writepos(x,y+3,"\aDelete")
  121.                                 writepos(x,y+4,"\aCopy as..")
  122.                                 writepos(x,y+5,"\aMove to..")
  123.                                 local e, btn, cx, cy
  124.                                 repeat
  125.                                     e, btn, cx, cy = os.pullEvent("mouse_click")
  126.                                 until cy>=y-1 and cy<=y+4
  127.                                 if cy==y and not fs.isDir(fs.combine(folder,sel)) then
  128.                                     term.clear()
  129.                                     render_folder(folder,shift)
  130.                                     fill_color(1,h,w-1,h,colors.gray)
  131.                                     term.setBackgroundColor(colors.gray)
  132.                                     term.setCursorPos(1,h)
  133.                                     shell.setDir(folder)
  134.                                     shell.run(sel)
  135.                                     shell.setDir("/")
  136.                                     term.setCursorPos(1,h)
  137.                                     write("Execution finished, press to continue")
  138.                                     os.pullEvent("mouse_click")
  139.                                     term.setBackgroundColor(colors.black)
  140.                                     term.clear()
  141.                                 elseif cy==y+1 then
  142.                                     shell.run("edit "..fs.combine(folder,sel))
  143.                                 elseif cy==y+2 then
  144.                                     writecol(x,y+2,"\aRename",colors.cyan,colors.white)
  145.                                     term.setCursorPos(x,y+6)
  146.                                     write("> ")
  147.                                     local newname = read()
  148.                                     if newname ~= "" and newname ~= sel then
  149.                                         fs.copy(fs.combine(folder,sel),fs.combine(folder,newname))
  150.                                         fs.delete(fs.combine(folder,sel))
  151.                                     end
  152.                                 elseif cy==y+3 then
  153.                                     writecol(x,y+3,"\aDelete",colors.cyan,colors.white)
  154.                                     term.setCursorPos(x,y+6)
  155.                                     write("Sure? Y/N ")
  156.                                     if read() == "Y" then
  157.                                         fs.delete(fs.combine(folder,sel))
  158.                                     end
  159.                                 elseif cy==y+4 then
  160.                                     writecol(x,y+4,"\aCopy as..",colors.cyan,colors.white)
  161.                                     term.setCursorPos(x,y+6)
  162.                                     write("> ")
  163.                                     local newname = read()
  164.                                     if newname ~= "" and newname ~= sel then
  165.                                         fs.copy(fs.combine(folder,sel),fs.combine(folder,newname))
  166.                                     end
  167.                                 elseif cy==y+5 then
  168.                                     writecol(x,y+5,"\aMove to..",colors.cyan,colors.white)
  169.                                     term.setCursorPos(x,y+6)
  170.                                     write("> ")
  171.                                     local newname = read()
  172.                                     if newname ~= "" and fs.combine(folder,newname) ~= fs.combine(folder,sel) then
  173.                                         fs.copy(fs.combine(folder,sel),fs.combine(folder,newname,sel))
  174.                                         fs.delete(fs.combine(folder,sel))
  175.                                     end
  176.                                 end
  177.                                 term.clear()
  178.                             end
  179.                         end
  180.                     else
  181.                         last_click = {event[3],event[4]}
  182.                     end
  183.                 end
  184.             end
  185.         elseif event[1] == "mouse_scroll" then
  186.             shift = max(shift+event[2],1)
  187.             if shift+h-3>#fs.list(folder) then shift = max(#fs.list(folder)-h+3,1) end
  188.         elseif event[1] == "term_resize" or event[1] == "disk_eject" or event[1] == "disk" then
  189.             term.clear()
  190.         end
  191.     end
  192. end
  193.  
  194. local success, msg = pcall(main)
  195. if not success then blue_screen(msg) end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement