Advertisement
CCCoder

FDialog (fixed)

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