LDDestroier

LDDFM File Manager API (computercraft)

Sep 18th, 2016
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.42 KB | None | 0 0
  1. --[[
  2.     LDDFM
  3.      File Manager API!
  4.     Select files with ease!
  5.    
  6.     Just do something like
  7.       local path = makeMenu()
  8.     and you're set!
  9.    
  10.     Get with:
  11.       pastebin get wfdb986Z lddfm
  12.       std ld lddfm lddfm
  13.       std PB wfdb986Z lddfm
  14. --]]
  15.  
  16. local lddfm = { --main API
  17.     scroll = 0,
  18.     ypaths = {},
  19. }
  20.  
  21. lddfm.scr_x, lddfm.scr_y = term.getSize()
  22.  
  23. lddfm.setPalate = function(_p)
  24.     if type(_p) ~= "table" then
  25.         _p = {}
  26.     end
  27.     lddfm.p = { --the DEFAULT color palate
  28.         bg =        _p.bg or colors.gray,           -- whole background color
  29.         d_txt =     _p.d_txt or colors.yellow,      -- directory text color
  30.         d_bg =      _p.d_bg or colors.gray,         -- directory bg color
  31.         f_txt =     _p.f_txt or colors.white,       -- file text color
  32.         f_bg =      _p.f_bg or colors.gray,         -- file bg color
  33.         p_txt =     _p.p_txt or colors.black,       -- path text color
  34.         p_bg =      _p.p_bg or colors.lightGray,    -- path bg color
  35.         close_txt = _p.close_txt or colors.gray,    -- close button text color
  36.         close_bg =  _p.close_bg or colors.lightGray,-- close button bg color
  37.         scr =       _p.scr or colors.lightGray,     -- scrollbar color
  38.         scrbar =    _p.scrbar or colors.gray,       -- scroll tab color
  39.     }
  40. end
  41.  
  42. lddfm.setPalate()
  43.  
  44. lddfm.foldersOnTop = function(floop,path)
  45.     local output = {}
  46.     for a = 1, #floop do
  47.         if fs.isDir(fs.combine(path,floop[a])) then
  48.             table.insert(output,1,floop[a])
  49.         else
  50.             table.insert(output,floop[a])
  51.         end
  52.     end
  53.     return output
  54. end
  55.  
  56. lddfm.filterFileFolders = function(list,path,_noFiles,_noFolders,_noCD,_doHidden)
  57.     local output = {}
  58.     for a = 1, #list do
  59.         local entry = fs.combine(path,list[a])
  60.         if fs.isDir(entry) then
  61.             if entry == ".." then
  62.                 if not (_noCD or _noFolders) then table.insert(output,list[a]) end
  63.             else
  64.                 if not ((not _doHidden) and list[a]:sub(1,1) == ".") then
  65.                     if not _noFolders then table.insert(output,list[a]) end
  66.                 end
  67.             end
  68.         else
  69.             if not ((not _doHidden) and list[a]:sub(1,1) == ".") then
  70.                 if not _noFiles then table.insert(output,list[a]) end
  71.             end
  72.         end
  73.     end
  74.     return output
  75. end
  76.  
  77. lddfm.isColor = function(col)
  78.     for k,v in pairs(colors) do
  79.         if v == col then
  80.             return true, k
  81.         end
  82.     end
  83.     return false
  84. end
  85.  
  86. lddfm.clearLine = function(x1,x2,_y,_bg,_char)
  87.     local cbg, bg = term.getBackgroundColor()
  88.     local x,y = term.getCursorPos()
  89.     local sx,sy = term.getSize()
  90.     if type(_char) == "string" then char = _char else char = " " end
  91.     if type(_bg) == "number" then
  92.         if lddfm.isColor(_bg) then bg = _bg
  93.         else bg = cbg end
  94.     else bg = cbg end
  95.     term.setCursorPos(x1 or 1, _y or y)
  96.     term.setBackgroundColor(bg)
  97.     if x2 then --it pains me to add an if statement to something as simple as this
  98.         term.write((char or " "):rep(x2-x1))
  99.     else
  100.         term.write((char or " "):rep(sx-(x1 or 0)))
  101.     end
  102.     term.setBackgroundColor(cbg)
  103.     term.setCursorPos(x,y)
  104. end
  105.  
  106. lddfm.render = function(_x1,_y1,_x2,_y2,_rlist,_path,_rscroll,_canClose,_scrbarY)
  107.     local tsv = term.current().setVisible
  108.     local px,py = term.getCursorPos()
  109.     if tsv then tsv(false) end
  110.     local x1, x2, y1, y2 = _x1 or 1, _x2 or lddfm.scr_x, _y1 or 1, _y2 or lddfm.scr_y
  111.     local rlist = _rlist or {"Invalid directory."}
  112.     local path = _path or "And that's terrible."
  113.     ypaths = {}
  114.     local rscroll = _rscroll or 0
  115.     for a = y1, y2 do
  116.         lddfm.clearLine(x1,x2,a,lddfm.p.bg)
  117.     end
  118.     term.setCursorPos(x1,y1)
  119.     term.setTextColor(lddfm.p.p_txt)
  120.     lddfm.clearLine(x1,x2+1,y1,lddfm.p.p_bg)
  121.     term.setBackgroundColor(lddfm.p.p_bg)
  122.     term.write(("/"..path):sub(1,x2-x1))
  123.     for a = 1,(y2-y1) do
  124.         if rlist[a+rscroll] then
  125.             term.setCursorPos(x1,a+(y1))
  126.             if fs.isDir(fs.combine(path,rlist[a+rscroll])) then
  127.                 lddfm.clearLine(x1,x2,a+(y1),lddfm.p.d_bg)
  128.                 term.setTextColor(lddfm.p.d_txt)
  129.                 term.setBackgroundColor(lddfm.p.d_bg)
  130.             else
  131.                 lddfm.clearLine(x1,x2,a+(y1),lddfm.p.f_bg)
  132.                 term.setTextColor(lddfm.p.f_txt)
  133.                 term.setBackgroundColor(lddfm.p.f_bg)
  134.             end
  135.             term.write(rlist[a+rscroll]:sub(1,x2-x1))
  136.             ypaths[a+(y1)] = rlist[a+rscroll]
  137.         else
  138.             lddfm.clearLine(x1,x2,a+(y1),lddfm.p.bg)
  139.         end
  140.     end
  141.     local scrbarY = _scrbarY or math.ceil( (y1+1)+( (_rscroll/(#_rlist-(y2-(y1+1))))*(y2-(y1+1)) ) )
  142.     for a = y1+1, y2 do
  143.         term.setCursorPos(x2,a)
  144.         if a == scrbarY then
  145.             term.setBackgroundColor(lddfm.p.scrbar)
  146.         else
  147.             term.setBackgroundColor(lddfm.p.scr)
  148.         end
  149.         term.write(" ")
  150.     end
  151.     if _canClose then
  152.         term.setCursorPos(x2-4,y1)
  153.         term.setTextColor(lddfm.p.close_txt)
  154.         term.setBackgroundColor(lddfm.p.close_bg)
  155.         term.write("close")
  156.     end
  157.     term.setCursorPos(px,py)
  158.     if tsv then tsv(true) end
  159.     return scrbarY
  160. end
  161.  
  162. lddfm.coolOutro = function(x1,y1,x2,y2,_bg,_txt,char)
  163.     local cx, cy = term.getCursorPos()
  164.     local bg, txt = term.getBackgroundColor(), term.getTextColor()
  165.     term.setTextColor(_txt or colors.white)
  166.     term.setBackgroundColor(_bg or colors.black)
  167.     local _uwah = 0
  168.     for y = y1, y2 do
  169.         for x = x1, x2 do
  170.             _uwah = _uwah + 1
  171.             term.setCursorPos(x,y)
  172.             term.write(char or " ")
  173.             if _uwah >= math.ceil((x2-x1)*1.63) then sleep(0) _uwah = 0 end
  174.         end
  175.     end
  176.     term.setTextColor(txt)
  177.     term.setBackgroundColor(bg)
  178.     term.setCursorPos(cx,cy)
  179. end
  180.  
  181. lddfm.scrollMenu = function(amount,list,y1,y2)
  182.     if #list >= y2-y1 then
  183.         lddfm.scroll = lddfm.scroll + amount
  184.         if lddfm.scroll < 0 then
  185.             lddfm.scroll = 0
  186.         end
  187.         if lddfm.scroll > #list-(y2-y1) then
  188.             lddfm.scroll = #list-(y2-y1)
  189.         end
  190.     end
  191. end
  192.  
  193. --[[
  194.  a quick explanation of the arguments:
  195.  
  196. x1 and y1: top-left corner coordinates of menu window. defaults to the top-left corner of the screen
  197. x2 and y2: bottom-right corner coordinates of menu window. defaults to the bottom-right corner of the screen
  198. _path: path to start viewing. defaults to "/"
  199. _noFiles: whether or not to view files in the menu, mainly for picking a path for installing something. defaults to false
  200. _noFolders: whether or not to view folders in the menu, mainly for choosing a file to run or whatever. defaults to false
  201. _noCD: whether or not you can change the directory, mainly to limit choices to a single folder. defaults to false
  202. _noSelectFolders: whether or not you can select folders to return. defaults to false
  203. _doHidden: whether or not to hide hidden files (starts with "."). defaults to false
  204. _p: the palate. has: bg, d_txt, d_bg, f_txt, t_bg, p_txt, p_bg, scr, scrbar. 'd' is for directory, 'f' is for file, 'p' is for path bar.
  205. _canClose: whether or not you can click on the little top-right "Cancel" button.
  206. --]]
  207.  
  208. makeMenu = function(_x1,_y1,_x2,_y2,_path,_noFiles,_noFolders,_noCD,_noSelectFolders,_doHidden,_p,_canClose)
  209.     if _noFiles and _noFolders then
  210.         return false, "C'mon, man..."
  211.     end
  212.     if _x1 == true then
  213.         return false, "arguments: x1, y1, x2, y2, path, noFiles, noFolders, noCD, noSelectFolders, doHidden, palate, canClose" -- a little help
  214.     end
  215.     lddfm.setPalate(_p)
  216.     local path, list = _path or ""
  217.     lddfm.scroll = 0
  218.     local _pbg, _ptxt = term.getBackgroundColor(), term.getTextColor()
  219.     local x1, x2, y1, y2 = _x1 or 1, _x2 or lddfm.scr_x, _y1 or 1, _y2 or lddfm.scr_y
  220.     local keysDown = {}
  221.     local _barrY
  222.     while true do
  223.         list = lddfm.foldersOnTop(lddfm.filterFileFolders(fs.list(path),path,_noFiles,_noFolders,_noCD,_doHidden),path)
  224.         if (fs.getDir(path) ~= "..") and not (_noCD or _noFolders) then
  225.             table.insert(list,1,"..")
  226.         end
  227.         _res, _barrY = pcall( function() return lddfm.render(x1,y1,x2,y2,list,path,lddfm.scroll,_canClose) end)
  228.         if not _res then
  229.             local tsv = term.current().setVisible
  230.             if tsv then tsv(true) end
  231.             error(_barrY)
  232.         end
  233.         local evt = {os.pullEvent()}
  234.         if evt[1] == "mouse_scroll" then
  235.             lddfm.scrollMenu(evt[2],list,y1,y2)
  236.         elseif evt[1] == "mouse_click" then
  237.             local butt,mx,my = evt[2],evt[3],evt[4]
  238.             if (butt == 1 and my == y1 and mx <= x2 and mx >= x2-4) and _canClose then
  239.                 --lddfm.coolOutro(x1,y1,x2,y2)
  240.                 term.setTextColor(_ptxt) term.setBackgroundColor(_pbg)
  241.                 return false
  242.             elseif ypaths[my] and (mx >= x1 and mx < x2) then --x2 is reserved for the scrollbar, breh
  243.                 if fs.isDir(fs.combine(path,ypaths[my])) then
  244.                     if _noCD or butt == 3 then
  245.                         if not _noSelectFolders or _noFolders then
  246.                             --lddfm.coolOutro(x1,y1,x2,y2)
  247.                             term.setTextColor(_ptxt) term.setBackgroundColor(_pbg)
  248.                             return fs.combine(path,ypaths[my])
  249.                         end
  250.                     else
  251.                         path = fs.combine(path,ypaths[my])
  252.                         lddfm.scroll = 0
  253.                     end
  254.                 else
  255.                     term.setTextColor(_ptxt) term.setBackgroundColor(_pbg)
  256.                     return fs.combine(path,ypaths[my])
  257.                 end
  258.             end
  259.         elseif evt[1] == "key" then
  260.             keysDown[evt[2]] = true
  261.             if evt[2] == keys.enter and not (_noFolders or _noCD or _noSelectFolders) then --the logic for _noCD being you'd normally need to go back a directory to select the current directory.
  262.                 --lddfm.coolOutro(x1,y1,x2,y2)
  263.                 term.setTextColor(_ptxt) term.setBackgroundColor(_pbg)
  264.                 return path
  265.             end
  266.             if evt[2] == keys.up then
  267.                 lddfm.scrollMenu(-1,list,y1,y2)
  268.             elseif evt[2] == keys.down then
  269.                 lddfm.scrollMenu(1,list,y1,y2)
  270.             end
  271.             if evt[2] == keys.pageUp then
  272.                 lddfm.scrollMenu(y1-y2,list,y1,y2)
  273.             elseif evt[2] == keys.pageDown then
  274.                 lddfm.scrollMenu(y2-y1,list,y1,y2)
  275.             end
  276.             if evt[2] == keys.home then
  277.                 lddfm.scroll = 0
  278.             elseif evt[2] == keys["end"] then
  279.                 if #list > (y2-y1) then
  280.                     lddfm.scroll = #list-(y2-y1)
  281.                 end
  282.             end
  283.             if evt[2] == keys.h then
  284.                 if keysDown[keys.leftCtrl] or keysDown[keys.rightCtrl] then
  285.                     _doHidden = not _doHidden
  286.                 end
  287.             end
  288.         elseif evt[1] == "key_up" then
  289.             keysDown[evt[2]] = false
  290.         end
  291.     end
  292. end
  293.  
  294. return makeMenu
Add Comment
Please, Sign In to add comment