Created
July 5, 2010 22:23
-
-
Save JoshCheek/464761 to your computer and use it in GitHub Desktop.
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 | |
# A poor man's implementation of the tree app http://trac.macports.org/browser/trunk/dports/sysutils/tree/Portfile | |
# Written in part because I like the app and wanted that functionality on my school computer | |
# and in part because I was going through some videos about the command line, and wanted something interesting | |
# to put in my ~/bin dir | |
# | |
# AUTHOR: Josh Cheek | |
# 5 July 2010 | |
# print how to use it and exit | |
def usage | |
puts "USAGE: #{File.basename __FILE__} [dir]" | |
exit | |
end | |
# check arguments | |
usage if ARGV.length > 1 || ARGV.first.to_s[/^-/] | |
# extract the root directory (defaults to current dir, remove any trailing slash) | |
ROOT = (ARGV.first || '.').sub %r(/?$) , '' | |
# ensure the dir exists | |
usage unless File.directory? ROOT | |
def relative_path_print( prefixes , dir ) | |
fullnames = Dir.glob "#{dir}/*" | |
fullnames.each_with_index do |fullname,index| | |
basename = File.basename fullname | |
if File.directory? fullname | |
if index.next == fullnames.size # if it is the last dir, print with ` and below it is empty | |
print prefixes.join , '`-- ' , basename , "\n" | |
relative_path_print prefixes + [' '] , fullname | |
else # if it is not the last dir, print with |, and below it gets | | |
print prefixes.join , '|-- ' , basename , "\n" | |
relative_path_print prefixes + ['| '] , fullname | |
end | |
elsif index.next == fullnames.size # if is last file, print with ` | |
print prefixes.join , '`-- ' , basename , "\n" | |
else # if is just a file in the list, print with | | |
print prefixes.join , '|-- ' , basename , "\n" | |
end | |
end | |
end | |
puts ROOT | |
relative_path_print [''] , ROOT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment