module DumbDownViewer::Cli

Constants

FORMATTER
OPTIONS

Public Class Methods

add_summary(tree, options) click to toggle source
# File lib/dumb_down_viewer/cli.rb, line 157
def self.add_summary(tree, options)
  if summary_type = options[:summary] and summary_type != :default
    STDERR.puts "Unknown option #{summary_type} for --summary: default is used instead."
  end
  visitor = DumbDownViewer::FileCountSummary.new
  tree.accept(visitor, nil)
  DumbDownViewer::FileCountSummary::NodeFormat.new
end
copy_to(tree, options) click to toggle source
# File lib/dumb_down_viewer/cli.rb, line 192
def self.copy_to(tree, options)
  DumbDownViewer::TreeDuplicator.duplicate(tree, options[:copy_to])
end
execute() click to toggle source
# File lib/dumb_down_viewer/cli.rb, line 94
def self.execute
  options = parse_command_line_options
  tree = DumbDownViewer.build_node_tree(ARGV[0] || '.')
  prune_dot_files(tree) unless options[:show_all]
  prune_dirs_with_more_than(tree, options[:file_limit]) if options[:file_limit]
  prune_level(tree, options[:level]) if options[:level]
  select_match(tree, options) if options[:match]
  ignore_match(tree, options) if options[:ignore_match]
  prune_files(tree) if options[:directories]
  copy_to(tree, options) if options[:copy_to]
  open_output(options[:output]) do |out|
    out.print format_tree(tree, options)
    out.puts total_count(tree) if options[:total_count]
  end
end
format_json(tree, options) click to toggle source
# File lib/dumb_down_viewer/cli.rb, line 148
def self.format_json(tree, options)
  json = DumbDownViewer::JSONConverter.dump(tree)
  json + $/
end
format_tree(tree, options) click to toggle source
# File lib/dumb_down_viewer/cli.rb, line 166
def self.format_tree(tree, options)
  if options[:file_limit] and tree.sub_nodes.empty?
    ''
  elsif options[:json] or options[:format] == :json
    format_json(tree, options)
  elsif options[:xml] or options[:format] == :xml
    format_xml(tree, options)
  else
    format_tree_with_builder(tree, options)
  end
end
format_tree_with_builder(tree, options) click to toggle source
# File lib/dumb_down_viewer/cli.rb, line 178
def self.format_tree_with_builder(tree, options)
  style = options[:style]
  col_sep = options[:format] == :tsv ? "\t" : ','
  node_format = options[:summary] ? add_summary(tree, options) : nil
  builder = TreeViewBuilder.create(tree)
  formatter = FORMATTER[options[:format]].new(style, col_sep, node_format)
  formatter.format_table(builder.tree_table)
end
format_xml(tree, options) click to toggle source
# File lib/dumb_down_viewer/cli.rb, line 153
def self.format_xml(tree, options)
  DumbDownViewer::XMLConverter.dump(tree)
end
ignore_match(tree, options) click to toggle source
# File lib/dumb_down_viewer/cli.rb, line 131
def self.ignore_match(tree, options)
  pat = Regexp.compile(options[:ignore_match], options[:ignore_case])
  pruner = DumbDownViewer::TreePruner.create(false) {|node| not node.directory? and pat =~ node.name }
  pruner.visit(tree, nil)
end
open_output(filename) { |out| ... } click to toggle source
# File lib/dumb_down_viewer/cli.rb, line 196
def self.open_output(filename)
  if filename
    open(File.expand_path(filename), "wb") do |out|
      yield out
    end
  else
    yield STDOUT
  end
end
parse_command_line_options() click to toggle source
# File lib/dumb_down_viewer/cli.rb, line 68
def self.parse_command_line_options
  options = { style: :default, format: :default }
  OptionParser.new_with_yaml(OPTIONS) do |opt|
    opt.version = DumbDownViewer::VERSION
    opt.inherit_ruby_options("E") # -E --encoding

    opt.on(:style) {|style| options[:style] = style.to_sym }
    opt.on(:format) {|format| options[:format] = format.to_sym }
    opt.on(:directories) {|directories| options[:directories] = true }
    opt.on(:level) {|level| options[:level] = level.to_i }
    opt.on(:match) {|pattern| options[:match] = pattern }
    opt.on(:ignore_match) {|pattern| options[:ignore_match] = pattern }
    opt.on(:show_all) { options[:show_all] = true }
    opt.on(:ignore_case) { options[:ignore_case] = true }
    opt.on(:output) {|output_file| options[:output] = output_file }
    opt.on(:file_limit) {|number_of_files| options[:file_limit] = number_of_files.to_i }
    opt.on(:summary) {|summary_type| options[:summary] = summary_type.to_sym }
    opt.on(:json) { options[:json] = true }
    opt.on(:xml) { options[:xml] = true }
    opt.on(:total_count) { options[:total_count] = true }
    opt.on(:copy_to) {|dest_dir| options[:copy_to] = dest_dir }
    opt.parse!
  end
  options
end
prune_dirs_with_more_than(tree, number_of_files) click to toggle source
# File lib/dumb_down_viewer/cli.rb, line 137
def self.prune_dirs_with_more_than(tree, number_of_files)
  pruner = DumbDownViewer::TreePruner.create(false) do |node|
    node.directory? and node.sub_nodes.size > number_of_files
  end
  if tree.sub_nodes.size > number_of_files
    tree.directories.clear
    tree.files.clear
  end
  pruner.visit(tree, nil)
end
prune_dot_files(tree) click to toggle source
# File lib/dumb_down_viewer/cli.rb, line 110
def self.prune_dot_files(tree)
  pruner = DumbDownViewer::TreePruner.create(false) {|node| node.name.start_with? '.' }
  pruner.visit(tree, nil)
end
prune_files(tree) click to toggle source
# File lib/dumb_down_viewer/cli.rb, line 115
def self.prune_files(tree)
  pruner = DumbDownViewer::TreePruner.create {|node| node.directory? }
  pruner.visit(tree, nil)
end
prune_level(tree, level) click to toggle source
# File lib/dumb_down_viewer/cli.rb, line 120
def self.prune_level(tree, level)
  pruner = DumbDownViewer::TreePruner.create {|node| node.depth <= level }
  pruner.visit(tree, nil)
end
select_match(tree, options) click to toggle source
# File lib/dumb_down_viewer/cli.rb, line 125
def self.select_match(tree, options)
  pat = Regexp.compile(options[:match], options[:ignore_case])
  pruner = DumbDownViewer::TreePruner.create {|node| node.directory? or pat =~ node.name }
  pruner.visit(tree, nil)
end
total_count(tree) click to toggle source
# File lib/dumb_down_viewer/cli.rb, line 187
def self.total_count(tree)
  count = DumbDownViewer::TotalNodeCount.count(tree)
  "#{$/}#{count[:directories]} directories, #{count[:files]} files"
end