Advertisement
Guest User

ComputerCraft: Tree Command (not final) by Espen

a guest
Feb 8th, 2012
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.20 KB | None | 0 0
  1. -- Context: http://www.computercraft.info/forums2/index.php?/topic/44-request-windows-tree-command/
  2.  
  3. local tArgs = { ... }
  4. local currentPath = shell.resolve(".")
  5. local lineCounter = 0
  6. local pageFlip = false
  7. local askingForHelp = false
  8.  
  9. --[[ Process command line arguments ]]
  10. if #tArgs > 1 then  -- More than one argument.
  11.     if ( tArgs[1] == string.lower( "-m" ) ) or ( tArgs[1] == string.lower( "-more" ) ) then
  12.         pageFlip = true
  13.         currentPath = shell.resolve( tArgs[2] )
  14.     elseif ( tArgs[2] == string.lower( "-m" ) ) or ( tArgs[2] == string.lower( "-more" ) ) then
  15.         pageFlip = true
  16.         currentPath = shell.resolve( tArgs[1] )
  17.     else
  18.         currentPath = shell.resolve( tArgs[1] )
  19.     end
  20. else    -- Only one argument.
  21.     if ( tArgs[1] == string.lower( "-h" ) ) or ( tArgs[1] == string.lower( "-help" ) ) then
  22.         askingForHelp = true
  23.     elseif tArgs[1] ~= nil then
  24.         currentPath = shell.resolve( tArgs[1] )
  25.     end
  26. end
  27.  
  28. function printUsage()
  29.     print( "Usage: tree [-more] [-help] [<DIRECTORY>]\n" )
  30.    
  31.     print( "-m  or  -more   Enable page flipping." )
  32.     print( "-h  or  -help   Show this help." )
  33.     print( "<DIRECTORY>     Name of the directory." )
  34.     print( "                (Default is current directory)" )
  35. end
  36.  
  37. function listDir( path, prefix )
  38.     if path == "" then path = "\\" end  -- Path of execution.
  39.    
  40.     -- Iterate through every element (file or dir) in the path.
  41.     for k, v in pairs( fs.list( path ) ) do
  42.         ---[[ PAGE-FLIP
  43.         if pageFlip then
  44.             lineCounter = lineCounter + 1
  45.             if lineCounter >= 17 then
  46.                 write("Press any key to continue")
  47.                 read()
  48.                 term.setCursorPos( 1, 1 )
  49.                 term.clear()
  50.                 lineCounter = 0
  51.             end
  52.         end
  53.         --]]
  54.         -- Print file/dir name.
  55.         print( prefix.."-- "..v )
  56.         -- Traverse next directory, if any.
  57.         local nextDir = path.."\\"..v
  58.         if fs.isDir( nextDir ) then listDir( nextDir, prefix.."   |" ) end
  59.     end
  60. end
  61.  
  62. if askingForHelp then
  63.     printUsage()
  64. else
  65.     print( "Listing Directory: "..currentPath )
  66.     listDir( currentPath, "|" )
  67. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement