Advertisement
nitrogenfingers

lsa

May 21st, 2015
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.51 KB | None | 0 0
  1. --[[ lsa: List Advanced
  2.     Allows the display of hidden files and file sizes
  3.     By Nitrogen Fingers
  4. ]]--
  5.  
  6. local tArgs = {...}
  7. local paths = {}
  8. local files = {}
  9. local errors = {}
  10. local _long, _all = false, false
  11. local w, h = term.getSize()
  12. local _,cy = term.getCursorPos()
  13. local _maxlen, _maxsize, _maxdrive = 0,0,0
  14.  
  15. --Performs a screen-specific print that will throw an event if the bottom of the screen is reached.
  16. --Doesn't do any wrapping, so make sure the string is the right size
  17. local function lprint(str, col)
  18.     if term.isColour() and col then term.setTextColour(col)
  19.     else term.setTextColour(colours.white) end
  20.     if cy == 1 then
  21.         os.pullEvent("key")
  22.         _,cy = term.getCursorPos()
  23.     end
  24.     local _,_yold = term.getCursorPos()
  25.     print(str)
  26.     local _,_ynew = term.getCursorPos()
  27.     cy = cy - 1 + (_ynew - _yold)
  28. end
  29.  
  30. --Gets the combined size of a given directory
  31. local function calculateDirectorySize(_fullpath)
  32.     local _size = 0
  33.     local _list = fs.list(_fullpath)
  34.     for i=1,#_list do
  35.         local _newPath = _fullpath.."/".._list[i]
  36.         if fs.isDir(_newPath) then
  37.             _size = _size + calculateDirectorySize(_newPath)
  38.         else
  39.             _size = _size + fs.getSize(_newPath)
  40.         end
  41.     end
  42.     return _size
  43. end
  44.  
  45. --Figures out the spacing for the columns in a long print
  46. --There's a memory inefficiency here; we're doing a recursive call twice.
  47. --But really, meh.
  48. local function calculateColumnWidths(_list, _path)
  49.     for i=1,#_list do
  50.         local _fullpath = shell.resolve(_path).."/".._list[i]
  51.         if fs.isDir(_fullpath) and _long then
  52.             _maxsize = math.max(_maxsize, #tostring(calculateDirectorySize(_fullpath)))
  53.         elseif _long then
  54.             _maxsize = math.max(_maxsize, #tostring(fs.getSize(_fullpath)))
  55.         end
  56.         if _list[i]:sub(1,1) ~= "." or _all then
  57.             _maxlen = math.max(_maxlen, #_list[i])
  58.             _maxdrive = math.max(_maxdrive, #fs.getDrive(_fullpath))
  59.         end
  60.     end
  61. end
  62.  
  63. local function printUsageInfo()
  64.     lprint("Usage: ls [option]... [path]...")
  65.     lprint("Lists all files in the specified directories. Files are sorted by type and alpabetically")
  66.     lprint("-l      use long listing format")
  67.     lprint("-a      do not ignore entries starting with \'.\'")
  68.     lprint("-h      display usage info")
  69. end
  70.  
  71. local function printListing(_list, _path)
  72.     local _files = {}
  73.     for i=#_list,1,-1 do
  74.         local _fullPath = shell.resolve(_path).."/".._list[i]
  75.    
  76.         if not _all and _list[i]:sub(1,1) == "." then
  77.             table.remove(_list, i)
  78.         elseif not fs.isDir(_fullPath) then
  79.             table.insert(_files, table.remove(_list, i))
  80.         end
  81.     end
  82.     table.sort(_files)
  83.     table.sort(_list)
  84.    
  85.     local _c = ""
  86.     if _long then
  87.         for i=1,#_list do
  88.             local _fullPath = shell.resolve(_path).."/".._list[i]
  89.             local _size = tostring(calculateDirectorySize(_fullPath))
  90.             term.setTextColour(colours.white)
  91.             local _read = "d"
  92.             if fs.isReadOnly(_fullPath) then _read = _read.."-" else _read = _read.."w" end
  93.             local _drive = fs.getDrive(_fullPath)
  94.             term.write(_read.." "..string.rep(" ", _maxsize - #_size).._size.." ".._drive..
  95.                     string.rep(" ", _maxdrive - #_drive + 1))
  96.             if term.isColour() then term.setTextColour(colours.green) end
  97.             term.write(_list[i])
  98.             lprint("")
  99.         end
  100.         for i=1,#_files do
  101.             local _fullPath = shell.resolve(_path).."/".._files[i]
  102.             local _size = tostring(fs.getSize(_fullPath))
  103.             local _read = "f"
  104.             local _drive = fs.getDrive(_fullPath)
  105.             if fs.isReadOnly(_fullPath) then _read = _read.."-" else _read = _read.."w" end
  106.             lprint(_read.." "..string.rep(" ", _maxsize - #_size).._size.." ".._drive..
  107.                     string.rep(" ", _maxdrive - #_drive + 1).._files[i], colours.white)
  108.         end
  109.     else
  110.         for i=1,#_list do
  111.             if #_c + _maxlen + 1 > w then
  112.                 lprint(_c, colours.green)
  113.                 _c = ""
  114.             end
  115.             _c = _c.._list[i]..string.rep(" ", _maxlen - #_list[i] + 1)
  116.         end
  117.         if _c ~= "" then
  118.             lprint(_c, colours.green)
  119.             _c = ""
  120.         end
  121.         for i=1,#_files do
  122.             if #_c + _maxlen + 1 > w then
  123.                 lprint(_c, colours.white)
  124.                 _c = ""
  125.             end
  126.             _c = _c.._files[i]..string.rep(" ", _maxlen - #_files[i] + 1)
  127.         end
  128.         if _c ~= "" then lprint(_c, colours.white) end
  129.     end
  130. end
  131.  
  132. for i=1,#tArgs do
  133.     if tArgs[i]:sub(1,1) == "-" then
  134.         if #paths > 0 or #files > 0 then
  135.             table.insert(errors, "Option "..tArgs[i].." found after path")
  136.         end
  137.    
  138.         for j=2,#tArgs[i] do
  139.             local _comm = tArgs[i]:sub(j,j)
  140.             if _comm == "l" then
  141.                 _long = true
  142.             elseif _comm == "a" then
  143.                 _all = true
  144.             elseif _comm == "h" then
  145.                 printUsageInfo()
  146.                 return
  147.             else
  148.                 table.insert(errors, "Unrecognized option \'"..tArgs[i].."\'. Use -h for help.")
  149.             end
  150.         end
  151.     else
  152.         if not fs.exists(shell.resolve(tArgs[i])) then
  153.             table.insert(errors, "No such file or directory: "..tArgs[i])
  154.         elseif fs.isDir(shell.resolve(tArgs[i])) then
  155.             table.insert(paths, tArgs[i])
  156.             calculateColumnWidths(fs.list(shell.resolve(tArgs[i])), tArgs[i])
  157.         else
  158.             table.insert(files, tArgs[i])
  159.             _maxlen = math.max(_maxlen, #tArgs[i])
  160.             _maxsize = math.max(_maxsize, #tostring(fs.getSize(shell.resolve(tArgs[i]))))
  161.             _maxdrive = math.max(_maxdrive, #fs.getDrive(shell.resolve(tArgs[i])))
  162.         end
  163.     end
  164. end
  165.  
  166. if #paths == 0 and #files == 0 then
  167.     table.insert(paths, ".")
  168.     calculateColumnWidths(fs.list(shell.resolve(".")), ".")
  169. end
  170. for _,err in pairs(errors) do lprint(err, colours.red) end
  171. if #errors > 0 then
  172.     return
  173. end
  174.  
  175.  
  176. if #files > 0 then
  177.     printListing(files, ".")
  178.     if #paths > 0 then lprint("") end
  179. end
  180.  
  181. for i=1,#paths do
  182.     if #paths > 1 or #files > 1 then lprint(paths[i]..":") end
  183.     printListing(fs.list(shell.resolve(paths[i])), paths[i])
  184.     if i ~= #paths then lprint("") end
  185. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement