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

Defined Under Namespace

Classes: Column, NestedFormatter, ProgressFormatter, Row, Table

Constant Summary collapse

DEFAULTS =
{
  :width => 100,
  :align => 'left',
  :formatter => 'nested',
  :encoding => :unicode,
}
VERSION =
'3.3.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OptionsValidator

#validate_options

Instance Attribute Details

#formatterObject

Returns the value of attribute formatter.



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

def formatter
  @formatter
end

Instance Method Details

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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/command_line_reporter.rb', line 97

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

  align = options[:align] || DEFAULTS[:align]
  width = options[:width] || DEFAULTS[:width]
  color = options[:color]
  bold = options[:bold] || false

  line = case align
         when 'left'
           text
         when 'right'
           text.rjust(width)
         when 'center'
           text.rjust((width - text.size)/2 + text.size)
         else
           raise ArgumentError
         end

  line = line.send(color) if color
  line = line.send('bold') if bold

  puts line
end

#capture_outputObject



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

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

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



135
136
137
138
# File 'lib/command_line_reporter.rb', line 135

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

#datetime(options = {}) ⇒ Object

Raises:

  • (Exception)


83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/command_line_reporter.rb', line 83

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

  format = options[:format] || '%Y-%m-%d - %l:%M:%S%p'
  align = options[:align] || DEFAULTS[:align]
  width = options[:width] || DEFAULTS[:width]

  text = Time.now.strftime(format)

  raise Exception if text.size > width

  aligned(text, :align => align, :width => width, :color => options[:color], :bold => options[:bold])
end


61
62
63
# File 'lib/command_line_reporter.rb', line 61

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

#header(options = {}) ⇒ Object



48
49
50
# File 'lib/command_line_reporter.rb', line 48

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

#horizontal_rule(options = {}) ⇒ Object



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

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



57
58
59
# File 'lib/command_line_reporter.rb', line 57

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

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



52
53
54
55
# File 'lib/command_line_reporter.rb', line 52

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

#restore_outputObject



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

def restore_output
  $stdout = STDOUT
end

#row(options = {}) ⇒ Object



128
129
130
131
132
133
# File 'lib/command_line_reporter.rb', line 128

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

#suppress_outputObject



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

def suppress_output
  $stdout = StringIO.new
end

#table(options = {}) ⇒ Object



122
123
124
125
126
# File 'lib/command_line_reporter.rb', line 122

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

#vertical_spacing(lines = 1) ⇒ Object



77
78
79
80
81
# File 'lib/command_line_reporter.rb', line 77

def vertical_spacing(lines = 1)
  puts "\n" * lines
rescue
  raise ArgumentError
end