Last active
August 29, 2015 14:24
-
-
Save audibleblink/9d0a2629f72286d1674d to your computer and use it in GitHub Desktop.
reimplemented unix tree
This file contains hidden or 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 | |
# 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) |
This file contains hidden or 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 | |
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