Class: TreeRb::CliTree

Inherits:
Object
  • Object
show all
Defined in:
lib/tree_rb/cli/cli_tree.rb

Overview

Command Line Interface to Tree

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.runObject



9
10
11
# File 'lib/tree_rb/cli/cli_tree.rb', line 9

def self.run
  self.new.parse_args(ARGV)
end

Instance Method Details

#options_parser(options) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/tree_rb/cli/cli_tree.rb', line 13

def options_parser(options)
  parser = OptionParser.new
  parser.banner = 'Usage: tree.rb [options] [directory]'
  parser.separator 'list contents of directories in a tree-like format'
  parser.separator 'this is a almost :-) a clone of tree unix command written in ruby'
  parser.separator 'Code https://github.com/tokiro/treevisitor. Feedback to [email protected]'

  #
  # Generic
  #
  parser.separator ""
  parser.separator "Generic options: "

  parser.on("--help", "Show this message") do
    puts parser
    options[:exit] = 1
  end

  parser.on("--version", "Show the version") do
    puts "tree.rb version #{TreeRb::VERSION}"
    options[:exit] = 1
  end

  parser.on("-v", "--[no-]verbose", "Run verbosely") do |v|
    options[:verbose] = v
  end

  parser.on("-q", "--quiet", "quiet mode as --no-verbose") do
    options[:verbose] = false
  end

  parser.on("-o [FILE]", "--output [FILE]", String) do |v|
    if options[:output]
      puts "only one file of output can be used"
      options[:exit] = true
    end
    options[:output] = v
  end

  options[:force_overwrite_output] = false
  parser.on("--force", "overwrite output") do
    options[:force_overwrite_output] = true
  end

  #
  # Filters
  #
  parser.separator ""
  parser.separator "Filter options: "

  parser.on("-a", "All file are listed i.e. include dot files") do
    options[:all_files] = true
  end

  parser.on("-d", "List directories only") do
    options[:only_directories] = true
  end

  options[:only_files] = false
  parser.on("--only-files", "show only file implies -i") do
    options[:only_files] = true
    options[:show_indentation] = false
  end

  formats = %w[build-dir print-dir json d3js html_partition html_tree html_treemap yaml sqlite dircat]
  # algo_aliases = { "b" => "build-dir", "v" => "print-dir", "j" => "json", "y" => "yaml", "s" => "sqlite" }
  # algo_list = (algo_aliases.keys + formats).join(',')
  parser.on('--format FORMAT', formats, 'select a output format', "  (#{formats})") do |format|
    options[:output_plugins] = format
  end

  options[:max_level] = nil
  parser.on("-L [LEVEL]", Integer, 'Max display depth of the directory tree.') do |l|
    options[:max_level] = l
  end

  #
  # Presentation options
  #
  parser.separator ""
  parser.separator "print options:"

  #
  # begin colorize
  #
  # copied from tree man page
  parser.on("-n", 'Turn colorization off always, over-ridden by the -C option.') do
    options[:colorize_force] = false
  end

  # copied from tree man page
  parser.on("-C",
            'Turn colorization on always, using built-in color',
            "  defaults if the LS_COLORS environment variable is not set.",
            "  Useful to colorize output to a pipe.") do
    options[:colorize_force] = true
  end
  #
  # end colorize
  #

  options[:ansi_line_graphics] = false
  parser.on("-A", "Turn on ANSI line graphics hack when printing the indentation lines.") do
    options[:ansi_line_graphics] = true
  end

  options[:show_full_path] = false
  parser.on("-f", "Prints the full path prefix for each file.") do
    options[:show_full_path] = true
  end

  parser.on("-s", "Print the size of each file in bytes along with the name.") do
    options[:show_size] = true
  end

  parser.on("-h",
            "Print the size of each file but in a more human readable way,",
            "  e.g. appending  a  size  letter  for  kilobytes  (K), megabytes (M),",
            "  gigabytes (G), terabytes (T), petabytes (P) and exabytes (E).") do
    options[:show_size_human] = true
  end

  options[:show_indentation] = true
  parser.on("-i",
            'Makes tree not print the indentation lines, ',
            '  useful when used in conjunction with the -f option.') do
    options[:show_indentation] = false
  end

  options[:show_md5] = false
  parser.on("--md5", "show md5 of file") do
    options[:show_md5] = true
  end

  options[:show_sha1] = false
  parser.on("--sha1", "show sha1 of a file") do
    options[:show_sha1] = true
  end

  options[:show_report] = true
  parser.on("--no-report", "Omits printing of the file and directory report at the end of the tree listing.") do
    options[:show_report] = false
  end

  parser.separator ""
  parser.separator "shortcut options:"

  options[:show_md5sum] = false
  parser.on("--md5sum", "show ake md5sum implies -i and --only-files") do
    options[:only_files] = true
    options[:show_md5sum] = true
    options[:show_indentation] = false
    options[:show_report] = false
  end

  options[:show_sha1sum] = false
  parser.on('--sha1sum', 'show ake sha1sum output implies -i and --only-files') do
    options[:only_files] = true
    options[:show_sha1sum] = true
    options[:show_indentation] = false
    options[:show_report] = false
  end


  parser
