Module: CommandLineReporter

Includes:
OptionsValidator
Defined in:
lib/command_line_reporter.rb,
lib/command_line_reporter/row.rb,
lib/command_line_reporter/table.rb,
lib/command_line_reporter/column.rb,
lib/command_line_reporter/version.rb,
lib/command_line_reporter/formatter/nested.rb,
lib/command_line_reporter/formatter/progress.rb

Overview

rubocop:disable Metrics/ModuleLength

Defined Under Namespace

Classes: Column, NestedFormatter, ProgressFormatter, Row, Table

Constant Summary collapse

DEFAULTS =
{
  width: 100,
  align: 'left',
  formatter: 'nested',
  encoding: :unicode
}.freeze
VERSION =
'4.0.2'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OptionsValidator

#validate_options

Instance Attribute Details

#formatterObject

Returns the value of attribute formatter.



15
16
17
# File 'lib/command_line_reporter.rb', line 15

def formatter
  @formatter
end

Instance Method Details

#aligned(text, options = {}) ⇒ Object

Raises:

  • (Exception)


99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/command_line_reporter.rb', line 99

def aligned(text, options = {})
  validate_options(options, :align, :width, :format, :color, :bold)

  options[:align] = default_options_align(options[:align])
  options[:width] = default_options_width(options[:width])
  options[:bold] = default_options_bold(options[:bold])

  raise Exception if text.size > options[:width]

  line = align_line(text, options)

  puts apply_color(line, options)
end

#capture_outputObject



24
25
26
27
28
29
# File 'lib/command_line_reporter.rb', line 24

def capture_output
  $stdout.rewind
  $stdout.read
ensure
  $stdout = STDOUT
end

#column(text, options = {}) ⇒ Object



126
127
128
129
# File 'lib/command_line_reporter.rb', line 126

def column(text, options = {})
  col = CommandLineReporter::Column.new(text, options)
  @row.add(col)
end

#datetime(options = {}) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/command_line_reporter.rb', line 90

def datetime(options = {})
  validate_options(options, :align, :width, :format, :color, :bold)

  format = default_options_date_format(options[:format])
  text = Time.now.strftime(format)

  aligned(text, options)
end


63
64
65
# File 'lib/command_line_reporter.rb', line 63

def footer(options = {})
  section(:footer, options)
end

#header(options = {}) ⇒ Object



50
51
52
# File 'lib/command_line_reporter.rb', line 50

def header(options = {})
  section(:header, options)
end

#horizontal_rule(options = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/command_line_reporter.rb', line 67

def horizontal_rule(options = {})
  validate_options(options, :char, :width, :color, :bold)

  # Got unicode?
  use_char = "\u2501" == 'u2501' ? '-' : "\u2501"

  char = options[:char].is_a?(String) ? options[:char] : use_char
  width = options[:width] || DEFAULTS[:width]

  aligned(char * width, width: width, color: options[:color], bold: options[:bold])
end

#progress(override = nil) ⇒ Object



59
60
61
# File 'lib/command_line_reporter.rb', line 59

def progress(override = nil)
  self.formatter.progress(override)
end

#report(options = {}, &block) ⇒ Object



54
55
56
57
# File 'lib/command_line_reporter.rb', line 54

def report(options = {}, &block)
  self.formatter ||= DEFAULTS[:formatter]
  self.formatter.format(options, block)
end

#restore_outputObject



35
36
37
# File 'lib/command_line_reporter.rb', line 35

def restore_output
  $stdout = STDOUT
end

#row(options = {}) ⇒ Object



119
120
121
122
123
124
# File 'lib/command_line_reporter.rb', line 119

def row(options = {})
  options[:encoding] ||= @table.encoding
  @row = CommandLineReporter::Row.new(options)
  yield
  @table.add(@row)
end

#suppress_outputObject



31
32
33
# File 'lib/command_line_reporter.rb', line 31

def suppress_output
  $stdout = StringIO.new
end

#table(options = {}) ⇒ Object



113
114
115
116
117
# File 'lib/command_line_reporter.rb', line 113

def table(options = {})
  @table = CommandLineReporter::Table.new(options)
  yield
  @table.output
end

#vertical_spacing(lines = 1) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/command_line_reporter.rb', line 79

def vertical_spacing(lines = 1)
  lines = Integer(lines)

  # because puts "\n" * 0 produces an unwanted newline
  if lines.zero?
    print "\0"
  else
    puts "\n" * lines
  end
end