-
-
Save ellemenno/3ef0b522dbb23928e224 to your computer and use it in GitHub Desktop.
Directory tree printer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# encoding: utf-8 | |
@bar = '─' | |
@elbow = '└' | |
@pipe = '│' | |
@space = ' ' | |
@tee = '├' | |
@slash = '/' | |
def print_tree(dir = '.', depth = 32, indent = '') | |
return if depth < 0 | |
(entries = Dir.glob(File.join(dir, '*'))).each do |e| | |
f = File.basename(e) | |
d = File.directory?(e) | |
branch = (e == entries.last) ? @elbow : @tee | |
line = "#{indent}#{branch}#{@bar}#{f}" | |
line << @slash if d | |
puts line | |
if d | |
column = (e == entries.last) ? @space : @pipe | |
print_tree(e, depth - 1, "#{indent}#{column}#{@space}") | |
end | |
end | |
end | |
ARGV << '.' << '32' if ARGV.empty? | |
print_tree(ARGV[0], ARGV[1].to_i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment