Top Level Namespace

Defined Under Namespace

Modules: Redwood

Instance Method Summary collapse

Instance Method Details

#optparseObject

Redwood is an implementation of ‘tree` in Ruby

Usage: redwood [ OPTIONS ] [ DIRECTORY ]

Learn more about the original at: mama.indstate.edu/users/ice/tree/



11
# File 'bin/redwood', line 11

require 'optparse'

#setup_tree(tree, options) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'bin/redwood', line 68

def setup_tree(tree, options)
  if options[:depth]
    abort 'Invalid level, must be greater than 0' if options[:depth] <= 1
    tree.descendants.each {|f| f.unlink if f.depth > options[:depth] }
  end
  if !options[:follow_symlinks]
    tree.descendants.each do |f|
      if f.symlink?        
        f.value << " -> #{f.readlink}"
      end
      f.unlink if f.parent.symlink?
    end
  end
  if options[:only_directories]
    tree.descendants.each {|f| f.unlink if !f.directory? }
  end
  if !options[:show_hidden_files]
    tree.descendants.each do |f| 
        f.unlink if f.basename.index('.').eql?(0)
    end
  end
  tree.walk {|f| f.value = f.path } if options[:full_path]
  
  if options[:date]
    tree.descendants.each {|f| f.value.insert(0,"[#{f.send(options[:date]).strftime('%b %d %H:%M')}] ") }
  end
  if options[:size]
    tree.descendants.each {|f| f.value.insert(0,"[#{f.size}] ") if f.size? }
  end
  if options[:ftype]
    tree.descendants.each do |f|
      f.value << "|" if f.ftype == 'fifo'
      if f.symlink?
        f.value << "@"
      elsif f.directory?
        f.value << "/"
      end
      f.value << "*" if f.executable? && f.file?
      f.value << "=" if f.socket?
    end
  end
  tree
end

#statement(tree) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'bin/redwood', line 53

def statement(tree)
  directories = 0
  files = 0
  tree.descendants.each do |f|
    if f.directory?
      directories += 1
    else
      files += 1
    end
  end
  d = directories.eql?(1) ? 'directory' : 'directories'
  f = files.eql?(1) ? 'file' : 'files'
  "#{directories} #{d}, #{files} #{f}"
end

#usageObject



13
14
15
16
17
18
# File 'bin/redwood', line 13

def usage
  File.readlines(__FILE__).
  grep(/^##.*/).
  map { |line| line.chomp[3..-1] }.
  join("\n")
end