Advertisement
XDjackieXD

better ls - computercraft

Dec 3rd, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.32 KB | None | 0 0
  1. function table.contains(table, element)  -- checks if a table contains a given value
  2.   for _, value in pairs(table) do
  3.     if value == element then
  4.       return true
  5.     end
  6.   end
  7.   return false
  8. end
  9.  
  10.  
  11. --end functions
  12.  
  13. local tArgs = { ... }
  14.  
  15.  
  16. local dir = ""  --dir to list
  17. local option = {} --given options
  18.  
  19. for n, arg in pairs(tArgs) do  --search options and dir to list
  20.   if string.sub( arg, 1, 1 ) == "-" then
  21.     for i=1, ( string.len(arg) - 1 ) do
  22.       table.insert(option, string.sub(arg, i+1, i+1))
  23.     end
  24.   else
  25.     dir = arg
  26.   end
  27. end
  28.  
  29.  
  30.  
  31. -- Get all the files in the directory
  32. local sDir = shell.dir()
  33. if dir ~= "" then
  34.     sDir = shell.resolve( dir )
  35. end
  36.  
  37.  
  38. -- Sort into dirs/files, and calculate column count
  39. local tAll = fs.list( sDir )
  40. local tFiles = {}
  41. local tDirs = {}
  42.  
  43. for n, sItem in pairs( tAll ) do
  44.     if string.sub( sItem, 1, 1 ) ~= "." or table.contains(option,"a") then --if option "a" is given, also print dirs starting with "."
  45.         local sPath = fs.combine( sDir, sItem )
  46.         if fs.isDir( sPath ) then
  47.             table.insert( tDirs, sItem )
  48.         else
  49.             table.insert( tFiles, sItem )
  50.         end
  51.     end
  52. end
  53. table.sort( tDirs )
  54. table.sort( tFiles )
  55.  
  56. if term.isColour() then
  57.     textutils.pagedTabulate( colors.green, tDirs, colours.white, tFiles )
  58. else
  59.     textutils.pagedTabulate( tDirs, tFiles )
  60. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement