Skip to content

Instantly share code, notes, and snippets.

@ellemenno
Last active July 22, 2017 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ellemenno/3ef0b522dbb23928e224 to your computer and use it in GitHub Desktop.
Save ellemenno/3ef0b522dbb23928e224 to your computer and use it in GitHub Desktop.
Directory tree printer
#!/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