Advertisement
LDDestroier

LDDFM BETA

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