end

#parse_args(argv) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/tree_rb/cli/cli_tree.rb', line 180

def parse_args(argv)
  options = {:verbose => true, :force => false, :output_plugins => 'build-dir'}
  parser = options_parser(options)

  begin
    rest = parser.parse(argv)
  rescue OptionParser::InvalidOption => e
    $stderr.puts e.to_s
    $stderr.puts 'try --help for help'
    return false
  rescue OptionParser::MissingArgument => e
    $stderr.puts e.to_s
    $stderr.puts 'try --help for help'
    return false
  rescue OptionParser::InvalidArgument => e
    $stderr.puts e.to_s
    $stderr.puts 'try --help for help'
    return false
  end

  unless options[:exit].nil?
    return options[:exit]
  end

  #
  # option: output, force
  #
  output = $stdout
  if options[:output]
    filename = options[:output]
    if File.exist?(filename) and not options[:force_overwrite_output]
      $stderr.puts "catalog '#{filename}' exists use --force to overwrite"
      return 0
    end
    output = File.open(filename, 'w')
    $stderr.puts "Writing file '#{filename}'"
  end

  if options[:colorize_force]
    options[:colorize] = options[:colorize_force]
  else
    options[:colorize] = output.isatty
  end

  # TODO: capture CTRL^C to avoid show the stack trace
  # http://ruby-doc.org/core-1.9.3/Kernel.html#method-i-trap
  Signal.trap('INT') do
    puts 'intercepted ctr+c'
    exit
  end

  if rest.length < 1
    dirname = Dir.pwd
  else
    dirname = rest[0]
  end

  #
  # 1. build dir tree walker
  #

  dirname = File.expand_path(dirname)
  begin
    directory_tree_walker = DirTreeWalker.new(dirname, options)
  rescue ArgumentError => e
    $stderr.puts e.to_s
    return false
  end
  unless options[:all_files]
    directory_tree_walker.ignore(/^\.[^.]+/) # ignore all file starting with "."
  end
  directory_tree_walker.visit_file = !options[:only_directories]

  formats = {
      'build-dir' => Proc.new do |directory_tree_walker,output,options|

        visitor = BuildDirTreeVisitor.new(options)
        directory_tree_walker.run(visitor)

        output.puts visitor.root.to_str('', options)

        if options[:show_report]
          output.puts
          output.puts "#{visitor.nr_directories} directories, #{visitor.nr_files} files"
        end
      end,

      'print-dir' => Proc.new do |directory_tree_walker,output,options|
        visitor = PrintDirTreeVisitor.new
        directory_tree_walker.run(visitor)
      end,

      'yaml' => Proc.new do |directory_tree_walker,output,options|
        visitor = DirectoryToHashVisitor.new(dirname)
        root = directory_tree_walker.run(visitor).root
        output.puts root.to_yaml
      end,

      'json' => Proc.new do |directory_tree_walker,output,options|
        visitor = DirectoryToHashVisitor.new(dirname)
        root = directory_tree_walker.run(visitor).root
        begin
          output.puts JSON.pretty_generate(root)
        rescue JSON::NestingError => e
          $stderr.puts "#{File.basename(__FILE__)}:#{__LINE__} #{e.to_s}"
        end
      end,

      'd3js' => Proc.new do |directory_tree_walker,output,options|
        require 'tree_rb/output_plugins/html/d3js_output'
        D3jsOutput.new.run(directory_tree_walker, dirname, nil, output)
      end,

      'html_partition' => Proc.new do |directory_tree_walker,output,options|
        require 'tree_rb/output_plugins/html/d3js_output'
        D3jsOutput.new.run(directory_tree_walker, dirname, 'd3js_layout_partition.erb', output)
      end,

      'html_tree' => Proc.new do |directory_tree_walker,output,options|
        require 'tree_rb/output_plugins/html/d3js_output'
        D3jsOutput.new.run(directory_tree_walker, dirname, 'd3js_layout_tree.erb', output)
      end,

      'html_treemap' => Proc.new do |directory_tree_walker,output,options|
        require 'tree_rb/output_plugins/html/d3js_output'
        D3jsOutput.new.run(directory_tree_walker, dirname, 'd3js_layout_treemap.erb', output)
      end,

      'dircat' => Proc.new do |directory_tree_walker,output,options|
        require 'tree_rb/output_plugins/dircat/dircat_output'
        DirCatOutput.new.run(directory_tree_walker, options)
      end,

      'sqlite' => Proc.new do |directory_tree_walker,output,options|
        require 'tree_rb/output_plugins/sqlite/sqlite_output'
        SqliteOutput.new.run(directory_tree_walker, output, options)
      end
  }

  format = options[:output_plugins]

  p = formats[format]
  if p
    p.call(directory_tree_walker, output, options)
  else
    puts "unknown format #{options[:output_plugins]} specified"
  end

  0
end