Advertisement
CrazedProgrammer

File Dialog API

Apr 28th, 2015
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.70 KB | None | 0 0
  1. -- File Dialog API by CrazedProgrammer
  2. -- You can find info and documentation on these pages:
  3. --
  4. -- You may use this in your ComputerCraft programs and modify it without asking.
  5. -- However, you may not publish this API under your name without asking me.
  6. -- If you have any suggestions, bug reports or questions then please send an email to:
  7.  
  8. local function list(dir)
  9.     local fullList = fs.list(dir)
  10.     table.sort(fullList, function (a, b) return string.lower(a) < string.lower(b) end)
  11.     local displayList = { }
  12.     for i = 1, #fullList do if fs.isDir(dir.."/"..fullList[i]) then displayList[#displayList + 1] = fullList[i] end end
  13.     for i = 1, #fullList do if not fs.isDir(dir.."/"..fullList[i]) then displayList[#displayList + 1] = fullList[i] end end
  14.     return displayList
  15. end
  16.  
  17. local function draw(dir, offset)
  18.     local width, height = term.getSize()
  19.     term.setBackgroundColor(colors.white)
  20.     term.clear()
  21.     term.setCursorPos(1, 2 - offset)
  22.     term.setBackgroundColor(colors.yellow)
  23.     term.setTextColor(colors.black)
  24.     term.write(".."..string.rep(" ", width - 2))
  25.     for k,v in pairs(list(dir)) do
  26.         term.setCursorPos(1, k + 2 - offset)
  27.         if fs.isDir(dir.."/"..v) then
  28.             term.setBackgroundColor(colors.yellow)
  29.             term.write(v..string.rep(" ", width - #v))
  30.         else
  31.             term.setBackgroundColor(colors.white)
  32.             term.write(v)
  33.         end
  34.     end
  35.     term.setCursorPos(1, 1)
  36.     term.setBackgroundColor(colors.gray)
  37.     term.setTextColor(colors.white)
  38.     if dir ~= "" then
  39.         term.write("/"..dir..string.rep(" ", width - #dir - 2))
  40.     else
  41.         term.write("/"..string.rep(" ", width - 2))
  42.     end
  43.     term.setBackgroundColor(colors.red)
  44.     term.write("X")
  45.     term.setBackgroundColor(colors.gray)
  46.     term.setCursorPos(width, 2)
  47.     term.write("^")
  48.     term.setCursorPos(width, height - 1)
  49.     term.write("v")
  50.     term.setCursorPos(1, height)
  51.     term.setBackgroundColor(colors.lightGray)
  52.     term.setTextColor(colors.black)
  53.     term.write(string.rep(" ", width))
  54. end
  55.  
  56. local function reset()
  57.     term.setCursorPos(1, 1)
  58.     term.setBackgroundColor(colors.black)
  59.     term.setTextColor(colors.white)
  60.     term.clear()
  61. end
  62.  
  63. function open(dir)
  64.     dir = dir or ""
  65.     local offset, width, height = 0, term.getSize()
  66.     draw(dir, offset)
  67.     while true do
  68.         local e = {os.pullEvent()}
  69.         if e[1] == "mouse_click" then
  70.             local x, y = e[3], e[4]
  71.             if x == width and y == 1 then
  72.                 reset()
  73.                 return
  74.             elseif x == width and y == 2 then
  75.                 offset = offset - math.floor(height / 3) + 1
  76.                 if offset < 0 then
  77.                     offset = 0
  78.                 end
  79.             elseif x == width and y == height - 1 then
  80.                 offset = offset + math.floor(height / 3) - 1
  81.             elseif y > 1 and y < height then
  82.                 if y + offset == 2 and dir ~= "" then
  83.                     dir = fs.getDir(dir)
  84.                 else
  85.                     for k,v in pairs(list(dir)) do
  86.                         if k == y + offset - 2 then
  87.                             if fs.isDir(dir.."/"..v) then
  88.                                 dir = fs.getDir(dir.."/"..v.."/_")
  89.                             else
  90.                                 reset()
  91.                                 return fs.getDir(dir.."/"..v.."/_")
  92.                             end
  93.                         end
  94.                     end
  95.                 end
  96.             elseif y == height then
  97.                 term.setCursorPos(1, height)
  98.                 local file = read()
  99.                 if file == "" then
  100.                 elseif file == ".." and dir ~= "" then
  101.                     dir = fs.getDir(dir)
  102.                 elseif file:sub(1, 1) == "/" then
  103.                     if fs.isDir(file) then
  104.                         dir = file:sub(2, #file)
  105.                     else
  106.                         reset()
  107.                         return file:sub(2, #file)
  108.                     end
  109.                 elseif fs.isDir(dir.."/"..file) then
  110.                     dir = fs.getDir(dir.."/"..file.."/_")
  111.                 else
  112.                     reset()
  113.                     return fs.getDir(dir.."/"..file.."/_")
  114.                 end
  115.             end
  116.         elseif e[1] == "mouse_scroll" then
  117.             offset = offset + e[2] * (math.floor(height / 3) - 1)
  118.             if offset < 0 then
  119.                 offset = 0
  120.             end
  121.         elseif e[1] == "term_resize" then
  122.             width, height = term.getSize()
  123.         end
  124.         draw(dir, offset)
  125.     end
  126. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement