Advertisement
CaptainSpaceCat

Directory Explorer

Dec 13th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.52 KB | None | 0 0
  1. local tFiles = {}
  2. local tDirs = {}
  3.  
  4. local cSelected = 1
  5. local oLevel = 0
  6. local currentDir = ""
  7. local inDir = true
  8.  
  9. local w, h = term.getSize()
  10. local x, y = term.getSize()
  11.  
  12. local correctPass = "larp"
  13.  
  14. blit = function( txt, txtcol, backcol)
  15.         term.setBackgroundColor(backcol)
  16.         term.setTextColor(txtcol)
  17.         term.write(txt)
  18. end
  19.  
  20. --[[COROUTINE BASED INTRO SEQUENCE]]--
  21.  
  22. local slowWrite
  23. local coDone = false
  24. function resetWrite()
  25. slowWrite = coroutine.create(function (str, speed, delay)
  26.   for i = 1, #str do
  27.     local index = 1
  28.     if not coDone then
  29.       write(str:sub(i, i))
  30.       coroutine.yield(1/speed)
  31.     end
  32.   end
  33.   return true, delay
  34. end)
  35. end
  36.  
  37. function blink(speed)
  38.         if not speed then
  39.                 speed = .4
  40.         end
  41.         local a,b = term.getCursorPos()
  42.         uWords(" ", a, b, _, colors.lime)
  43.         coroutine.yield(speed)
  44.         uWords(" ", a, b, _, colors.black)
  45.         coroutine.yield(speed)
  46.     term.setCursorPos(a,b)
  47. end
  48.  
  49. local messages = {
  50. {"WELCOME TO ROBCO INDUSTRIES (TM) TERMLINK", 1, 1, 160, .05},
  51. {">", 1, 2, 160, 1},
  52. {"SET TERMINAL/INQUIRE", 2, 2, 30, .75},
  53. {"RIT-V300", 1, 4, 30, .05},
  54. {">", 1, 6, 160, 1},
  55. {"SET FILE/PROTECTION-OWNER:RWED ACCOUNTS.F", 2, 6, 30, .05},
  56. {">", 1, 7, 160, 1},
  57. {"SET HALT RESTART/MAINT", 2, 7, 30, .5},
  58. {"Initializing Robco Industries(TM) MF Boot Agent", 1, 9, 160, .1},
  59. {"RETROS BIOS", 1, 10, 160, .1},
  60. {"RBIOS-4.02.08.00 52EE5.E7.E8", 1, 11, 160, .1},
  61. {"Copyright 2201-2201 Robco Ind.", 1, 12, 160, .1},
  62. {"Uppermem: 64 KB", 1, 13, 160, .1},
  63. {"Root (5AB)", 1, 14, 160, .1},
  64. {"Maintenance Mode", 1, 15, 160, .05},
  65. {">", 1, 17, 160, 1},
  66. {"RUN DEBUG/ACCOUNTS.F", 2, 17, 30, 1}
  67. }
  68.  
  69. local cMsg = 1
  70. local writeTimer, blinkTimer
  71.  
  72. function txt()
  73.   term.setCursorBlink(true)
  74.   term.setBackgroundColor(colors.black)
  75.   term.setTextColor(colors.lime)
  76.   term.clear()
  77.   term.setCursorPos(1, 1)
  78.   resetWrite()
  79.   _, length = coroutine.resume(slowWrite, messages[cMsg][1], messages[cMsg][4], messages[cMsg][5])
  80.   writeTimer = os.startTimer(length)
  81.   while true do
  82.     local events = {os.pullEvent()}
  83.     if events[1] == "timer" then
  84.       if events[2] == writeTimer then
  85.         _, length, delay = coroutine.resume(slowWrite, messages[cMsg][1], messages[cMsg][4], messages[cMsg][5])
  86.         if delay then
  87.           term.setCursorBlink(true)
  88.           writeTimer = os.startTimer(delay)
  89.           cMsg = cMsg + 1
  90.           if cMsg > #messages then
  91.             sleep(delay)
  92.             break
  93.           end
  94.           resetWrite()
  95.           term.setCursorPos(messages[cMsg][2], messages[cMsg][3])
  96.         else
  97.           term.setCursorBlink(false)
  98.           writeTimer = os.startTimer(length)
  99.         end
  100.       end
  101.     elseif events[1] == "key" or events[1] == "mouse_click" then
  102.       os.cancelTimer(writeTimer)
  103.       coDone = true
  104.       term.setBackgroundColor(colors.black)
  105.       term.clear()
  106.       term.setCursorPos(1, 1)
  107.       break
  108.     end
  109.   end
  110.   term.setCursorBlink(false)
  111. end
  112.  
  113. --[[PASSWORD COLLECTION]]--
  114.  
  115. function password()
  116.     shell.run("clear")
  117.         textutils.slowPrint("ROBCO INDUSTRIES (TM) TERMLINK PROTOCOL",i)
  118.         textutils.slowPrint("ENTER PASSWORD NOW",i)
  119.     local c,d = term.getCursorPos()
  120.         textutils.slowWrite("4 ATTEMPT(S) LEFT: ")
  121.         boxes("slow",4)
  122.         for i=1,5 do
  123.         for i=1,3 do
  124.             term.setCursorPos(c,d+2+i)
  125.             term.clearLine()
  126.         end
  127.                 term.setCursorPos(c,d+2)
  128.         term.clearLine()
  129.                 term.write("> ")
  130.         term.setCursorBlink(false)
  131.                 local pass = read("*")
  132.         if pass:lower() ~= correctPass and i == 5 then
  133.             textutils.slowWrite(">Entry Denied",80)
  134.             blinkN(1)
  135.             textutils.slowWrite(">Lockout in progress")
  136.             blinkN(1)
  137.             scroll()
  138.             while true do
  139.                 term.setCursorPos((x/2)-(7),(y/2)-1)
  140.                 term.write("TERMINAL LOCKED")
  141.                 term.setCursorPos((x/2)-(15),(y/2)+1)  
  142.                 term.write("PLEASE CONTACT AN ADMINISTRATOR")
  143.                 sleep(1)
  144.             end
  145.                 elseif pass:lower() ~= correctPass then
  146.             textutils.slowWrite(">Entry Denied",80)
  147.             blinkN(1)
  148.             local mem = NumCorrect(pass)
  149.             textutils.slowWrite(">"..mem.." correct",80)
  150.             blinkN(1)
  151.                 else
  152.                 textutils.slowWrite(">Exact Match!",80)
  153.                         blinkN(1)
  154.                         textutils.slowPrint(">Please Wait",80)
  155.                         textutils.slowPrint(">while system",80)
  156.                         textutils.slowPrint(">is accessed",80)
  157.             blinkN(1)
  158.             scroll()
  159.             break
  160.         end
  161.         term.setCursorPos(c,d)
  162.         term.clearLine()
  163.         term.write(tostring(4-i).." ATTEMPT(S) LEFT: ")
  164.         boxes(nil,4-i)
  165.         end
  166.        
  167. end
  168.        
  169. function scroll()
  170.     local g,h = term.getCursorPos()
  171.     for i=1,h+1 do
  172.         term.scroll(1)
  173.         sleep(.1)
  174.     end
  175. end
  176.      
  177. function NumCorrect(pass)
  178.     local count = 0
  179.     for i=1,#correctPass do
  180.         if pass:sub(i,i) == correctPass:sub(i,i) then
  181.             count = count + 1
  182.         end
  183.     end
  184.     return tostring(count).."/"..tostring(#correctPass)
  185. end
  186.  
  187. function boxes(a,f)
  188.     if a == "slow" then
  189.             for i=1,f do
  190.                     term.setBackgroundColor(colors.lime)
  191.                     textutils.slowWrite(" ")
  192.                     term.setBackgroundColor(colors.black)
  193.                     textutils.slowWrite(" ")
  194.         end
  195.         print(nil)
  196.         elseif a == nil then
  197.         for i=1,f do
  198.                     term.setBackgroundColor(colors.lime)
  199.                     term.write(" ")
  200.                     term.setBackgroundColor(colors.black)
  201.                     term.write(" ")
  202.         end
  203.     end
  204. end
  205.        
  206. function blinkN(p,s)
  207.         if not s then
  208.                 s = .4
  209.         end
  210.         local a,b = term.getCursorPos()
  211.         for i = 1,p do
  212.                 sleep(s)
  213.                 term.setCursorPos(a,b)
  214.                 blit(" ",colors.lime,colors.lime)
  215.                 sleep(s)
  216.                 term.setCursorPos(a,b)
  217.                 blit(" ",colors.lime,colors.black)
  218.         end
  219.         term.setCursorPos(1,b+1)
  220. end
  221. --local blink = coroutine.create(blink)
  222.  
  223. txt()
  224. password()
  225.  
  226. --[[FILE EXPLORER]]--
  227.  
  228. function uWords(string, x, y, txtcol, bakcol)
  229.     string = string or ""
  230.     txtcol = txtcol or colors.white
  231.         bakcol = bakcol or colors.black
  232.     if not tonumber(x) then
  233.         x = x:gsub(" ", "")
  234.             if x:sub(1, 1) == "l" then
  235.                 if x:find("+") then
  236.                     offset = tonumber(x:sub(x:find("+") + 1))
  237.                     term.setCursorPos(1 + offset, y)
  238.                 else
  239.                     term.setCursorPos(1, y)
  240.                 end
  241.             elseif x:sub(1, 1) == "c" then
  242.                 if x:find("+") then
  243.                     offset = tonumber(x:sub(x:find("+") + 1))
  244.                     term.setCursorPos(w/2 - #string/2 + 1 + offset, y)
  245.             elseif x:find("-") then
  246.                     offset = tonumber(x:sub(x:find("-") + 1))
  247.                     term.setCursorPos(w/2 - #string/2 + 1 - offset, y)
  248.             else
  249.                     term.setCursorPos(w/2 - #string/2 + 1, y)
  250.             end
  251.             elseif x:sub(1, 1) == "r" then
  252.                 if x:find("-") then
  253.                     offset = tonumber(x:sub(x:find("-") + 1))
  254.                     term.setCursorPos(w - #string + 1 - offset, y)
  255.                 else
  256.                     term.setCursorPos(w - #string + 1, y)
  257.                 end
  258.         end
  259.     else
  260.             term.setCursorPos(x, y)
  261.     end
  262.     term.setBackgroundColor(bakcol)
  263.     term.setTextColor(txtcol)
  264.     term.write(string)
  265. end
  266.  
  267. local function draw(string, xPos, yPos, txtcol, bakcol)
  268.     local string = string or ""
  269.     local txtcol = txtcol or colors.white
  270.     local bakcol = bakcol or colors.black
  271.     term.setCursorPos(xPos, yPos)
  272.     term.setBackgroundColor(bakcol)
  273.     term.setTextColor(txtcol)
  274.     term.write(string)
  275. end
  276.  
  277. function clearTable(t)
  278.   for i = 1, #t do
  279.     t[i] = nil
  280.   end
  281. end
  282.  
  283. function updateFiles(f)
  284.   clearTable(f)
  285.   for _, file in ipairs(fs.list(currentDir)) do
  286.     f[#f + 1] = {}
  287.     f[#f].name = file
  288.     if fs.isDir(fs.combine(currentDir, file)) then
  289.       f[#f].type = "directory"
  290.     else
  291.       f[#f].type = "file"
  292.     end
  293.   end
  294.   --table.sort(f)
  295. end
  296.  
  297. local tFiles = {}
  298.  
  299. function displayScreen(level, update)
  300.   term.setBackgroundColor(colors.black)
  301.   term.clear()
  302.   uWords("ROBCO INDUSTRIES UNIFIED OPERATING SYSTEM", "c", 1, colors.lime)
  303.   uWords("COPYRIGHT 2075-2077 ROBCO INDUSTRIES", "c", 2, colors.lime)
  304.   uWords("-Server " .. os.getComputerID() .. "-", "c", 3, colors.lime)
  305.   uWords(os.getComputerLabel() and os.getComputerLabel() .. currentDir or currentDir, 1, 4, colors.lime)
  306.   uWords("----------", 1, 5, colors.lime)
  307.  
  308.   uWords("  ", "r", h-1, colors.lime, colors.red)
  309.   uWords("  ", "r", h, colors.lime, colors.red)
  310.   term.setBackgroundColor(colors.black)
  311.  
  312.   if update then
  313.     updateFiles(tFiles)
  314.     inDir = false
  315.   end
  316.  
  317.   for i = 1, #tFiles - level do
  318.     local pre = tFiles[i + level].type == "file" and ">" or "+>"
  319.     if i + level == cSelected then
  320.       uWords(pre .. tFiles[i + level].name, 1, i + 5, colors.black, colors.lime)
  321.     else
  322.       uWords(pre .. tFiles[i + level].name, 1, i + 5, colors.lime)
  323.     end
  324.     if i == h - 7 then
  325.       break
  326.     end
  327.   end
  328.   if currentDir == "" then
  329.     if cSelected == #tFiles + 1 then
  330.       uWords("=>" .. "new", 1, h, colors.black, colors.lime)
  331.     else
  332.       uWords("=>" .. "new", 1, h, colors.lime)
  333.     end
  334.   else
  335.     if cSelected == #tFiles + 1 then
  336.       uWords("/>" .. "back", 1, h, colors.black, colors.lime)
  337.     else
  338.       uWords("/>" .. "back", 1, h, colors.lime)
  339.     end
  340.   end
  341. end
  342.  
  343. function loadFileOptions(file, ...)
  344.   clearTable(tFiles)
  345.   for i = 1, select("#", ...) do
  346.     tFiles[i] = {}
  347.     tFiles[i].name = select(i, ...)
  348.     tFiles[i].type = select(i, ...)
  349.   end
  350.   currentDir = currentDir .. "/" .. file
  351. end
  352.  
  353. function activateFile(f)
  354.   oLevel = 0
  355.   cSelected = 1
  356.   if f.type == "directory" then
  357.     currentDir = currentDir .. "/" .. f.name
  358.     oLevel = 0
  359.     cSelected = 1
  360.     inDir = true
  361.   elseif f.type == "file" then
  362.     loadFileOptions(f.name, "Edit", "Run", "Delete")
  363.     inDir = false
  364.   elseif f.type == "Edit" then
  365.     shell.run("edit " .. currentDir)
  366.   elseif f.type == "Run" then
  367.     term.clear()
  368.     uWords("Input args:", 1, 1, colors.lime)
  369.     term.setCursorPos(1, 2)
  370.     local args = read()
  371.     shell.run(currentDir .. " " .. args)
  372.     os.pullEvent()
  373.   elseif f.type == "Delete" then
  374.     shell.run("delete " .. currentDir)
  375.     currentDir = currentDir:gsub("/[%w%.]+$", "")
  376.     inDir = true
  377.   end
  378. end
  379.  
  380. function eventHandler(e)
  381.   if e[1] == "key" then
  382.     if e[2] == keys.enter then
  383.       if cSelected > #tFiles then
  384.         if currentDir == "" then
  385.           term.setBackgroundColor(colors.black)
  386.           term.clear()
  387.           uWords("Input Program Name:", 1, 1, colors.lime)
  388.           term.setCursorPos(1, 2)
  389.           local program = read()
  390.           shell.run("edit " .. program)
  391.           oLevel = 0
  392.           cSelected = 1
  393.           inDir = true
  394.         else
  395.           currentDir = currentDir:gsub("/[%w%.]+$", "")
  396.           inDir = true
  397.           oLevel = 0
  398.           cSelected = 1
  399.         end
  400.       else
  401.         activateFile(tFiles[cSelected])
  402.       end
  403.     elseif e[2] == keys.down then
  404.       if cSelected - oLevel == h - 7 and cSelected < #tFiles then
  405.         oLevel = oLevel + 1
  406.       end
  407.       cSelected = cSelected <= #tFiles and cSelected + 1 or #tFiles + 1
  408.     elseif e[2] == keys.up then
  409.       if cSelected - oLevel == h - 18 and cSelected > 1 then
  410.         oLevel = oLevel - 1
  411.       end
  412.       cSelected = cSelected > 1 and cSelected - 1 or 1
  413.     end
  414.   elseif e[1] == "mouse_click" then
  415.     if e[2] == 1 and e[3] > w - 2 and e[4] > h - 2 then
  416.       os.shutdown()
  417.     end
  418.   end
  419. end
  420.    
  421. while true do
  422.   displayScreen(oLevel, inDir)
  423.   local events = {os.pullEventRaw()}
  424.   eventHandler(events)
  425. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement