Skip to content

Instantly share code, notes, and snippets.

@audibleblink
Last active Aug 29, 2015
Embed
What would you like to do?
reimplemented unix tree
#!/usr/bin/env ruby
# without the glyphs, if you prefer
def list entry=Dir.pwd
Dir.entries(entry)[2..-1].each do |item|
puts File.basename(item)
list("#{entry}/#{item}") if File.directory?("#{entry}/#{item}")
end
end
ARGV.empty? ? list : list(ARGV.first)
#!/usr/bin/env ruby
def tree entry=Dir.pwd, indent=0
entries = Dir.entries(entry)[2..-1]
entries.each do |item|
printable_item = entries.last != item ?
"├── #{File.basename(item)}" :
"└── #{File.basename(item)}"
puts "#{'│ ' * indent}#{printable_item}"
tree("#{entry}/#{item}", indent + 1) if File.directory?("#{entry}/#{item}")
end
end
ARGV.empty? ? tree : tree(ARGV.first)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment