require 'dumb_down_viewer' require 'dumb_down_viewer/tree_view_builder' require 'optparse_plus' module DumbDownViewer module Cli FORMATTER = { default: DumbDownViewer::TreeViewBuilder::PlainTextFormat, csv: DumbDownViewer::TreeViewBuilder::CSVFormat, tsv: DumbDownViewer::TreeViewBuilder::CSVFormat, tree_csv: DumbDownViewer::TreeViewBuilder::TreeCSVFormat } OPTIONS = < number_of_files end if tree.sub_nodes.size > number_of_files tree.directories.clear tree.files.clear end pruner.visit(tree, nil) end def self.format_json(tree, options) json = DumbDownViewer::JSONConverter.dump(tree) json + $/ end def self.format_xml(tree, options) DumbDownViewer::XMLConverter.dump(tree) end 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 def self.format_tree(tree, options) if options[:file_limit] and tree.sub_nodes.empty? '' elsif options[:json] format_json(tree, options) elsif options[:xml] format_xml(tree, options) else format_tree_with_builder(tree, options) end end 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 def self.total_count(tree) count = DumbDownViewer::TotalNodeCount.count(tree) "#{$/}#{count[:directories]} directories, #{count[:files]} files" end def self.copy_to(tree, options) DumbDownViewer::TreeDuplicator.duplicate(tree, options[:copy_to]) end def self.open_output(filename) if filename open(File.expand_path(filename), "wb") do |out| yield out end else yield STDOUT end end end end