Advertisement
HangMan23

Finder.lua

Aug 14th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.88 KB | None | 0 0
  1. --[[----------------------------------------------Check for EU
  2.  
  3. if not require("filesystem").exists("/lib/ExtraUtilits.lua") or require("ExtraUtilits").version < 1.7 then
  4.  
  5.   require("component").gpu.set(1, 1, "You don'thave ExtraUtilits library or version is lower 1.7 to run this program. Install now?[y/n](internet card required)")
  6.  
  7.    if require("io").read() == "y" then
  8.  
  9.     print("downloading ExtraUtilits...")
  10.  
  11.     local internet = require("internet")
  12.     local req = internet.request("http://pastebin.com/raw/474MKXJZ")
  13.     local text = ""
  14.  
  15.     for line in req.read do
  16.  
  17.       text = text .. line
  18.  
  19.     end
  20.  
  21.     local file = require("io").open("/lib/ExtraUtilits.lua", "w")
  22.     file:write(text)
  23.     file:close()
  24.  
  25.     print("rebooting...")
  26.     require("computer").shutdown(true)
  27.  
  28.   else
  29.  
  30.     os.exit()
  31.  
  32.   end
  33.  
  34. end
  35. ]]
  36. ------------------------------------------------Defines
  37.  
  38. local EU = require("ExtraUtilits")
  39. local shell = require("shell")
  40. local term = require("term")
  41. local gpu = require("component").gpu
  42. local unicode = require("unicode")
  43. local computer = require("computer")
  44. local fs = require("filesystem")
  45. local io = require("io")
  46. local event = require("event")
  47.  
  48. local args = shell.parse(...)
  49. local path = ""
  50. local buttons = {}
  51. local list = {}
  52. local sys = 0
  53. local changePath = function() end
  54.  
  55. local colors = {
  56. ["lua"] = 0x00ff00,
  57. ["directory"] = 0x008cff,
  58. ["cfg"] = 0xff841f,
  59. ["dfpwm"] = 0xff00ff,
  60. ["default"] = 0xffffff,
  61. ["bios"] = 0xccaa87,
  62. ["rec"] = 0x87c3ff
  63. }
  64.  
  65. local formats = {
  66. ["lua"] = "default",
  67. ["bios"] = "/bios.lua",
  68. ["rec"] = "/play.lua"
  69. }
  70.  
  71. ------------------------------------------------Local functions
  72.  
  73. local function comp_shell()
  74.  
  75.   local command = EU.ReadyUtilits.WFUIV2(64, 22, 30, "computer shell")
  76.  
  77.   command = "computer." .. command
  78.  
  79.   local func, reason = load(command)
  80.  
  81.   if func then
  82.  
  83.     local result, reason = xpcall(func, debug.traceback)
  84.  
  85.   end
  86.  
  87. end
  88.  
  89. local function alt()
  90.  
  91.   local ok = false
  92.  
  93.   local evt, _, key1, key2
  94.  
  95.   repeat
  96.  
  97.     evt, _, key1, key2 = computer.pullSignal()
  98.  
  99.     if evt == "key_down" or evt == "key_up" then ok = true end
  100.  
  101.   until ok
  102.  
  103.   if evt == key_up then
  104.  
  105.     return false
  106.  
  107.   elseif evt == "key_down" then
  108.  
  109.     if key1 == 99 and key2 == 46 then
  110.  
  111.       comp_shell()
  112.  
  113.     end
  114.  
  115.   end
  116.  
  117. end
  118.  
  119. local hotKeys = {
  120.  
  121. {key1 = 0, key2 = 56, func = alt}
  122.  
  123. }
  124.  
  125. local w, h = gpu.getResolution()
  126.  
  127. local ploadFile = function(name, args)
  128.  
  129.   local args = args or {}
  130.  
  131.   local func, reason = loadfile(name)
  132.  
  133.   local rs
  134.  
  135.   local err = false
  136.  
  137.   if not func then
  138.  
  139.     err = true
  140.     rs = reason
  141.  
  142.   else
  143.  
  144.     func = function() func(table.unpack(args)) end
  145.  
  146.     local result, reason = xpcall(func, debug.traceback)
  147.  
  148.     if not result and reason then
  149.  
  150.       err = true
  151.       rs = reason
  152.  
  153.     else
  154.  
  155.       local text = "[press any key to continue]"
  156.  
  157.       gpu.setBackground(0x323232)
  158.       gpu.setForeground(0xffffff)
  159.       term.clear()
  160.  
  161.       local w, h = gpu.getResolution()
  162.  
  163.       gpu.set(w / 2 - #text / 2, h / 2, text)
  164.  
  165.       repeat until computer.pullSignal() == "key_down"
  166.  
  167.     end
  168.  
  169.   end
  170.  
  171.   --[[if err then
  172.  
  173.     local text = "Error with running file:"
  174.  
  175.     gpu.setBackground(0x323232)
  176.     gpu.setForeground(0xff0000)
  177.     term.clear()
  178.  
  179.     local w, h = gpu.getResolution()
  180.  
  181.     local lines = {}
  182.  
  183.     local brk = false
  184.  
  185.     repeat
  186.  
  187.       for i = 1, #rs do
  188.  
  189.         local symbol = rs:sub(i, i)
  190.  
  191.         if i == #rs then
  192.  
  193.           brk = true
  194.  
  195.           break
  196.  
  197.         elseif symbol == "\n" then
  198.  
  199.           lines[#lines+1] = rs:sub(1, i - 1)
  200.  
  201.           rs = lines[#lines]
  202.  
  203.           break
  204.  
  205.         else
  206.  
  207.           lines[#lines] = lines[#lines] or "" .. rs:sub(i, i)
  208.  
  209.         end
  210.  
  211.       end
  212.  
  213.     until brk
  214.  
  215.     gpu.set(w / 2 - #text / 2, h / 2 - #lines / 2 - 1, text)
  216.  
  217.     for i = 1, #lines do
  218.  
  219.       local line = lines[i]
  220.  
  221.       gpu.set(w / 2 - #line / 2, h / 2 - #lines / 2 + i, line)
  222.  
  223.     end
  224.  
  225.     event.pull("key_down")
  226.  
  227.   end]]
  228.  
  229.   gpu.setBackground(0x323232)
  230.   term.clear()
  231.   EU.draw(buttons)
  232.   changePath()
  233.  
  234. end
  235.  
  236. local function onKey(ev)
  237.  
  238.   local _, _, key1, key2 = table.unpack(ev)
  239.  
  240.   for i in pairs(hotKeys) do
  241.  
  242.     local hotKey = hotKeys[i]
  243.  
  244.     if key1 == hotKey.key1 then
  245.  
  246.       if hotKey.key2 == nil or hotKey.key2 == key2 then
  247.  
  248.         computer.beep(1200, 0.1)
  249.  
  250.         hotKey.func()
  251.  
  252.       end
  253.  
  254.     end  
  255.  
  256.   end
  257.  
  258. end
  259.  
  260. local function press(button, color, timeout)
  261.  
  262.   if not timeout then timeout = 0.2 end
  263.   local dColor = button[5]
  264.   button[5] = color
  265.   EU.draw({button})
  266.   os.sleep(timeout)
  267.   button[5] = dColor
  268.   EU.draw({button})
  269.  
  270. end
  271.  
  272. local function sysMenu()
  273.  
  274.   press(buttons[sys], 0x323232)
  275.   local menu = {}
  276.  
  277.   local function top()
  278.  
  279.     press(menu[1], 0xff7a7a)
  280.     menu.close = true
  281.  
  282.   end
  283.  
  284.   local function shutdown()
  285.  
  286.     press(menu[2], 0x999999)
  287.     computer.shutdown()
  288.  
  289.   end
  290.  
  291.   local function reboot()
  292.  
  293.     press(menu[3], 0x999999)
  294.  
  295.     local file = io.open("/Autorun.lua", "w")
  296.  
  297. local code = [[
  298. require("filesystem").remove("/Autorun.lua")
  299. os.sleep(1)
  300. loadfile("/finder.lua")()
  301. ]]
  302.  
  303.     file:write(code)
  304.  
  305.     file:close()
  306.  
  307.     computer.shutdown(true)
  308.  
  309.   end
  310.  
  311.   menu[1] = {148, 45, 10, 1, 0xff0000, 0xffffff, "System", top}
  312.   menu[2] = {148, 46, 10, 1, 0x646464, 0x323232, "Shutdown", shutdown}
  313.   menu[3] = {148, 47, 10, 1, 0x646464, 0x323232, "Reboot", reboot}
  314.  
  315.   EU.draw(menu)
  316.   EU.buttonPress(menu)
  317.   gpu.setBackground(0x323232)
  318.   term.clear()
  319.   changePath()
  320.  
  321. end
  322.  
  323. ------------------------------------------------Define main path
  324.  
  325. if args and #args > 0 then
  326.  
  327.   path = args[1]
  328.  
  329. else
  330.  
  331.   path = shell.resolve("")
  332.  
  333. end
  334.  
  335. ------------------------------------------------Main function
  336.  
  337. local function changePath()
  338.  
  339.   gpu.setBackground(0x323232)
  340.   term.clear()
  341.  
  342.   buttons = {}
  343.   list = EU.list(path)
  344.  
  345.   for i = 1, #list do
  346.  
  347.     local function showMenu()
  348.  
  349.       press(buttons[i], 0x646464)
  350.  
  351.       local lw = unicode.wlen(list[i])
  352.       local w = unicode.wlen(list[i])
  353.       if w < 15 then w = 15 end
  354.  
  355.       local menu = {}
  356.      
  357.       local function top()
  358.  
  359.         press(menu[1], 0xff7a7a)
  360.         menu.close = true
  361.  
  362.       end
  363.  
  364.       local function edit()
  365.  
  366.         press(menu[2], 0x999999)
  367.         loadfile("/bin/edit.lua")(shell.resolve(path .. "/" .. list[i]))
  368.         menu.close = true
  369.  
  370.       end
  371.  
  372.       local function remove()
  373.  
  374.         EU.screenBackup(function()
  375.  
  376.         local remButtons = {}
  377.  
  378.         press(menu[3], 0x999999)
  379.  
  380.         local function ok()
  381.  
  382.           press(remButtons[1], 0x646464)
  383.           fs.remove(shell.resolve(path .. "/" .. list[i]))
  384.           remButtons.close = true
  385.  
  386.         end
  387.  
  388.         local text = "do you want to remove " .. list[i] .. "?"
  389.  
  390.         gpu.setBackground(0xffffff)
  391.         gpu.setForeground(0x646464)
  392.         gpu.fill(160 / 2 - (#text / 2) - 1, h/2 - 1, #text + 2, 4, " ")
  393.         gpu.set(80 - #text/2, h/2, text)
  394.  
  395.         remButtons = {
  396.  
  397.         {80 + #text/2-4, h / 2 + 2, 3, 1, 0xffffff, 0x323232, "Yes", ok},
  398.         {80 - #text/2, h / 2 + 2, 2, 1, 0xffffff, 0x323232, "No", function() remButtons.close = true press(remButtons[2], 0x646464) end}
  399.  
  400.         }
  401.  
  402.         remButtons.eventFunc = function(ev)
  403.  
  404.           if ev[1] == "key_down" then
  405.  
  406.             if ev[3] == 13 then
  407.  
  408.               ok()
  409.  
  410.             elseif ev[3] == 9 then
  411.  
  412.               remButtons.close = true
  413.               press(remButtons[2], 0x646464)
  414.  
  415.             end
  416.  
  417.           end
  418.  
  419.         end
  420.  
  421.         EU.draw(remButtons)
  422.         EU.buttonPress(remButtons)
  423.  
  424.         menu.close = true
  425.  
  426.         end)
  427.  
  428.       end
  429.  
  430.       local function run_with_args()
  431.  
  432.         press(menu[4], 0x999999)
  433.         loadfile(shell.resolve(path .. "/" .. list[i]))(EU.ReadyUtilits.WriteFieldUI(60, 23, "Enter arguments:"))
  434.         menu.close = true
  435.  
  436.       end
  437.  
  438.       local function rename()
  439.  
  440.         press(menu[5], 0x999999)
  441.         fs.rename(shell.resolve(path .. "/" .. list[i]), shell.resolve(path .. "/" .. EU.ReadyUtilits.WriteFieldUI(60, 23, "Enter new file name:")))
  442.         menu.close = true
  443.  
  444.       end
  445.  
  446.       menu[1] = {lw + 3, i, w, 1, 0xff0000, 0xffffff, list[i], top}
  447.       menu[2] = {lw + 3, i + 1, w, 1, 0x646464, 0x323232, "edit", edit}
  448.       menu[3] = {lw + 3, i + 2, w, 1, 0x646464, 0x323232, "remove", remove}
  449.       menu[4] = {lw + 3, i + 3, w, 1, 0x646464, 0x323232, "run with args", run_with_args}
  450.       menu[5] = {lw + 3, i + 4, w, 1, 0x646464, 0x323232, "rename", rename}
  451.  
  452.       EU.draw(buttons)
  453.       EU.draw(menu, true)
  454.       EU.buttonPress(menu)
  455.       changePath()
  456.  
  457.     end
  458.  
  459.     local function dofile()
  460.  
  461.       local lPath
  462.       press(buttons[i], 0x646464)
  463.  
  464.       if fs.isDirectory(list[i]) then
  465.  
  466.         path = shell.resolve(path .. "/" .. list[i])
  467.         changePath()
  468.  
  469.       else
  470.  
  471.         local format = EU.getFormat(list[i])
  472.         for k, v  in pairs(formats) do if k == format then lPath = v end end
  473.  
  474.       end
  475.  
  476.       if not lPath then lPath = "/bin/edit.lua" end
  477.  
  478.       if lPath == "default" then ploadFile(list[i])
  479.  
  480.       else
  481.  
  482.         if fs.exists(lPath) then
  483.  
  484.           gpu.set(80, 25, lPath .. "|" .. list[i])
  485.           os.sleep(1)
  486.           ploadFile(lPath, {list[i]})
  487.  
  488.         end
  489.  
  490.       end
  491.  
  492.     end
  493.  
  494.     --Files buttons
  495.  
  496.     local color = colors["default"]
  497.     local f = ""
  498.  
  499.     if fs.isDirectory(shell.resolve(path .. "/" .. list[i])) then
  500.  
  501.       color = colors["directory"]
  502.  
  503.     else
  504.  
  505.       local format = EU.getFormat(list[i])
  506.       for k, v in pairs(colors) do if format == k then color = v end end
  507.  
  508.     end
  509.  
  510.     buttons[i] = {}
  511.     buttons[i][1] = 1
  512.     buttons[i][2] = i + 1
  513.     buttons[i][3] = unicode.wlen(list[i])
  514.     buttons[i][4] = 1
  515.     buttons[i][5] = 0x323232
  516.     buttons[i][6] = color
  517.     buttons[i][7] = list[i]
  518.     buttons[i][8] = dofile
  519.     buttons[i][10] = showMenu
  520.  
  521.   end
  522.  
  523.   --Interface
  524.  
  525.   local now = #buttons+1
  526.   local function close() f_close = true gpu.setBackground(0x000000) gpu.setForeground(0xffffff) term.clear() os.exit() end
  527.   buttons[now] = {160, 1, 1, 1, 0x161616, 0xffffff, "X", close}
  528.  
  529.   local now = #buttons+1
  530.   local function enterPath() path = shell.resolve(EU.ReadyUtilits.WriteFieldUI(60, 23, "Enter new path:")) changePath() EU.buttonPress(buttons) end
  531.   buttons[now] = {3, 1, 157, 1, 0x161616, 0x00ffff, path, enterPath}
  532.  
  533.   local now = #buttons+1
  534.  
  535.   function new()
  536.  
  537.     press(buttons[now], 0x999999)
  538.  
  539.     local menu = {}
  540.    
  541.     local function top()
  542.  
  543.       press(menu[1], 0xff7a7a)
  544.       menu.close = true
  545.  
  546.     end
  547.  
  548.     local function file()
  549.  
  550.       press(menu[2], 0x646464)
  551.       local name = shell.resolve(path .. "/" .. EU.ReadyUtilits.WriteFieldUI(60, 23, "Enter file name:"))
  552.       loadfile("/bin/edit.lua")(name)
  553.       menu.close = true
  554.  
  555.     end
  556.  
  557.     local function folder()
  558.  
  559.       press(menu[3], 0x999999)
  560.       local name = shell.resolve(path .. "/" .. EU.ReadyUtilits.WriteFieldUI(60, 23, "Enter folder name:"))
  561.       local result, reason = fs.makeDirectory(name)
  562.       if not result then EU.screenBackup(function() gpu.setBackground(0x161616) gpu.setForeground(0x00ffff) gpu.set(60, 23, reason) os.sleep(1) end) end
  563.       menu.close = true
  564.    
  565.     end
  566.  
  567.     menu[1] = {145, 2, 10, 1, 0xff0000, 0xffffff,  "New", top}
  568.     menu[2] = {145, 3, 10, 1, 0x646464, 0x323232, "file", file}
  569.     menu[3] = {145, 4, 10, 1, 0x646464, 0x323232, "folder", folder}
  570.  
  571.     EU.draw(menu)
  572.     EU.buttonPress(menu)
  573.     changePath()
  574.  
  575.   end
  576.  
  577.   buttons[now] = {158, 2, 3, 1, 0x323232, 0x00ff00, "New", new}
  578.  
  579.   local now = #buttons+1
  580.   function run() press(buttons[now], 0x646464) local name = shell.resolve(EU.ReadyUtilits.WriteFieldUI(60, 23, "Enter file name:")) local func = ploadFile(shell.resolve(path .. "/" .. name)) if func then func() end end
  581.   buttons[now] = {158, 3, 3, 1, 0x323232, 0x00ff00, "Run", run}
  582.  
  583.   local now = #buttons+1
  584.   function root() press(buttons[now], 0x323232) path = "/" changePath() end
  585.   buttons[now] = {1, 1, 2, 1, 0x161616, 0x00ffff, "<<", root}
  586.  
  587.   local now = #buttons+1
  588.   buttons[now] = {1, 50, 160, 1, 0x161616, 0x00ffff, "", function() end}
  589.  
  590.   local now = #buttons+1
  591.   function lua() press(buttons[now], 0x646464) gpu.setBackground(0) gpu.setForeground(0xffffff) term.clear() loadfile("/bin/lua.lua")() changePath() end
  592.   buttons[now] = {158, 4, 3, 1, 0x323232, 0x00ff00, "Lua", lua}
  593.  
  594.   local now = #buttons+1
  595.   sys = now
  596.   buttons[now] = {155, 50, 6, 1, 0x161616, 0xffffff, "System", sysMenu}
  597.  
  598.   buttons.eventFunc = function(ev)
  599.  
  600.     local evt, _, key1, key2 = table.unpack(ev)
  601.  
  602.     if evt == "key_down" and key1 == 23 and key2 == 17 then
  603.  
  604.       close()
  605.  
  606.     elseif unicode.char(key1) == "r" then
  607.  
  608.       changePath()
  609.  
  610.     elseif evt == "key_down" then
  611.  
  612.       onKey(ev)
  613.  
  614.     end
  615.  
  616.   end
  617.  
  618.   EU.draw(buttons)
  619.   EU.buttonPress(buttons)
  620.  
  621. end
  622.  
  623. ------------------------------------------------Run
  624. changePath()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement