Advertisement
Corona

[Computercraft] Explorer

Feb 3rd, 2014
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 26.00 KB | None | 0 0
  1. -- (C) 2014 Viktor alias Coronaxe. no rights reserved, but please mind the creator's credits anyways!
  2.  
  3. local timezone = "utc"
  4. local timeoffset = "+1"
  5.  
  6. local ver = 0.50
  7. local apiver = 0.36
  8.  
  9. local screenAPI = "uRpaiUB3"
  10. local rvercode = "axgaywWj"
  11. local rexplorercode = "SCv9r7y7"
  12.  
  13. local args = {...}
  14.  
  15. --This code checks for my screen API
  16. if not fs.exists("/apis/screen") then
  17.   if not http then
  18.     error("Warning! Screen API not present and HTTP API not enabled! Please enable HTTP API in computercraft.cfg, or place the latest Version of the screen API as \"/apis/screen\"!")
  19.   end
  20.   print("Setting up apis for use, please stand by...")
  21.   sleep(1)
  22.   print("Getting API...")
  23.   local response = http.get("http://pastebin.com/raw.php?i="..screenAPI)
  24.   print("Saving API to disk...")
  25.   local sResponse = response.readAll()
  26.   fs.makeDir("/apis")
  27.   local fh = fs.open("/apis/screen" ,"w")
  28.   fh.write(tostring(sResponse))
  29.   fh.close()
  30.   print("Finished!")
  31.   print("")
  32.   print("This bit of code has been contributed by Thib0704! Thank you, Thib!")
  33.   sleep(2)
  34. end
  35. --code has been contributed by Thib0704 @ computercraft.info!
  36.  
  37. --following code checks if there are updates
  38. if http then
  39.   print("checking for updates...")
  40.   local rver = tonumber(http.get("http://pastebin.com/raw.php?i="..rvercode).readAll())
  41.   if rver ~= nil and rver > ver then
  42.     print("Found update! Beginning update process... Please do not terminate the program while updating!")
  43.     sleep(1)
  44.     fs.delete(shell.getRunningProgram())
  45.     print("Getting new version "..rver.."...")
  46.     local nv = http.get("http://pastebin.com/raw.php?i="..rexplorercode).readAll()
  47.     local fh = fs.open(shell.getRunningProgram(),"w")
  48.     fh.write(tostring(nv))
  49.     fh.close()
  50.     print("Done! Version Updated to "..rver..". The Computer will restart now")
  51.     sleep(3)
  52.     os.reboot()
  53.   end
  54. else
  55.   print("HTTP Not enabled. Weren't able to update!")
  56.   sleep(0.5)
  57. end
  58. --eoc
  59.  
  60. os.loadAPI("/apis/screen")
  61.  
  62. local executing = false
  63. if clipboard == nil then
  64.   clipboard = nil
  65. end
  66.  
  67. if screen.version() < apiver then
  68.   error("Your Version of the screen API is too old. Your Version: "..screen.version()..", Least Required Version: "..apiver..". Please restart this application to redownload the API")
  69.   fs.delete("/apis/screen")
  70. end
  71.  
  72. function quit()
  73.   screen.cls()
  74.   print("Thank you for using Explorer Version "..ver.."!")
  75.   --os.unloadAPI("screen")
  76.   error()
  77. end
  78.  
  79. function resolve(path)
  80.   if string.sub(path,1,1) == "/" then
  81.     return path
  82.   else
  83.     return "/"..path
  84.   end
  85. end
  86.  
  87. function cd(path)
  88.   local rPath = path
  89.   if fs.exists(rPath) then
  90.     local bPath = rPath
  91.     shell.setDir(bPath)
  92.     return true
  93.   else
  94.     return false
  95.   end
  96. end
  97.  
  98. function createFile(fPath)
  99.   local fh = fs.open(fPath,"w")
  100.   fh.close()
  101. end
  102.  
  103. function exec(path,parameters)
  104.   local rPath = path
  105.   --print(rPath)
  106.   if fs.exists(rPath) and not fs.isDir(rPath) then
  107.     local cString = rPath.." "..parameters
  108.     shell.run(cString)
  109.     return true
  110.   else
  111.     return false
  112.   end
  113. end
  114.  
  115.  
  116.  
  117. function list(path)
  118.   local rPath = path
  119.   if fs.exists(rPath) then
  120.     local fTable = fs.list(rPath)
  121.     if fTable ~= nil then
  122.       table.sort(fTable)
  123.       local tFiles = {}
  124.       local tFilesPath = {}
  125.       local tDirs = {}
  126.       local tDirsPath = {}
  127.       local tDirAndFiles = {}
  128.       for n, sItem in pairs( fTable ) do
  129.         if string.sub( sItem, 1, 1 ) ~= "." then
  130.         local sPath = fs.combine( rPath, sItem )
  131.         sPath = "/"..sPath
  132.           if fs.isDir( sPath ) then
  133.             table.insert( tDirs, sItem )
  134.             table.insert( tDirsPath, sPath )
  135.           else
  136.             table.insert( tFiles, sItem )
  137.             table.insert( tFilesPath, sPath )
  138.           end
  139.         end
  140.       end
  141.       table.sort( tDirs )
  142.       table.sort( tFiles )
  143.       tDirAndFiles.directories = tDirs
  144.       tDirAndFiles.dirpaths = tDirsPath
  145.       tDirAndFiles.files = tFiles
  146.       tDirAndFiles.filepaths = tFilesPath
  147.       tDirAndFiles.all = {}
  148.       tDirAndFiles.allpaths = {}
  149.       for i=1, #tDirsPath do
  150.         table.insert(tDirAndFiles.all,fs.getName(tDirsPath[i]))
  151.         table.insert(tDirAndFiles.allpaths,tDirsPath[i])
  152.       end
  153.       for i=1, #tFilesPath do
  154.         table.insert(tDirAndFiles.all,fs.getName(tFilesPath[i]))
  155.         table.insert(tDirAndFiles.allpaths,tFilesPath[i])
  156.       end
  157.       return tDirAndFiles
  158.     else
  159.       print(shell.dir().." is not a directory!")
  160.       return false
  161.     end
  162.   else
  163.     print("invalid path!")
  164.     return false
  165.   end
  166. end
  167.  
  168. function drawFiles(tDat,offset,highlight) --please only use absolute filepaths!
  169.   local bckstr = ".."
  170.   print("Currently in: "..resolve(shell.dir()))
  171.   local x,y = term.getCursorPos()
  172.   local btnBack = {x1 = 1, x2 = #bckstr, y1 = y, y2 = y, path = shell.resolve(".."), name = ".."}
  173.   local drawnCoords = {}
  174.   if resolve(shell.dir()) ~= "/" then
  175.     table.insert(drawnCoords,btnBack)
  176.     local tx,ty = term.getCursorPos()
  177.     term.setBackgroundColor(colors.lime)
  178.     write(bckstr)
  179.     term.setCursorPos(tx,ty+1)
  180.   else
  181.     highlight = highlight + 1
  182.   end
  183.   for i=offset, #tDat do
  184.     local tcpx, tcpy = term.getCursorPos()
  185.     if fs.isDir(tDat[i]) then
  186.       if i==highlight then
  187.         term.setBackgroundColor(colors.purple)
  188.       else
  189.         term.setBackgroundColor(colors.blue)
  190.       end
  191.     else
  192.       if i==highlight then
  193.         term.setBackgroundColor(colors.orange)
  194.       else
  195.         term.setBackgroundColor(colors.red)
  196.       end
  197.     end
  198.     write(fs.getName(tDat[i]))
  199.     local temTable = {x1 = tcpx, x2 = tcpx + #fs.getName(tDat[i])-1, y1 = tcpy, y2 = tcpy, path = tDat[i], name = fs.getName(tDat[i])}
  200.     table.insert(drawnCoords,temTable)
  201.     term.setCursorPos(tcpx,tcpy+1)
  202.   end
  203.   return drawnCoords
  204. end
  205.  
  206. function options()
  207.  
  208. end
  209.  
  210. function drawFileMenu(referrer,x,y,pd,nfm)
  211.   local btnWidth = 7
  212.   local fileBtn = {}
  213.   local dirBtn = {}
  214.  
  215.   local tx,ty = term.getSize()
  216.  
  217.   local msgRun = "run "
  218.   local msgEdit = "edit"
  219.   local msgOpen = "open"
  220.   local msgCopy = "copy"
  221.   local msgPaste = "pste"
  222.   local msgRename = "rnme"
  223.   local msgDelete = "del "
  224.  
  225.   local btnRun = {x1 = x, x2 = x+btnWidth, y1 = y, y2 = y, text = msgRun, color = colors.blue, exec = function(referrer) if referrer.path~=resolve(shell.getRunningProgram()) then executing=true shell.run("clear") shell.run(referrer.path) executing=false sleep(1.5) end end}
  226.   local btnEdit = {x1 = x, x2 = x+btnWidth, y1 = y, y2 = y, text = msgEdit, color = colors.blue, exec = function(referrer) executing=true shell.run("edit "..referrer.path) executing=false end}
  227.   local btnOpen = {x1 = x, x2 = x+btnWidth, y1 = y, y2 = y, text = msgOpen, color = colors.blue, exec = function(referrer) cd(referrer.path) end}
  228.   local btnCopy = {x1 = x, x2 = x+btnWidth, y1 = y, y2 = y, text = msgCopy, color = colors.blue, exec = function(referrer) if resolve(shell.getRunningProgram())~=referrer.path and resolve(referrer.path)~="/" then clipboard = referrer.path end end}
  229.   local btnPaste = {x1 = x, x2 = x+btnWidth, y1 = y, y2 = y, text = msgPaste, color = colors.blue, exec = function(referrer) local clipboardto=clipboard local ex=true if clipboard~=nil and fs.exists(clipboard) then while ex do if fs.exists(shell.dir().."/"..fs.getName(clipboardto)) then clipboardto=clipboardto.."_copy" else ex=false end end fs.copy(clipboard,shell.dir().."/"..fs.getName(clipboardto)) else clipboard=nil end end}
  230.   local btnRename = {x1 = x, x2 = x+btnWidth, y1 = y, y2 = y, text = msgRename, color = colors.blue, exec = function(referrer) local sx,sy = term.getSize() local evn,eparam,x,y local info = "New Name?" local arInput = {x1 = sx/2-14,x2 = sx/2+14,y1 = sy/2-3,y2 = sy/2,color = colors.lightBlue,pos = 1,data = {}} local btnCancel = {x1 = sx/2-14,x2 = sx/2-5,y1 = sy/2+1,y2 = sy/2+4,text = "Cancel",color = colors.red} local btnConfirm = {x1 = sx/2+5,x2 = sx/2+14,y1 = sy/2+1,y2 = sy/2+4,text = "Confirm",color = colors.lime} term.setCursorPos(sx/2-#info/2,sy/2-5) print(info) screen.drawBox(arInput) screen.drawButton(btnCancel,true) screen.drawButton(btnConfirm,true) screen.getFocus(arInput) term.setCursorBlink(true) for i=1,string.len(referrer.name) do arInput.data[i] = string.sub(referrer.name,i,i) arInput.pos = arInput.pos+1 end repeat screen.getFocus(arInput) for i=1,arInput.x2-arInput.x1-2 do term.setBackgroundColor(colors.black) write(" ") end screen.getFocus(arInput) for i=1,#arInput.data do term.setBackgroundColor(colors.black) write(arInput.data[i]) end evn,eparam,x,y = os.pullEvent() if evn == "char" and arInput.pos<=arInput.x2-arInput.x1-2 then arInput.data[arInput.pos] = eparam arInput.pos = arInput.pos+1 elseif evn == "key" and eparam == 14 and arInput.pos>1 then arInput.pos = arInput.pos-1 arInput.data[arInput.pos] = nil end until (evn == "mouse_click") or (evn == "key" and eparam == 28) local sFile = "" for i=1,#arInput.data do sFile = sFile..arInput.data[i] end if (sFile ~= nil) and (evn == "key" and eparam == 28) or (evn == "mouse_click" and screen.pressed(btnConfirm,x,y)) then if not fs.exists(shell.dir().."/"..sFile) and not fs.isReadOnly(shell.dir().."/"..sFile) then fs.move(referrer.path,shell.dir().."/"..sFile) else screen.cls() print("An Error Occurred. File Exists or Folder is Readonly.") sleep(1.5) end end term.setCursorBlink(false) end}
  231.   local btnDelete = {x1 = x, x2 = x+btnWidth, y1 = y, y2 = y, text = msgDelete, color = colors.blue, exec = function(referrer) local warning="REALLY DELETE "..referrer.name.."?" local xt,yt = term.getSize() term.setCursorPos(xt/2-#warning/2,yt/2-6) term.write(warning) local yes={x1=xt/2-4,x2=xt/2+4,y1=yt/2-4,y2=yt/2-1,text="YES",color=colors.red} local no={x1=xt/2-4,x2=xt/2+4,y1=yt/2+1,y2=yt/2+4,text="NO",color=colors.red} local accessDenied={x1=xt/2-10,x2=xt/2+10,y1=yt/2-5,y2=yt/2+5,text="ACCESS DENIED",color=colors.red} screen.drawButton(yes,true) screen.drawButton(no,true) local evn,btn,px,py = os.pullEvent("mouse_click") if btn==1 and screen.pressed(yes,px,py) then if not fs.isReadOnly(referrer.path) and referrer.path ~= shell.getRunningProgram() then fs.delete(referrer.path) else screen.drawButton(accessDenied,true) sleep(1.5) end end end}
  232.  
  233.   if not pd and nfm then
  234.     fileBtn = {}
  235.     dirBtn = {btnCopy}
  236.   elseif pd and nfm then
  237.     fileBtn = {}
  238.     dirBtn = {btnCopy,btnPaste}
  239.   else
  240.     if pd then
  241.       --print("True")
  242.       --sleep(3)
  243.       fileBtn = {btnRun, btnEdit, btnCopy, btnPaste, btnRename, btnDelete}
  244.       dirBtn = {btnOpen, btnCopy, btnPaste, btnRename, btnDelete}
  245.     else
  246.       --print("false")
  247.       --sleep(3)
  248.       fileBtn = {btnRun, btnEdit, btnCopy, btnRename, btnDelete}
  249.       dirBtn = {btnOpen, btnCopy, btnRename, btnDelete}
  250.     end
  251.   end
  252.   if (ty-#fileBtn<=y-2 and not fs.isDir(referrer.path)) or (ty-#dirBtn<=y-2 and fs.isDir(referrer.path)) then
  253.     if not fs.isDir(referrer.path) then
  254.       for i=1,#fileBtn do
  255.         fileBtn[i].y1 = y-i+1
  256.         fileBtn[i].y2 = y-i+1
  257.       end
  258.     else
  259.       for i=1,#dirBtn do
  260.         dirBtn[i].y1 = y-i+1
  261.         dirBtn[i].y2 = y-i+1
  262.       end
  263.     end
  264.   else
  265.     if not fs.isDir(referrer.path) then
  266.       for i=1,#fileBtn do
  267.         fileBtn[i].y1 = y+i-1
  268.         fileBtn[i].y2 = y+i-1
  269.       end
  270.     else
  271.       for i=1,#dirBtn do
  272.         dirBtn[i].y1 = y+i-1
  273.         dirBtn[i].y2 = y+i-1
  274.       end
  275.     end
  276.   end
  277.  
  278.   if fs.isDir(referrer.path) then
  279.     for i=1,#dirBtn do
  280.       screen.drawButton(dirBtn[i],true)
  281.     end
  282.   return dirBtn
  283.   else
  284.     for i=1,#fileBtn do
  285.       screen.drawButton(fileBtn[i],true)
  286.     end
  287.   return fileBtn
  288.   end
  289. end
  290.  
  291. function drawTaskBar(deployed, dtype)
  292.   local tx,ty = term.getSize()
  293.   local allbtns = {}
  294.   local menubtns = {}
  295.   local newbtns = {}
  296.   local switchbtns = {}
  297.  
  298.   local barBackground = {x1 = 1, x2 = 45, y1 = 1, y2 = 1, color = colors.gray}
  299.   screen.drawButton(barBackground)
  300.  
  301.   local msgMenu = "^MENU^ "
  302.   local msgQuit = " Quit  "
  303.   local msgShutdown = "Shutdwn"
  304.   local msgRestart = "Restart"
  305.   local msgOptions = "Options"
  306.   local btnMenu = {x1 = 1, x2 = #msgMenu, y1 = 1, y2 = 1, text = msgMenu, color = colors.gray, textcolor = colors.yellow, altTextColor = colors.lime, altText = "vMENUv ", exec = function() return "MENU" end}
  307.   local btnQuit = {x1 = 1, x2 = #msgQuit, y1 = 1, y2 = 1, text = msgQuit, color = colors.gray, textcolor = colors.lime, exec = function() screen.cls() quit() end}
  308.   local btnShutdown = {x1 = 1, x2 = #msgShutdown, y1 = 1, y2 = 1, text = msgShutdown, color = colors.gray, textcolor = colors.lime, exec = function() os.shutdown() end}
  309.   local btnRestart = {x1 = 1, x2 = #msgRestart, y1 = 1, y2 = 1, text = msgRestart, color = colors.gray, textcolor = colors.lime, exec = function() os.reboot() end}
  310.   local btnOptions = {x1 = 1, x2 = #msgOptions, y1 = 1, y2 = 1, text = msgOptions, color = colors.gray, textcolor = colors.lime, exec = function() end}
  311.   menubtns = {btnMenu, btnQuit, btnShutdown, btnRestart, btnOptions}
  312.  
  313.   for i=2,#menubtns do
  314.     menubtns[i].y1 = i
  315.     menubtns[i].y2 = i
  316.   end
  317.  
  318.   local separator = {x1 = btnMenu.x2 + 1, x2 = btnMenu.x2 + 1, y1 = 1, y2 = 1, text = "|", color = colors.gray, textcolor = colors.orange}
  319.  
  320.   local msgNew = " ^NEW^ "
  321.   local msgFile = "  File "
  322.   local msgFolder = " Folder"
  323.   local btnNew = {x1 = btnMenu.x2 + 2, x2 = btnMenu.x2 + 2 + #msgNew, y1 = 1, y2 = 1, text = msgNew, color = colors.gray, textcolor = colors.yellow, altTextColor = colors.lime, altText = " vNEWv ", exec = function() return "NEW" end}
  324.   local btnFile = {x1 = btnMenu.x2 + 2, x2 = btnMenu.x2 + 2 + #msgFolder, y1 = 1, y2 = 1, text = msgFile, color = colors.gray, textcolor = colors.lime, exec = function() local sx,sy = term.getSize() local evn,eparam,x,y local info = "New File" local arInput = {x1 = sx/2-14,x2 = sx/2+14,y1 = sy/2-3,y2 = sy/2,color = colors.lightBlue,pos = 1,data = {}} local btnCancel = {x1 = sx/2-14,x2 = sx/2-5,y1 = sy/2+1,y2 = sy/2+4,text = "Cancel",color = colors.red} local btnConfirm = {x1 = sx/2+5,x2 = sx/2+14,y1 = sy/2+1,y2 = sy/2+4,text = "Confirm",color = colors.lime} term.setCursorPos(sx/2-#info/2,sy/2-5) print(info) screen.drawBox(arInput) screen.drawButton(btnCancel,true) screen.drawButton(btnConfirm,true) screen.getFocus(arInput) term.setCursorBlink(true) repeat evn,eparam,x,y = os.pullEvent() if evn == "char" and arInput.pos<=arInput.x2-arInput.x1-2 then arInput.data[arInput.pos] = eparam arInput.pos = arInput.pos+1 elseif evn == "key" and eparam == 14 and arInput.pos>1 then arInput.pos = arInput.pos-1 arInput.data[arInput.pos] = nil end screen.getFocus(arInput) for i=1,arInput.x2-arInput.x1-2 do term.setBackgroundColor(colors.black) write(" ") end screen.getFocus(arInput) for i=1,#arInput.data do term.setBackgroundColor(colors.black) write(arInput.data[i]) end until (evn == "mouse_click") or (evn == "key" and eparam == 28) local sFile = "" for i=1,#arInput.data do sFile = sFile..arInput.data[i] end if (sFile ~= nil) and (evn == "key" and eparam == 28) or (evn == "mouse_click" and screen.pressed(btnConfirm,x,y)) then if not fs.exists(shell.dir().."/"..sFile) and not fs.isReadOnly(shell.dir().."/"..sFile) then createFile(shell.dir().."/"..sFile) else screen.cls() print("An Error Occurred. File Exists or Folder is Readonly.") sleep(1.5) end end term.setCursorBlink(false) end}
  325.   local btnFolder = {x1 = btnMenu.x2 + 2, x2 = btnMenu.x2 + 2 + #msgFolder, y1 = 1, y2 = 1, text = msgFolder, color = colors.gray, textcolor = colors.lime, exec = function() local sx,sy = term.getSize() local evn,eparam,x,y local info = "New Directory" local arInput = {x1 = sx/2-14,x2 = sx/2+14,y1 = sy/2-3,y2 = sy/2,color = colors.lightBlue,pos = 1,data = {}} local btnCancel = {x1 = sx/2-14,x2 = sx/2-5,y1 = sy/2+1,y2 = sy/2+4,text = "Cancel",color = colors.red} local btnConfirm = {x1 = sx/2+5,x2 = sx/2+14,y1 = sy/2+1,y2 = sy/2+4,text = "Confirm",color = colors.lime} term.setCursorPos(sx/2-#info/2,sy/2-5) print(info) screen.drawBox(arInput) screen.drawButton(btnCancel,true) screen.drawButton(btnConfirm,true) screen.getFocus(arInput) term.setCursorBlink(true) repeat evn,eparam,x,y = os.pullEvent() if evn == "char" and arInput.pos<=arInput.x2-arInput.x1-2 then arInput.data[arInput.pos] = eparam arInput.pos = arInput.pos+1 elseif evn == "key" and eparam == 14 and arInput.pos>1 then arInput.pos = arInput.pos-1 arInput.data[arInput.pos] = nil end screen.getFocus(arInput) for i=1,arInput.x2-arInput.x1-2 do term.setBackgroundColor(colors.black) write(" ") end screen.getFocus(arInput) for i=1,#arInput.data do term.setBackgroundColor(colors.black) write(arInput.data[i]) end until (evn == "mouse_click") or (evn == "key" and eparam == 28) local sFolder = "" for i=1,#arInput.data do sFolder = sFolder..arInput.data[i] end if (sFolder ~= nil) and (evn == "key" and eparam == 28) or (evn == "mouse_click" and screen.pressed(btnConfirm,x,y)) then if not fs.exists(shell.dir().."/"..sFolder) and not fs.isReadOnly(shell.dir().."/"..sFolder) then fs.makeDir(shell.dir().."/"..sFolder) else screen.cls() print("An Error Occurred. File Exists or Folder is Readonly.") sleep(1.5) end end term.setCursorBlink(false) end}
  326.   newbtns = {btnNew, btnFile, btnFolder}
  327.  
  328.   for i=2,#newbtns do
  329.     newbtns[i].y1 = i
  330.     newbtns[i].y2 = i
  331.   end
  332.  
  333.   local separator2 = {x1 = btnNew.x2 + 1, x2 = btnNew.x2 + 1, y1 = 1, y2 = 1, text = "|", color = colors.gray, textcolor = colors.orange}
  334.  
  335.   switchbtns = {btnMenu,btnNew}
  336.  
  337.   local btnFS = {x1 = tx-11,x2 = 7,y1 = 1,y2 = 1,text = math.floor(fs.getFreeSpace(shell.dir())/1024).."KB",textcolor = colors.lime, color = colors.gray}
  338.   if math.floor(fs.getFreeSpace(shell.dir())/1024)<500 then
  339.     btnFS.textcolor = colors.blue
  340.   end
  341.   if math.floor(fs.getFreeSpace(shell.dir())/1024)<300 then
  342.     btnFS.textcolor = colors.yellow
  343.   end
  344.   if math.floor(fs.getFreeSpace(shell.dir())/1024)<100 then
  345.     btnFS.textcolor = colors.red
  346.   end
  347.   if clipboard~=nil then
  348.     local btnClipboard = {x1 = separator2.x2+2, x2 = separator2.x2 + #fs.getName(clipboard) + 1, y1 = 1, y2 = 1, text = "CB: "..fs.getName(clipboard), textcolor = colors.red, color = colors.gray}
  349.     allbtns = {separator, separator2, btnMenu, btnNew, btnFS, btnClipboard}
  350.   else
  351.     allbtns = {separator, separator2, btnMenu, btnNew, btnFS}
  352.   end
  353.  
  354.   for i=1,#allbtns do
  355.     screen.drawButton(allbtns[i])
  356.   end
  357.  
  358.   if deployed and dtype == "MENU" then
  359.     btnMenu.text = btnMenu.altText
  360.     btnMenu.textcolor = btnMenu.altTextColor
  361.   for i=1,#menubtns do
  362.     screen.drawButton(menubtns[i])
  363.   end
  364.     return menubtns
  365.   elseif deployed and dtype == "NEW" then
  366.     btnNew.text = btnNew.altText
  367.     btnNew.textcolor = btnNew.altTextColor
  368.     for i=1,#newbtns do
  369.       screen.drawButton(newbtns[i])
  370.     end
  371.     return newbtns
  372.   else
  373.     return switchbtns
  374.   end
  375. end
  376.  
  377. function init()
  378.   screen.cls()
  379.   print("Explorer Version "..ver..", Program written by Coronaxe!")
  380.   print("")
  381.   print("o) Leftklick a File (red) or Folder (blue) to highlight it")
  382.   print("o) Rightklick a File (red) to open the context menu")
  383.   print("o) Leftklick a highlighted Folder (purple) to switch to it")
  384.   print("o) To Quit, please click on the \"x\" in the upper right!")
  385.   print("")
  386.   print("Please click anywhere to proceed.")
  387.   os.pullEvent("mouse_click")
  388. end
  389.  
  390. function usage()
  391.   print("Use \""..fs.getName(shell.getRunningProgram()).."\", to start the Explorer!")
  392.   print("Use \""..fs.getName(shell.getRunningProgram()).." help\", to print the Help Message!")
  393. end
  394.  
  395. function bsod(ermsg)
  396.     term.setBackgroundColor(colors.blue)
  397.     term.clear()
  398.     term.setCursorPos(1,1)
  399.     local tx,ty = term.getSize()
  400.    
  401.     local tText = {
  402.     "*!=#?!, an error has occurred!",
  403.     "",
  404.     "Detailed information:",
  405.     ermsg,
  406.     "If this is the first time",
  407.     "you see this, ignore it.",
  408.     "If this is not the first time",
  409.     "you see this error,",
  410.     "please wait for implementation",
  411.     "of an error reporter!",
  412.     }
  413.  
  414.     for i=1, #tText do
  415.         term.setCursorPos(tx/2-#tText[i]/2,ty/2-7+i)
  416.         print(tText[i])
  417.     end
  418. end
  419.  
  420. function showError(ermsg)
  421.   local mx,my = term.getSize()
  422.   local btnError = {
  423.   x1 = 3,
  424.   x2 = mx -3,
  425.   y1 = 3,
  426.   y2 = my -3,
  427.   color = colors.red
  428.   }
  429.   screen.drawButton(btnError)
  430.     local tText = {
  431.     "*!=#?!, an error has occurred!",
  432.     "",
  433.     "Detailed information:",
  434.     ermsg,
  435.   "Please report this error!"
  436.     }
  437.   term.setBackgroundColor(colors.red)
  438.     for i=1, #tText do
  439.         term.setCursorPos(mx/2-#tText[i]/2,4+i)
  440.         print(tText[i])
  441.     end
  442.   sleep(2.5)
  443. end
  444.  
  445. function main()
  446.   local soffset = 1
  447.   local active = 0
  448.   local pr = 0
  449.   local ca = false -- Context Menu Active Variable
  450.   local cx -- Context Menu last klicked X Coord
  451.   local cy -- Context Menu last klicked y Coord
  452.   local lItem -- last clicked Item
  453.   local ci -- ContectMenu Items
  454.   local dpb = false --draw the Paste Button
  455.   local nfm = false -- Variable for klicking on no File
  456.   local dtb = false --deploy taskbarbuttons
  457.   local dt = "" --either menu type or new type
  458.   local rv --returnvalue of the tb buttons
  459.   local tbb = {} --Taskbar Buttons
  460.   local dc = {} --Files Table
  461.   local ci = {} --Context Items
  462.  
  463.   local mx,my = term.getSize()
  464.   local btnQuit = {x1 = mx, x2 = mx, y1 = 1, y2 = 1, color = colors.red, text = "x"}
  465.   term.setCursorBlink(false)
  466.   shell.setDir(shell.dir())
  467.   shell.setAlias(fs.getName(shell.getRunningProgram()),"/"..shell.getRunningProgram())
  468.   screen.cls()
  469.   while true do
  470.     --screen.cls()
  471.     screen.drawButton(btnQuit)
  472.     term.setCursorPos(1,2)
  473.     data = list(shell.dir())
  474.     dc = drawFiles(data.allpaths,soffset,active-2)
  475.     tbb = drawTaskBar(dtb,rv)
  476.     if clipboard == nil then
  477.       dpb = false
  478.     else
  479.       dpb = true
  480.     end
  481.     if ca then
  482.       if lItem.name ~= ".." then
  483.         ci = drawFileMenu(lItem,cx,cy,dpb,nfm)
  484.       else
  485.         ca = false
  486.       end
  487.     end
  488.     local evn,misc,x,y = os.pullEvent()
  489.     --print(evn)
  490.     --sleep(3)
  491.     if evn == "mouse_click" then
  492.       if screen.pressed(btnQuit,x,y) then
  493.         quit()
  494.       end
  495.       if misc == 1 then
  496.         for i=1,#tbb do
  497.           if screen.pressed(tbb[i],x,y) then
  498.             pr = 1
  499.             ca = false
  500.             if dtb then
  501.               rv = ""
  502.               tbb[i].exec()
  503.               dtb = false
  504.             else
  505.               rv = tbb[i].exec()
  506.               dtb = true
  507.             end
  508.           end
  509.         end
  510.       end
  511.       if ca and misc == 1 and pr ~= 1 then
  512.         ca=false
  513.         for i=1,#ci do
  514.           if screen.pressed(ci[i],x,y) then
  515.             ci[i].exec(lItem)
  516.             pr = 1
  517.             --ca = false
  518.           end
  519.         end
  520.       end
  521.       if pr~=1 then
  522.         for i=1,#dc do
  523.           if screen.pressed(dc[i],x,y) then
  524.             pr = 1
  525.             if misc == 2 then
  526.               ca = true
  527.               cx = x
  528.               cy = y
  529.               lItem = dc[i]
  530.             else
  531.               if not fs.isDir(dc[i].path) or not dc[i].name == ".." then
  532.                   if active == i + soffset then
  533.                     executing = true
  534.                     if dc[i].path ~= resolve(shell.getRunningProgram()) then
  535.                       screen.cls()
  536.                       shell.run(dc[i].path)
  537.                       sleep(1.5)
  538.                     end
  539.                     executing = false
  540.                     active = 0
  541.                   else
  542.                     active = i + soffset
  543.                   end
  544.               else
  545.                 if active == i + soffset or dc[i].name == ".." then
  546.                   cd(dc[i].path)
  547.                   soffset = 1
  548.                   active = 0
  549.                 else
  550.                   active = i + soffset
  551.                 end
  552.               end
  553.             end
  554.           end
  555.         end
  556.       end
  557.       --Entry Point Mark for new File Menu (NEVERMIND!!!!! Taskbar!!!!)
  558.       if pr == 0 then
  559.         if evn == "mouse_click" and misc == 2 and y>2 then
  560.           local tTable = {}
  561.           tTable.path = shell.dir()
  562.           tTable.name = fs.getName(shell.dir())
  563.           active = 0
  564.           ca = true
  565.           nfm = true
  566.           cx = x
  567.           cy = y
  568.           lItem = tTable
  569.           pr = 0
  570.         elseif evn == "mouse_click" and misc == 1 then
  571.           dtb = false
  572.           ca = false
  573.           active = 0
  574.         end
  575.       elseif pr == 1 then
  576.         nfm = false
  577.         pr = 0
  578.       else
  579.         nfm = false
  580.         active = 0
  581.         ca = false
  582.       end
  583.     elseif evn == "mouse_scroll" then
  584.       if misc <= 0 and soffset ~= 1 then
  585.         ca = false
  586.         soffset = soffset + misc
  587.       elseif misc >= 0 and soffset <= #data.all-17 then
  588.         ca = false
  589.         soffset = soffset + misc
  590.       end
  591.     end
  592.     if evn~="timer" and evn~="http_success" then
  593.       screen.cls()
  594.     end
  595.   end
  596. end
  597.  
  598. function showClock()
  599.   local bstring = "before"
  600.   local astring = "after"
  601.   local tstring = ""
  602.   if tonumber(timeoffset) > 0 then
  603.     tstring = astring
  604.   else
  605.     tstring = bstring
  606.   end
  607.   if string.sub(timeoffset,1,1)=="+" then
  608.     timeoffset = string.sub(timeoffset,2,2)
  609.   elseif string.sub(timeoffset,1,1)=="-" then
  610.     timeoffset = string.sub(timeoffset,2,2)
  611.   end
  612.   if tonumber(timeoffset) > 24 or tonumber(timeoffset) < -24 then
  613.     error("Invalid Offset!")
  614.   end
  615.   while true do
  616.     local stime = "http://www.timeapi.org/"..timezone.."/"..timeoffset.."+hours+"..tstring.."+now?format=%25H:%25M"
  617.     local rtime = http.get(stime).readAll()
  618.     local tupdate = os.startTimer(5)
  619.     while true do
  620.       local rx,ry = term.getCursorPos()
  621.       term.setCursorPos(46,1)
  622.       if not executing then
  623.         term.setBackgroundColor(colors.gray)
  624.         term.write(rtime)
  625.       else
  626.         term.setBackgroundColor(colors.black)
  627.         term.setCursorPos(1,1)
  628.         while executing do
  629.           sleep(1)
  630.         end
  631.         break
  632.       end
  633.       term.setCursorPos(rx,ry)
  634.       sleep(0.05)
  635.       local tuevent = {os.pullEvent()}
  636.       if tuevent[1] == "timer" and tuevent[2] == tupdate then
  637.         break
  638.       end
  639.     end
  640.   end
  641. end
  642.  
  643. if #args > 0 then
  644.   if args[1] == "help" then
  645.     init()
  646.   else
  647.     usage()
  648.   end
  649. else
  650.   repeat
  651.   local success, value = pcall(parallel.waitForAny,main,showClock)
  652.   if not success then
  653.     --screen.cls()
  654.     --print(value)
  655.     --sleep(3)
  656.     showError(value)
  657.   end
  658.   until success
  659. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement