JosephPecoraro / scripts
- Source
- Commits
- Network (0)
- Issues (0)
- Graphs
-
Tree:
10364cf
scripts / tree
| 40d3b82a » | JosephPecoraro | 2009-06-07 | 1 | #!/usr/bin/env ruby | |
| 2 | # Author: Joseph Pecoraro | ||||
| 3 | # Date: Sunday, June 7, 2009 | ||||
| 4 | # Description: Tree Listing of a Directory | ||||
| 56c41b06 » | JosephPecoraro | 2009-06-08 | 5 | # Delim and Spacer work best when they are the same length | |
| 40d3b82a » | JosephPecoraro | 2009-06-07 | 6 | ||
| 7 | class DirTree | ||||
| 8 | |||||
| 56c41b06 » | JosephPecoraro | 2009-06-08 | 9 | def self.list(dir=Dir.pwd, delim='+-', spacer='| ', hidden=false) | |
| 10 | list = [] | ||||
| 40d3b82a » | JosephPecoraro | 2009-06-07 | 11 | depth = [] | |
| 12 | Dir.chdir(dir) | ||||
| 13 | spacer_length=spacer.length | ||||
| 14 | prefix = [' '*delim.length] | ||||
| 56c41b06 » | JosephPecoraro | 2009-06-08 | 15 | list = hidden ? Dir.glob('**/*', File::FNM_DOTMATCH).delete_if { |x| x=~/\.(\.|DS_Store)?$/ } : Dir['**/*'] | |
| 40d3b82a » | JosephPecoraro | 2009-06-07 | 16 | puts "#{delim}#{File.basename(dir)}" | |
| 17 | list.each_with_index do |filename, index| | ||||
| 18 | orig = filename.dup | ||||
| 19 | |||||
| 20 | # Check Depth Array against next string | ||||
| 21 | i = 0 | ||||
| 22 | depth.each do |lvl| | ||||
| 23 | unless filename.sub!(/^#{lvl}\//,'').nil? | ||||
| 24 | i += 1 | ||||
| 25 | else | ||||
| 26 | break | ||||
| 27 | end | ||||
| 28 | end | ||||
| 29 | |||||
| 30 | # Clean the Prefix - Chop characters off the end | ||||
| 31 | while i != depth.size | ||||
| 32 | depth.pop | ||||
| 33 | prefix.pop | ||||
| 34 | end | ||||
| 35 | |||||
| 36 | # Output No Leading Pipe if nothing is on the same level | ||||
| 37 | print "#{prefix.join}#{delim}#{filename}" | ||||
| 38 | if File.directory?(orig) | ||||
| 39 | print '/' | ||||
| 40 | another = false | ||||
| 41 | dir_prefix = depth.join('/') | ||||
| 42 | index.upto(list.size-1) do |n| | ||||
| 43 | str = list[n] | ||||
| 44 | if str =~ /^#{dir_prefix}/ | ||||
| 45 | str = str.sub(/^#{dir_prefix}\/?/,'') | ||||
| 46 | if str !~ /^#{filename}/ | ||||
| 47 | another = true | ||||
| 48 | break | ||||
| 49 | end | ||||
| 50 | end | ||||
| 51 | end | ||||
| 52 | depth << filename | ||||
| 53 | prefix << ( (another) ? spacer : ' '*spacer_length ) | ||||
| 54 | end | ||||
| 55 | puts | ||||
| 56 | |||||
| 57 | end | ||||
| 58 | end | ||||
| 59 | |||||
| 60 | end | ||||
| 61 | |||||
| 62 | |||||
| 63 | # When Run as a Script | ||||
| 64 | if $0 == __FILE__ | ||||
| 65 | trap("INT") { puts "\nInterrupted."; exit } | ||||
| 66 | require 'optparse' | ||||
| 56c41b06 » | JosephPecoraro | 2009-06-08 | 67 | hidden = false | |
| 40d3b82a » | JosephPecoraro | 2009-06-07 | 68 | OptionParser.new do |opts| | |
| 56c41b06 » | JosephPecoraro | 2009-06-08 | 69 | opts.banner = "usage: tree [options] [dir ...]" | |
| 70 | opts.on('-a', '--all', 'Show hidden files') { hidden = true } | ||||
| 71 | opts.on('-h', '--help', 'Show this help.') { puts opts; exit } | ||||
| 40d3b82a » | JosephPecoraro | 2009-06-07 | 72 | end.parse! | |
| 73 | ARGV << '.' if ARGV.empty? | ||||
| 74 | ARGV.each do |dir| | ||||
| 56c41b06 » | JosephPecoraro | 2009-06-08 | 75 | DirTree.list( File.expand_path(dir), '+--', '| ', hidden ) | |
| 40d3b82a » | JosephPecoraro | 2009-06-07 | 76 | puts | |
| 77 | end | ||||
| 78 | end | ||||
