command_line_reporter 3.3.6 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/command_line_reporter.rb +101 -54
- data/lib/command_line_reporter/column.rb +37 -26
- data/lib/command_line_reporter/formatter/nested.rb +7 -11
- data/lib/command_line_reporter/formatter/progress.rb +5 -5
- data/lib/command_line_reporter/row.rb +15 -21
- data/lib/command_line_reporter/table.rb +37 -25
- data/lib/command_line_reporter/version.rb +1 -1
- data/spec/column_spec.rb +233 -138
- data/spec/command_line_reporter_spec.rb +172 -174
- data/spec/nested_formatter_spec.rb +66 -66
- data/spec/options_validator_spec.rb +10 -10
- data/spec/progress_formatter_spec.rb +10 -25
- data/spec/row_spec.rb +24 -22
- data/spec/spec_helper.rb +1 -1
- data/spec/support/matchers/argument.rb +0 -12
- data/spec/table_spec.rb +74 -48
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1024c56ad76f095c3f3778ec780c4427f184ab34
|
4
|
+
data.tar.gz: 60ddfd0b420dc8ce7692f7d1cd226ba437c4349f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 040a9a4fad99322164d8b42225f7c06335f1747d4e387ceb6ddc7a483dc7375717f3f442123e85a9c7bd75a3e4937da125a4651fc4148afdf9c20f9a0b11ad04
|
7
|
+
data.tar.gz: 9c0ba99f7fc2ba37cadcc3d8cf15eb10b94f8da4f5de036952344ebeea80917a3e9c389d5810a2f7019ba98ae608833a1dac8bf8f3058e106bfe230ff58886e3
|
data/README.md
CHANGED
@@ -134,7 +134,7 @@ There are several methods the mixin provides that do not depend on the formatter
|
|
134
134
|
|
135
135
|
### License
|
136
136
|
|
137
|
-
Copyright (c) 2011-
|
137
|
+
Copyright (c) 2011-2017 Wes Bailey
|
138
138
|
|
139
139
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
140
140
|
associated documentation files (the "Software"), to deal in the Software without restriction,
|
@@ -8,17 +8,18 @@ require 'command_line_reporter/column'
|
|
8
8
|
require 'command_line_reporter/table'
|
9
9
|
require 'command_line_reporter/version'
|
10
10
|
|
11
|
+
# rubocop:disable Metrics/ModuleLength
|
11
12
|
module CommandLineReporter
|
12
13
|
include OptionsValidator
|
13
14
|
|
14
15
|
attr_reader :formatter
|
15
16
|
|
16
17
|
DEFAULTS = {
|
17
|
-
:
|
18
|
-
:
|
19
|
-
:
|
20
|
-
:
|
21
|
-
}
|
18
|
+
width: 100,
|
19
|
+
align: 'left',
|
20
|
+
formatter: 'nested',
|
21
|
+
encoding: :unicode
|
22
|
+
}.freeze
|
22
23
|
|
23
24
|
def capture_output
|
24
25
|
$stdout.rewind
|
@@ -38,7 +39,7 @@ module CommandLineReporter
|
|
38
39
|
def formatter=(type = 'nested')
|
39
40
|
return type if type.class != String
|
40
41
|
name = type.capitalize + 'Formatter'
|
41
|
-
klass = %W
|
42
|
+
klass = %W[CommandLineReporter #{name}].inject(Kernel) { |a, e| a.const_get(e) }
|
42
43
|
|
43
44
|
# Each formatter is a singleton that responds to #instance
|
44
45
|
@formatter = klass.instance
|
@@ -72,14 +73,14 @@ module CommandLineReporter
|
|
72
73
|
char = options[:char].is_a?(String) ? options[:char] : use_char
|
73
74
|
width = options[:width] || DEFAULTS[:width]
|
74
75
|
|
75
|
-
aligned(char * width, :
|
76
|
+
aligned(char * width, width: width, color: options[:color], bold: options[:bold])
|
76
77
|
end
|
77
78
|
|
78
79
|
def vertical_spacing(lines = 1)
|
79
80
|
lines = Integer(lines)
|
80
81
|
|
81
82
|
# because puts "\n" * 0 produces an unwanted newline
|
82
|
-
if lines
|
83
|
+
if lines.zero?
|
83
84
|
print "\0"
|
84
85
|
else
|
85
86
|
puts "\n" * lines
|
@@ -89,40 +90,24 @@ module CommandLineReporter
|
|
89
90
|
def datetime(options = {})
|
90
91
|
validate_options(options, :align, :width, :format, :color, :bold)
|
91
92
|
|
92
|
-
format = options[:format]
|
93
|
-
align = options[:align] || DEFAULTS[:align]
|
94
|
-
width = options[:width] || DEFAULTS[:width]
|
95
|
-
|
93
|
+
format = default_options_date_format(options[:format])
|
96
94
|
text = Time.now.strftime(format)
|
97
95
|
|
98
|
-
|
99
|
-
|
100
|
-
aligned(text, :align => align, :width => width, :color => options[:color], :bold => options[:bold])
|
96
|
+
aligned(text, options)
|
101
97
|
end
|
102
98
|
|
103
99
|
def aligned(text, options = {})
|
104
|
-
validate_options(options, :align, :width, :color, :bold)
|
100
|
+
validate_options(options, :align, :width, :format, :color, :bold)
|
105
101
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
bold = options[:bold] || false
|
102
|
+
options[:align] = default_options_align(options[:align])
|
103
|
+
options[:width] = default_options_width(options[:width])
|
104
|
+
options[:bold] = default_options_bold(options[:bold])
|
110
105
|
|
111
|
-
|
112
|
-
when 'left'
|
113
|
-
text
|
114
|
-
when 'right'
|
115
|
-
text.rjust(width)
|
116
|
-
when 'center'
|
117
|
-
text.rjust((width - text.size)/2 + text.size)
|
118
|
-
else
|
119
|
-
raise ArgumentError
|
120
|
-
end
|
106
|
+
raise Exception if text.size > options[:width]
|
121
107
|
|
122
|
-
line =
|
123
|
-
line = line.send('bold') if bold
|
108
|
+
line = align_line(text, options)
|
124
109
|
|
125
|
-
puts line
|
110
|
+
puts apply_color(line, options)
|
126
111
|
end
|
127
112
|
|
128
113
|
def table(options = {})
|
@@ -145,36 +130,98 @@ module CommandLineReporter
|
|
145
130
|
|
146
131
|
private
|
147
132
|
|
133
|
+
def align_line(text, options)
|
134
|
+
case options[:align]
|
135
|
+
when 'left'
|
136
|
+
text
|
137
|
+
when 'right'
|
138
|
+
text.rjust(options[:width])
|
139
|
+
when 'center'
|
140
|
+
text.rjust((options[:width] - text.size) / 2 + text.size)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
def apply_color(line, options)
|
145
|
+
line = line.send(options[:color]) if options[:color]
|
146
|
+
line = line.send('bold') if options[:bold]
|
147
|
+
line
|
148
|
+
end
|
149
|
+
|
150
|
+
# def default_datetime_options(options)
|
151
|
+
# format = define_format(options)
|
152
|
+
# align = default_options_align(options[:align])
|
153
|
+
# width = default_options_width(options[:width])
|
154
|
+
# [format, align, width]
|
155
|
+
# end
|
156
|
+
|
157
|
+
def default_options_date_format(format)
|
158
|
+
format || '%Y-%m-%d - %l:%M:%S%p'
|
159
|
+
end
|
160
|
+
|
148
161
|
def section(type, options)
|
149
|
-
|
162
|
+
options = define_section_values(options)
|
150
163
|
|
151
|
-
|
152
|
-
raise ArgumentError if title.size > width
|
164
|
+
raise ArgumentError if options[:title].size > options[:width]
|
153
165
|
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
166
|
+
print_header(type, options)
|
167
|
+
print_body(options)
|
168
|
+
print_footer(type, options)
|
169
|
+
end
|
170
|
+
|
171
|
+
def print_header(type, options)
|
172
|
+
return unless type == :header
|
173
|
+
vertical_spacing(options[:lines])
|
174
|
+
horizontal_rule(char: options[:rule], width: options[:width], color: options[:color], bold: options[:bold]) if options[:rule]
|
175
|
+
end
|
158
176
|
|
159
|
-
|
160
|
-
|
177
|
+
def print_body(options)
|
178
|
+
aligned(options[:title], align: options[:align], width: options[:width], color: options[:color], bold: options[:bold])
|
179
|
+
datetime(align: options[:align], width: options[:width], color: options[:color], bold: options[:bold]) if options[:timestamp]
|
180
|
+
end
|
161
181
|
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
182
|
+
def print_footer(type, options)
|
183
|
+
return unless type == :footer
|
184
|
+
horizontal_rule(char: options[:rule], width: options[:width], color: options[:color], bold: options[:bold]) if options[:rule]
|
185
|
+
vertical_spacing(options[:lines])
|
166
186
|
end
|
167
187
|
|
168
|
-
def
|
188
|
+
def define_section_values(options)
|
169
189
|
validate_options(options, :title, :width, :align, :spacing, :timestamp, :rule, :color, :bold)
|
170
190
|
|
171
|
-
title = options[:title]
|
172
|
-
|
173
|
-
|
174
|
-
lines = options[:spacing]
|
175
|
-
|
176
|
-
|
191
|
+
options[:title] = default_options_title(options[:title])
|
192
|
+
options[:width] = default_options_width(options[:width])
|
193
|
+
options[:align] = default_options_align(options[:align])
|
194
|
+
options[:lines] = default_options_lines(options[:spacing])
|
195
|
+
options[:bold] = default_options_bold(options[:bold])
|
196
|
+
|
197
|
+
options
|
198
|
+
end
|
199
|
+
|
200
|
+
def default_options_title(title)
|
201
|
+
title || 'Report'
|
202
|
+
end
|
203
|
+
|
204
|
+
def default_options_width(width)
|
205
|
+
width ||= DEFAULTS[:width]
|
206
|
+
Integer(width)
|
207
|
+
width
|
208
|
+
end
|
209
|
+
|
210
|
+
def default_options_align(align)
|
211
|
+
align ||= DEFAULTS[:align]
|
212
|
+
validate_align(align)
|
213
|
+
align
|
214
|
+
end
|
215
|
+
|
216
|
+
def default_options_lines(lines)
|
217
|
+
lines || 1
|
218
|
+
end
|
219
|
+
|
220
|
+
def default_options_bold(bold)
|
221
|
+
bold || false
|
222
|
+
end
|
177
223
|
|
178
|
-
|
224
|
+
def validate_align(align)
|
225
|
+
raise ArgumentError unless %i[left center right].include?(align.to_sym)
|
179
226
|
end
|
180
227
|
end
|
@@ -4,71 +4,82 @@ module CommandLineReporter
|
|
4
4
|
class Column
|
5
5
|
include OptionsValidator
|
6
6
|
|
7
|
-
VALID_OPTIONS = [
|
7
|
+
VALID_OPTIONS = %i[width padding align color bold underline reversed span].freeze
|
8
8
|
attr_accessor :text, :size, *VALID_OPTIONS
|
9
9
|
|
10
10
|
def initialize(text = nil, options = {})
|
11
|
-
|
11
|
+
validate_options(options, *VALID_OPTIONS)
|
12
|
+
assign_alignment_defaults(options)
|
13
|
+
assign_color_defaults(options)
|
12
14
|
|
13
15
|
self.text = text.to_s
|
14
|
-
|
15
|
-
self.width = options[:width] || 10
|
16
|
-
self.align = options[:align] || 'left'
|
17
|
-
self.padding = options[:padding] || 0
|
18
|
-
self.color = options[:color] || nil
|
19
|
-
self.bold = options[:bold] || false
|
20
|
-
self.underline = options[:underline] || false
|
21
|
-
self.reversed = options[:reversed] || false
|
22
|
-
|
23
|
-
raise ArgumentError unless self.width > 0
|
24
|
-
raise ArgumentError unless self.padding.to_s.match(/^\d+$/)
|
25
16
|
end
|
26
17
|
|
27
18
|
def size
|
28
|
-
|
19
|
+
width - (2 * padding) + (3 * (span - 1))
|
29
20
|
end
|
30
21
|
|
31
22
|
def required_width
|
32
|
-
|
23
|
+
text.to_s.size + 2 * padding
|
33
24
|
end
|
34
25
|
|
35
26
|
def screen_rows
|
36
|
-
if
|
37
|
-
[' ' *
|
27
|
+
if text.nil? || text.empty?
|
28
|
+
[' ' * width]
|
38
29
|
else
|
39
|
-
|
30
|
+
text.scan(/.{1,#{size}}/m).map { |s| to_cell(s) }
|
40
31
|
end
|
41
32
|
end
|
42
33
|
|
43
34
|
private
|
44
35
|
|
36
|
+
def assign_alignment_defaults(options)
|
37
|
+
self.span = options[:span] || 1
|
38
|
+
|
39
|
+
self.width = options[:width] || 10
|
40
|
+
self.width = Integer(width)
|
41
|
+
self.width *= span
|
42
|
+
|
43
|
+
self.align = options[:align] || 'left'
|
44
|
+
|
45
|
+
self.padding = options[:padding] || 0
|
46
|
+
self.padding = Integer(padding)
|
47
|
+
end
|
48
|
+
|
49
|
+
def assign_color_defaults(options)
|
50
|
+
self.color = options[:color] || nil
|
51
|
+
self.bold = options[:bold] || false
|
52
|
+
self.underline = options[:underline] || false
|
53
|
+
self.reversed = options[:reversed] || false
|
54
|
+
end
|
55
|
+
|
45
56
|
def to_cell(str)
|
46
57
|
# NOTE: For making underline and reversed work Change so that based on the
|
47
58
|
# unformatted text it determines how much spacing to add left and right
|
48
59
|
# then colorize the cell text
|
49
60
|
cell = str.empty? ? blank_cell : aligned_cell(str)
|
50
|
-
padding_str = ' ' *
|
61
|
+
padding_str = ' ' * padding
|
51
62
|
padding_str + colorize(cell) + padding_str
|
52
63
|
end
|
53
64
|
|
54
65
|
def blank_cell
|
55
|
-
' ' *
|
66
|
+
' ' * size
|
56
67
|
end
|
57
68
|
|
58
69
|
def aligned_cell(str)
|
59
|
-
case
|
70
|
+
case align
|
60
71
|
when 'left'
|
61
|
-
str.ljust(
|
72
|
+
str.ljust(size)
|
62
73
|
when 'right'
|
63
|
-
str.rjust(
|
74
|
+
str.rjust(size)
|
64
75
|
when 'center'
|
65
|
-
str.ljust((
|
76
|
+
str.ljust((size - str.size) / 2.0 + str.size).rjust(size)
|
66
77
|
end
|
67
78
|
end
|
68
79
|
|
69
80
|
def colorize(str)
|
70
|
-
str = str.send(color) if
|
71
|
-
str = str.send('bold') if
|
81
|
+
str = str.send(color) if color
|
82
|
+
str = str.send('bold') if bold
|
72
83
|
str
|
73
84
|
end
|
74
85
|
end
|
@@ -6,18 +6,18 @@ module CommandLineReporter
|
|
6
6
|
include Singleton
|
7
7
|
include OptionsValidator
|
8
8
|
|
9
|
-
VALID_OPTIONS = [
|
9
|
+
VALID_OPTIONS = %i[message type complete indent_size color bold].freeze
|
10
10
|
attr_accessor :indent_size, :complete_string, :message_string, :color, :bold
|
11
11
|
|
12
12
|
def format(options, block)
|
13
|
-
|
13
|
+
validate_options(options, *VALID_OPTIONS)
|
14
14
|
|
15
15
|
indent_level :incr
|
16
16
|
|
17
|
-
padding = ' ' * @indent_level * (options[:indent_size] ||
|
17
|
+
padding = ' ' * @indent_level * (options[:indent_size] || indent_size)
|
18
18
|
|
19
|
-
message_str = padding + (options[:message] ||
|
20
|
-
complete_str = options[:complete] ||
|
19
|
+
message_str = padding + (options[:message] || message_string)
|
20
|
+
complete_str = options[:complete] || complete_string
|
21
21
|
|
22
22
|
if options[:type] == 'inline'
|
23
23
|
colorize("#{message_str}...", true, options)
|
@@ -51,17 +51,13 @@ module CommandLineReporter
|
|
51
51
|
str = str.send(options[:color]) if options[:color]
|
52
52
|
str = str.bold if options[:bold]
|
53
53
|
|
54
|
-
|
55
|
-
print str
|
56
|
-
else
|
57
|
-
puts str
|
58
|
-
end
|
54
|
+
inline ? print(str) : puts(str)
|
59
55
|
end
|
60
56
|
|
61
57
|
def indent_level(value)
|
62
58
|
case value
|
63
59
|
when :incr
|
64
|
-
@indent_level =
|
60
|
+
@indent_level = @indent_level ? @indent_level + 1 : 0
|
65
61
|
when :decr
|
66
62
|
@indent_level -= 1
|
67
63
|
end
|
@@ -6,11 +6,11 @@ module CommandLineReporter
|
|
6
6
|
include Singleton
|
7
7
|
include OptionsValidator
|
8
8
|
|
9
|
-
VALID_OPTIONS = [
|
9
|
+
VALID_OPTIONS = %i[indicator color bold].freeze
|
10
10
|
attr_accessor *VALID_OPTIONS
|
11
11
|
|
12
12
|
def format(options, block)
|
13
|
-
|
13
|
+
validate_options(options, *VALID_OPTIONS)
|
14
14
|
|
15
15
|
self.indicator = options[:indicator] if options[:indicator]
|
16
16
|
self.color = options[:color]
|
@@ -22,10 +22,10 @@ module CommandLineReporter
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def progress(override = nil)
|
25
|
-
str = override ||
|
25
|
+
str = override || indicator
|
26
26
|
|
27
|
-
str = str.send(
|
28
|
-
str = str.send('bold') if
|
27
|
+
str = str.send(color) if color
|
28
|
+
str = str.send('bold') if bold
|
29
29
|
|
30
30
|
print str
|
31
31
|
end
|
@@ -2,11 +2,11 @@ module CommandLineReporter
|
|
2
2
|
class Row
|
3
3
|
include OptionsValidator
|
4
4
|
|
5
|
-
VALID_OPTIONS = [
|
5
|
+
VALID_OPTIONS = %i[header color bold encoding].freeze
|
6
6
|
attr_accessor :columns, :border, *VALID_OPTIONS
|
7
7
|
|
8
8
|
def initialize(options = {})
|
9
|
-
|
9
|
+
validate_options(options, *VALID_OPTIONS)
|
10
10
|
|
11
11
|
self.columns = []
|
12
12
|
self.border = false
|
@@ -17,29 +17,23 @@ module CommandLineReporter
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def add(column)
|
20
|
-
if column.color.nil? &&
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
if self.bold || self.header
|
25
|
-
column.bold = true
|
26
|
-
end
|
27
|
-
|
28
|
-
self.columns << column
|
20
|
+
column.color = color if column.color.nil? && color
|
21
|
+
column.bold = true if bold || header
|
22
|
+
columns << column
|
29
23
|
end
|
30
24
|
|
31
25
|
def output
|
32
26
|
screen_count.times do |sr|
|
33
27
|
border_char = use_utf8? ? "\u2503" : '|'
|
34
28
|
|
35
|
-
line =
|
29
|
+
line = border ? "#{border_char} " : ''
|
36
30
|
|
37
|
-
|
38
|
-
col =
|
31
|
+
columns.size.times do |mc|
|
32
|
+
col = columns[mc]
|
39
33
|
# Account for the fact that some columns will have more screen rows than their
|
40
34
|
# counterparts in the row. An example being:
|
41
|
-
# c1 = Column.new('x' * 50, :
|
42
|
-
# c2 = Column.new('x' * 20, :
|
35
|
+
# c1 = Column.new('x' * 50, width: 10)
|
36
|
+
# c2 = Column.new('x' * 20, width: 10)
|
43
37
|
#
|
44
38
|
# c1.screen_rows.size == 5
|
45
39
|
# c2.screen_rows.size == 2
|
@@ -58,12 +52,12 @@ module CommandLineReporter
|
|
58
52
|
if col.screen_rows[sr].nil?
|
59
53
|
line << ' ' * col.width << ' '
|
60
54
|
else
|
61
|
-
line <<
|
55
|
+
line << columns[mc].screen_rows[sr] << ' '
|
62
56
|
end
|
63
57
|
|
64
|
-
if
|
58
|
+
if border
|
65
59
|
line << border_char
|
66
|
-
line << ' ' if mc <
|
60
|
+
line << ' ' if mc < columns.size - 1
|
67
61
|
end
|
68
62
|
end
|
69
63
|
|
@@ -74,11 +68,11 @@ module CommandLineReporter
|
|
74
68
|
private
|
75
69
|
|
76
70
|
def screen_count
|
77
|
-
@sc ||=
|
71
|
+
@sc ||= columns.inject(0) { |a, e| e.screen_rows.size > a ? e.screen_rows.size : a }
|
78
72
|
end
|
79
73
|
|
80
74
|
def use_utf8?
|
81
|
-
|
75
|
+
encoding == :unicode && "\u2501" != 'u2501'
|
82
76
|
end
|
83
77
|
end
|
84
78
|
end
|