CrazedProgrammer

tree

Jan 9th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.88 KB | None | 0 0
  1. --[[
  2. "tree" script for CC
  3.  
  4. The MIT License (MIT)
  5. Copyright (c) 2017 CrazedProgrammer
  6.  
  7. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  8. associated documentation files (the "Software"), to deal in the Software without restriction,
  9. including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do
  11. so, subject to the following conditions:
  12.  
  13. The above copyright notice and this permission notice shall be included in all copies or
  14. substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  17. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  18. PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  20. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. ]]
  23.  
  24. local tree = { }
  25.  
  26. local function scan(path, depth)
  27.     local entries = fs.list(path)
  28.     for i = 1, #entries do
  29.         local epath = path.."/"..entries[i]
  30.         tree[#tree + 1] = {depth, fs.isDir(epath), entries[i]}
  31.         if fs.isDir(epath) then
  32.             scan(epath, depth + 1)
  33.         end
  34.     end
  35. end
  36.  
  37. scan(#({...}) == 0 and shell.dir() or shell.resolve(({...})[1]), 0)
  38.  
  39. local sw, sh = term.getSize()
  40. for i = 1, #tree do
  41.     local cx, cy = term.getCursorPos()
  42.     if cy == sh and i >= sh then
  43.         term.write("...")
  44.         os.pullEvent("key")
  45.         term.setCursorPos(1, cy)
  46.         term.write("   ")
  47.         term.setCursorPos(1, cy)
  48.     end
  49.     write(("-   "):rep(tree[i][1]))
  50.     term.setTextColor(term.isColor() and (tree[i][2] and colors.green or colors.white) or colors.white)
  51.     print(tree[i][3])
  52.     term.setTextColor(colors.white)
  53. end
Add Comment
Please, Sign In to add comment