hammer_cli 0.19.0 → 0.19.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/doc/release_notes.md +4 -0
- data/lib/hammer_cli/output/adapter/table.rb +6 -75
- data/lib/hammer_cli/output/definition.rb +11 -56
- data/lib/hammer_cli/output/generators/table.rb +121 -0
- data/lib/hammer_cli/output/generators.rb +1 -0
- data/lib/hammer_cli/output.rb +1 -1
- data/lib/hammer_cli/version.rb +1 -1
- data/man/hammer.1.gz +0 -0
- data/test/unit/output/definition_test.rb +7 -7
- metadata +67 -340
- data/test/reports/TEST-Fields-ContainerField-display-.xml +0 -7
- data/test/reports/TEST-Fields-ContainerField-display-blank-is-allowed.xml +0 -15
- data/test/reports/TEST-Fields-ContainerField-display-blank-is-not-allowed.xml +0 -15
- data/test/reports/TEST-Fields-ContainerField.xml +0 -7
- data/test/reports/TEST-Fields-Field-display-.xml +0 -7
- data/test/reports/TEST-Fields-Field-display-blank-is-allowed.xml +0 -11
- data/test/reports/TEST-Fields-Field-display-blank-is-not-allowed.xml +0 -11
- data/test/reports/TEST-Fields-Field-hide-blank-.xml +0 -11
- data/test/reports/TEST-Fields-Field-parameters.xml +0 -9
- data/test/reports/TEST-Fields-Field.xml +0 -13
- data/test/reports/TEST-HammerCLI-AbstractCommand-build-options.xml +0 -15
- data/test/reports/TEST-HammerCLI-AbstractCommand-exception-handler.xml +0 -13
- data/test/reports/TEST-HammerCLI-AbstractCommand-logging.xml +0 -21
- data/test/reports/TEST-HammerCLI-AbstractCommand-option-builder.xml +0 -11
- data/test/reports/TEST-HammerCLI-AbstractCommand-options.xml +0 -11
- data/test/reports/TEST-HammerCLI-AbstractCommand-output.xml +0 -19
- data/test/reports/TEST-HammerCLI-AbstractCommand-subcommand-behavior-remove-subcommand.xml +0 -11
- data/test/reports/TEST-HammerCLI-AbstractCommand-subcommand-behavior-subcommand-.xml +0 -13
- data/test/reports/TEST-HammerCLI-AbstractCommand-subcommand-behavior-subcommand.xml +0 -11
- data/test/reports/TEST-HammerCLI-AbstractCommand-subcommand-behavior.xml +0 -7
- data/test/reports/TEST-HammerCLI-AbstractCommand.xml +0 -11
- data/test/reports/TEST-HammerCLI-Apipie-Command-options.xml +0 -11
- data/test/reports/TEST-HammerCLI-Apipie-Command-resource-defined.xml +0 -9
- data/test/reports/TEST-HammerCLI-Apipie-Command-setting-resources.xml +0 -19
- data/test/reports/TEST-HammerCLI-Apipie-Command.xml +0 -9
- data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-aliasing-resources.xml +0 -13
- data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-filtering-options.xml +0 -15
- data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-required-options.xml +0 -11
- data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-setting-correct-normalizers.xml +0 -9
- data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-with-hash-params.xml +0 -11
- data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-with-one-simple-param.xml +0 -15
- data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder.xml +0 -7
- data/test/reports/TEST-HammerCLI-Completer-command-completion.xml +0 -29
- data/test/reports/TEST-HammerCLI-Completer-option-value-completion.xml +0 -17
- data/test/reports/TEST-HammerCLI-Completer-subcommand-completion.xml +0 -19
- data/test/reports/TEST-HammerCLI-Completer.xml +0 -7
- data/test/reports/TEST-HammerCLI-CompleterLine-line-complete.xml +0 -25
- data/test/reports/TEST-HammerCLI-CompleterLine-splitting-words.xml +0 -29
- data/test/reports/TEST-HammerCLI-CompleterLine.xml +0 -7
- data/test/reports/TEST-HammerCLI-CompleterWord-complete-.xml +0 -23
- data/test/reports/TEST-HammerCLI-CompleterWord-quote.xml +0 -15
- data/test/reports/TEST-HammerCLI-CompleterWord-quoted-.xml +0 -13
- data/test/reports/TEST-HammerCLI-CompleterWord.xml +0 -7
- data/test/reports/TEST-HammerCLI-Connection.xml +0 -21
- data/test/reports/TEST-HammerCLI-ExceptionHandler.xml +0 -21
- data/test/reports/TEST-HammerCLI-I18n.xml +0 -11
- data/test/reports/TEST-HammerCLI-MainCommand-loading-context-password.xml +0 -11
- data/test/reports/TEST-HammerCLI-MainCommand-loading-context-username.xml +0 -11
- data/test/reports/TEST-HammerCLI-MainCommand-loading-context-verbose.xml +0 -9
- data/test/reports/TEST-HammerCLI-MainCommand-loading-context.xml +0 -7
- data/test/reports/TEST-HammerCLI-MainCommand.xml +0 -7
- data/test/reports/TEST-HammerCLI-Modules-find-by-name.xml +0 -13
- data/test/reports/TEST-HammerCLI-Modules-load-a-module-module-not-found.xml +0 -13
- data/test/reports/TEST-HammerCLI-Modules-load-a-module-module-runtime-exception.xml +0 -13
- data/test/reports/TEST-HammerCLI-Modules-load-a-module-success.xml +0 -15
- data/test/reports/TEST-HammerCLI-Modules-load-a-module.xml +0 -7
- data/test/reports/TEST-HammerCLI-Modules-load-all-modules.xml +0 -9
- data/test/reports/TEST-HammerCLI-Modules-names.xml +0 -13
- data/test/reports/TEST-HammerCLI-Modules.xml +0 -7
- data/test/reports/TEST-HammerCLI-OptionBuilderContainer.0.xml +0 -7
- data/test/reports/TEST-HammerCLI-OptionBuilderContainer.xml +0 -11
- data/test/reports/TEST-HammerCLI-Options-Normalizers-abstract.xml +0 -9
- data/test/reports/TEST-HammerCLI-Options-Normalizers-bool.xml +0 -31
- data/test/reports/TEST-HammerCLI-Options-Normalizers-datetime.xml +0 -17
- data/test/reports/TEST-HammerCLI-Options-Normalizers-enum.xml +0 -15
- data/test/reports/TEST-HammerCLI-Options-Normalizers-enumlist.xml +0 -21
- data/test/reports/TEST-HammerCLI-Options-Normalizers-json-input.xml +0 -15
- data/test/reports/TEST-HammerCLI-Options-Normalizers-key-value-list.xml +0 -17
- data/test/reports/TEST-HammerCLI-Options-Normalizers-list.xml +0 -15
- data/test/reports/TEST-HammerCLI-Options-Normalizers.xml +0 -7
- data/test/reports/TEST-HammerCLI-Options-OptionDefinition-context.xml +0 -9
- data/test/reports/TEST-HammerCLI-Options-OptionDefinition-formatters.xml +0 -11
- data/test/reports/TEST-HammerCLI-Options-OptionDefinition.xml +0 -7
- data/test/reports/TEST-HammerCLI-Output-Adapter-Abstract-error-messages.xml +0 -15
- data/test/reports/TEST-HammerCLI-Output-Adapter-Abstract-messages.xml +0 -11
- data/test/reports/TEST-HammerCLI-Output-Adapter-Abstract-test-data-for-field.xml +0 -15
- data/test/reports/TEST-HammerCLI-Output-Adapter-Abstract.xml +0 -17
- data/test/reports/TEST-HammerCLI-Output-Adapter-Base-print-collection-show-ids.xml +0 -9
- data/test/reports/TEST-HammerCLI-Output-Adapter-Base-print-collection.xml +0 -27
- data/test/reports/TEST-HammerCLI-Output-Adapter-Base.xml +0 -7
- data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection-formatters.xml +0 -11
- data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection-handle-fields-with-collections.xml +0 -13
- data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection-handle-fields-with-containers.xml +0 -11
- data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection-handle-ids.xml +0 -11
- data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection.xml +0 -11
- data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-message.xml +0 -11
- data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues.xml +0 -7
- data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection-column-width.xml +0 -15
- data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection-formatters.xml +0 -11
- data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection-handle-ids.xml +0 -11
- data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection-sort-columns.xml +0 -9
- data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection.xml +0 -11
- data/test/reports/TEST-HammerCLI-Output-Adapter-Table.xml +0 -7
- data/test/reports/TEST-HammerCLI-Output-Definition-empty-.xml +0 -11
- data/test/reports/TEST-HammerCLI-Output-Definition.xml +0 -11
- data/test/reports/TEST-HammerCLI-Output-Dsl-collection.xml +0 -13
- data/test/reports/TEST-HammerCLI-Output-Dsl-custom-fields.xml +0 -11
- data/test/reports/TEST-HammerCLI-Output-Dsl-fields.xml +0 -15
- data/test/reports/TEST-HammerCLI-Output-Dsl-label.xml +0 -13
- data/test/reports/TEST-HammerCLI-Output-Dsl-path-definition.xml +0 -13
- data/test/reports/TEST-HammerCLI-Output-Dsl.xml +0 -9
- data/test/reports/TEST-HammerCLI-Output-FieldFilter.xml +0 -13
- data/test/reports/TEST-HammerCLI-Output-Formatters-BooleanFormatter.xml +0 -11
- data/test/reports/TEST-HammerCLI-Output-Formatters-ColorFormatter.xml +0 -9
- data/test/reports/TEST-HammerCLI-Output-Formatters-DateFormatter.xml +0 -11
- data/test/reports/TEST-HammerCLI-Output-Formatters-FieldFormatter.xml +0 -13
- data/test/reports/TEST-HammerCLI-Output-Formatters-FormatterContainer.xml +0 -13
- data/test/reports/TEST-HammerCLI-Output-Formatters-FormatterLibrary.xml +0 -11
- data/test/reports/TEST-HammerCLI-Output-Formatters-KeyValueFormatter.xml +0 -13
- data/test/reports/TEST-HammerCLI-Output-Formatters-ListFormatter.xml +0 -13
- data/test/reports/TEST-HammerCLI-Output-Formatters-LongTextFormatter.xml +0 -13
- data/test/reports/TEST-HammerCLI-Output-Output-adapters.xml +0 -17
- data/test/reports/TEST-HammerCLI-Output-Output-data.xml +0 -15
- data/test/reports/TEST-HammerCLI-Output-Output-formatters.xml +0 -9
- data/test/reports/TEST-HammerCLI-Output-Output-messages.xml +0 -19
- data/test/reports/TEST-HammerCLI-Output-Output.xml +0 -7
- data/test/reports/TEST-HammerCLI-Output-RecordCollection.xml +0 -13
- data/test/reports/TEST-HammerCLI-Settings-load-from-paths.xml +0 -15
- data/test/reports/TEST-HammerCLI-Settings.xml +0 -25
- data/test/reports/TEST-HammerCLI-ShellHistory-loading-old-history.xml +0 -11
- data/test/reports/TEST-HammerCLI-ShellHistory-saving-history.xml +0 -15
- data/test/reports/TEST-HammerCLI-ShellHistory.xml +0 -7
- data/test/reports/TEST-MiniTest-Spec.xml +0 -7
- data/test/reports/TEST-String-camelize.xml +0 -11
- data/test/reports/TEST-String-formatting.xml +0 -17
- data/test/reports/TEST-String-indent.xml +0 -11
- data/test/reports/TEST-String-interactive-.xml +0 -13
- data/test/reports/TEST-String.xml +0 -7
- data/test/reports/TEST-constraints-HammerCLI-Validator-AllConstraint-exist-.xml +0 -13
- data/test/reports/TEST-constraints-HammerCLI-Validator-AllConstraint.xml +0 -7
- data/test/reports/TEST-constraints-HammerCLI-Validator-AnyConstraint-exist-.xml +0 -13
- data/test/reports/TEST-constraints-HammerCLI-Validator-AnyConstraint.xml +0 -7
- data/test/reports/TEST-constraints-HammerCLI-Validator-BaseConstraint-exist-.xml +0 -9
- data/test/reports/TEST-constraints-HammerCLI-Validator-BaseConstraint-rejected.xml +0 -13
- data/test/reports/TEST-constraints-HammerCLI-Validator-BaseConstraint-required.xml +0 -13
- data/test/reports/TEST-constraints-HammerCLI-Validator-BaseConstraint.xml +0 -7
- data/test/reports/TEST-constraints.xml +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fba56b0d34d9914c40df8f424e68d9268d7e2a38e5f2dd421e10991f477fbd1d
|
4
|
+
data.tar.gz: 5b1e6d8ea29af809ab09884cc2579c3f328439a7d102335940820bd32ec3289d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7fdb4ed23030a8d87156504452c1b04959dd466015579ec2dec75b548bd5af2baea8a5538f0d4b866a2038addf2034c9df2bd89dc709378b275868e6c48c9e99
|
7
|
+
data.tar.gz: 45d29077823ab5997435b51f4cfab81a4c1373cd42793958811cf7ec48ab5837f48779b4ab237af9b980aca43dd43b685b2197e3f485dfc4894421098898c5f3
|
data/doc/release_notes.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
Release notes
|
2
2
|
=============
|
3
|
+
### 0.19.1 (2019-12-31)
|
4
|
+
* Allow column max width more than 80, [#28503](http://projects.theforeman.org/issues/28503)
|
5
|
+
* Extract table generator into reusable component ([PR #314](https://github.com/theforeman/hammer-cli/pull/314)), [#27318](http://projects.theforeman.org/issues/27318)
|
6
|
+
|
3
7
|
### 0.19.0 (2019-10-26)
|
4
8
|
* Allow schema building for custom options ([PR #316](https://github.com/Apipie/apipie-bindings/pull/316)), [#27899](http://projects.theforeman.org/issues/27899)
|
5
9
|
* New lines in text attr dont break output ([PR #300](https://github.com/Apipie/apipie-bindings/pull/300)), [#25878](http://projects.theforeman.org/issues/25878)
|
@@ -1,17 +1,7 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'wrapper_formatter')
|
2
|
-
require 'hammer_cli/output/utils'
|
3
2
|
|
4
3
|
module HammerCLI::Output::Adapter
|
5
|
-
|
6
4
|
class Table < Abstract
|
7
|
-
|
8
|
-
MAX_COLUMN_WIDTH = 80
|
9
|
-
MIN_COLUMN_WIDTH = 5
|
10
|
-
|
11
|
-
HLINE = '-'
|
12
|
-
LINE_SEPARATOR = '-|-'
|
13
|
-
COLUMN_SEPARATOR = ' | '
|
14
|
-
|
15
5
|
def features
|
16
6
|
return %i[rich_text serialized inline] if tags.empty?
|
17
7
|
|
@@ -29,33 +19,15 @@ module HammerCLI::Output::Adapter
|
|
29
19
|
compact_only: true)
|
30
20
|
.filtered_fields
|
31
21
|
formatted_collection = format_values(fields, collection)
|
32
|
-
# calculate hash of column widths (label -> width)
|
33
|
-
widths = calculate_widths(fields, formatted_collection)
|
34
|
-
|
35
|
-
header_bits = []
|
36
|
-
hline_bits = []
|
37
|
-
fields.map do |f|
|
38
|
-
header_bits << normalize_column(widths[f.label], f.label.upcase)
|
39
|
-
hline_bits << HLINE * widths[f.label]
|
40
|
-
end
|
41
22
|
|
42
|
-
|
43
|
-
|
44
|
-
output_stream.puts line
|
45
|
-
output_stream.puts header_bits.join(COLUMN_SEPARATOR)
|
46
|
-
output_stream.puts line
|
23
|
+
columns = fields.each_with_object({}) do |field, result|
|
24
|
+
result[field.label] = field.parameters
|
47
25
|
end
|
48
26
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
output_stream.puts row_bits.join(COLUMN_SEPARATOR)
|
54
|
-
end
|
55
|
-
|
56
|
-
# print closing line only when the table isn't empty
|
57
|
-
# and there is no --no-headers option
|
58
|
-
output_stream.puts line unless formatted_collection.empty? || @context[:no_headers]
|
27
|
+
table_gen = HammerCLI::Output::Generators::Table.new(
|
28
|
+
columns, formatted_collection, no_headers: @context[:no_headers]
|
29
|
+
)
|
30
|
+
output_stream.print(table_gen.result)
|
59
31
|
|
60
32
|
if collection.respond_to?(:meta) && collection.meta.pagination_set? &&
|
61
33
|
@context[:verbosity] >= collection.meta.pagination_verbosity &&
|
@@ -71,19 +43,6 @@ module HammerCLI::Output::Adapter
|
|
71
43
|
super << Fields::ContainerField
|
72
44
|
end
|
73
45
|
|
74
|
-
def normalize_column(width, value)
|
75
|
-
value = value.to_s
|
76
|
-
padding = width - HammerCLI::Output::Utils.real_length(value)
|
77
|
-
if padding >= 0
|
78
|
-
value += (" " * padding)
|
79
|
-
else
|
80
|
-
value, real_length = HammerCLI::Output::Utils.real_truncate(value, width-3)
|
81
|
-
value += '...'
|
82
|
-
value += ' ' if real_length < (width - 3)
|
83
|
-
end
|
84
|
-
value
|
85
|
-
end
|
86
|
-
|
87
46
|
def format_values(fields, collection)
|
88
47
|
collection.collect do |d|
|
89
48
|
fields.inject({}) do |row, f|
|
@@ -92,34 +51,6 @@ module HammerCLI::Output::Adapter
|
|
92
51
|
end
|
93
52
|
end
|
94
53
|
end
|
95
|
-
|
96
|
-
def calculate_widths(fields, collection)
|
97
|
-
Hash[fields.map { |f| [f.label, calculate_column_width(f, collection)] }]
|
98
|
-
end
|
99
|
-
|
100
|
-
def calculate_column_width(field, collection)
|
101
|
-
if field.parameters[:width]
|
102
|
-
return [field.parameters[:width], MIN_COLUMN_WIDTH].max
|
103
|
-
end
|
104
|
-
|
105
|
-
width = HammerCLI::Output::Utils.real_length(field.label.to_s)
|
106
|
-
max_width = max_width_for(field)
|
107
|
-
collection.each do |item|
|
108
|
-
width = [HammerCLI::Output::Utils.real_length(item[field.label]), width].max
|
109
|
-
return max_width if width >= max_width
|
110
|
-
end
|
111
|
-
width
|
112
|
-
end
|
113
|
-
|
114
|
-
private
|
115
|
-
|
116
|
-
def max_width_for(field)
|
117
|
-
if field.parameters[:max_width]
|
118
|
-
[field.parameters[:max_width], MAX_COLUMN_WIDTH].min
|
119
|
-
else
|
120
|
-
MAX_COLUMN_WIDTH
|
121
|
-
end
|
122
|
-
end
|
123
54
|
end
|
124
55
|
|
125
56
|
HammerCLI::Output::Output.register_adapter(:table, Table)
|
@@ -59,68 +59,23 @@ module HammerCLI::Output
|
|
59
59
|
end
|
60
60
|
|
61
61
|
def sets_table
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
hline_bits = ['-' * fields_col_size]
|
67
|
-
field_sets.map do |set|
|
68
|
-
header_bits << normalize_column(set.size, set)
|
69
|
-
hline_bits << '-' * set.size
|
70
|
-
end
|
71
|
-
rows_bits = fields_row(@fields, field_sets, fields_col_size)
|
72
|
-
line = "+-#{hline_bits.join('-+-')}-+\n"
|
73
|
-
table = line
|
74
|
-
table += "| #{header_bits.join(' | ')} |\n"
|
75
|
-
table += line
|
76
|
-
table += "#{rows_bits.join("\n")}\n"
|
77
|
-
table += line
|
78
|
-
table
|
62
|
+
columns = field_sets.unshift(_('Fields'))
|
63
|
+
data = fields_data(@fields, columns).flatten
|
64
|
+
table_gen = HammerCLI::Output::Generators::Table.new(columns, data)
|
65
|
+
table_gen.result
|
79
66
|
end
|
80
67
|
|
81
68
|
private
|
82
69
|
|
83
|
-
def
|
84
|
-
|
85
|
-
|
70
|
+
def fields_data(fields, sets)
|
71
|
+
fields.each_with_object([]) do |field, data|
|
72
|
+
next data << fields_data(field.fields, sets) if field.respond_to?(:fields)
|
86
73
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
padding /= 2
|
92
|
-
col.prepend(' ' * padding)
|
74
|
+
data << sets.each_with_object({}) do |set, res|
|
75
|
+
mark = field.sets.include?(set) ? 'x' : ''
|
76
|
+
value = set == sets.first ? field.full_label : mark
|
77
|
+
res.update(set => value)
|
93
78
|
end
|
94
|
-
col += (' ' * padding)
|
95
|
-
else
|
96
|
-
col, real_len = HammerCLI::Output::Utils.real_truncate(col, width - 3)
|
97
|
-
col += '...'
|
98
|
-
col += ' ' if real_len < (width - 3)
|
99
|
-
end
|
100
|
-
col
|
101
|
-
end
|
102
|
-
|
103
|
-
def fields_row(fields, sets, fields_col_size)
|
104
|
-
fields.each_with_object([]) do |field, rows|
|
105
|
-
next rows << fields_row(field.fields, sets, fields_col_size) if field.respond_to?(:fields)
|
106
|
-
|
107
|
-
row = [normalize_column(fields_col_size, field.full_label)]
|
108
|
-
sets.each do |set|
|
109
|
-
mark = field.sets.include?(set) ? 'x' : ' '
|
110
|
-
column = normalize_column(set.size, mark, centralize: true)
|
111
|
-
column += ' ' unless (set.size - column.size).zero?
|
112
|
-
row << column
|
113
|
-
end
|
114
|
-
rows << "| #{row.join(' | ')} |"
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
def field_labels(fields, full_labels: false)
|
119
|
-
fields.each_with_object([]) do |field, labels|
|
120
|
-
label = full_labels ? field.full_label : field.label
|
121
|
-
next labels << label unless field.respond_to?(:fields)
|
122
|
-
|
123
|
-
labels.concat(field_labels(field.fields, full_labels: full_labels))
|
124
79
|
end
|
125
80
|
end
|
126
81
|
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'hammer_cli/output/utils'
|
2
|
+
|
3
|
+
module HammerCLI
|
4
|
+
module Output
|
5
|
+
module Generators
|
6
|
+
class Table
|
7
|
+
class Column
|
8
|
+
attr_reader :label, :params
|
9
|
+
|
10
|
+
def initialize(label, params = nil)
|
11
|
+
@label = label.to_s
|
12
|
+
@params = params || {}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
MAX_COLUMN_WIDTH = 80
|
17
|
+
MIN_COLUMN_WIDTH = 5
|
18
|
+
|
19
|
+
HLINE = '-'
|
20
|
+
LINE_SEPARATOR = '-|-'
|
21
|
+
COLUMN_SEPARATOR = ' | '
|
22
|
+
|
23
|
+
attr_reader :header, :body, :footer, :result
|
24
|
+
|
25
|
+
def initialize(columns, data, options = {})
|
26
|
+
@columns = columns.map { |label, params| Column.new(label, params) }
|
27
|
+
@data = data
|
28
|
+
@options = options
|
29
|
+
create_table
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def create_header(header_bits, line)
|
35
|
+
result = StringIO.new
|
36
|
+
unless @options[:no_headers]
|
37
|
+
result.puts(line)
|
38
|
+
result.puts(header_bits.join(COLUMN_SEPARATOR))
|
39
|
+
result.puts(line)
|
40
|
+
end
|
41
|
+
result.string
|
42
|
+
end
|
43
|
+
|
44
|
+
def create_body(widths)
|
45
|
+
result = StringIO.new
|
46
|
+
@data.collect do |row|
|
47
|
+
row_bits = @columns.map do |col|
|
48
|
+
normalize_column(widths[col.label], row[col.label] || '')
|
49
|
+
end
|
50
|
+
result.puts(row_bits.join(COLUMN_SEPARATOR))
|
51
|
+
end
|
52
|
+
result.string
|
53
|
+
end
|
54
|
+
|
55
|
+
def create_footer(line)
|
56
|
+
result = StringIO.new
|
57
|
+
result.puts(line) unless @data.empty? || @options[:no_headers]
|
58
|
+
result.string
|
59
|
+
end
|
60
|
+
|
61
|
+
def create_result
|
62
|
+
result = StringIO.new
|
63
|
+
result.print(@header, @body, @footer)
|
64
|
+
result.string
|
65
|
+
end
|
66
|
+
|
67
|
+
def create_table
|
68
|
+
widths = calculate_widths(@columns, @data)
|
69
|
+
header_bits = []
|
70
|
+
hline_bits = []
|
71
|
+
@columns.map do |col|
|
72
|
+
header_bits << normalize_column(widths[col.label], col.label.upcase)
|
73
|
+
hline_bits << HLINE * widths[col.label]
|
74
|
+
end
|
75
|
+
line = hline_bits.join(LINE_SEPARATOR)
|
76
|
+
@header = create_header(header_bits, line)
|
77
|
+
@body = create_body(widths)
|
78
|
+
@footer = create_footer(line)
|
79
|
+
@result = create_result
|
80
|
+
end
|
81
|
+
|
82
|
+
def normalize_column(width, value)
|
83
|
+
value = value.to_s
|
84
|
+
padding = width - HammerCLI::Output::Utils.real_length(value)
|
85
|
+
if padding >= 0
|
86
|
+
value += (' ' * padding)
|
87
|
+
else
|
88
|
+
value, real_length = HammerCLI::Output::Utils.real_truncate(value, width - 3)
|
89
|
+
value += '...'
|
90
|
+
value += ' ' if real_length < (width - 3)
|
91
|
+
end
|
92
|
+
value
|
93
|
+
end
|
94
|
+
|
95
|
+
def calculate_widths(columns, data)
|
96
|
+
Hash[columns.map { |c| [c.label, calculate_column_width(c, data)] }]
|
97
|
+
end
|
98
|
+
|
99
|
+
def calculate_column_width(column, data)
|
100
|
+
if column.params[:width]
|
101
|
+
return [column.params[:width], MIN_COLUMN_WIDTH].max
|
102
|
+
end
|
103
|
+
|
104
|
+
width = HammerCLI::Output::Utils.real_length(column.label)
|
105
|
+
max_width = max_width_for(column)
|
106
|
+
data.each do |item|
|
107
|
+
width = [HammerCLI::Output::Utils.real_length(item[column.label]), width].max
|
108
|
+
return max_width if width >= max_width
|
109
|
+
end
|
110
|
+
width
|
111
|
+
end
|
112
|
+
|
113
|
+
def max_width_for(column)
|
114
|
+
return MAX_COLUMN_WIDTH unless column.params[:max_width]
|
115
|
+
|
116
|
+
column.params[:max_width]
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'hammer_cli/output/generators/table'
|
data/lib/hammer_cli/output.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'output/output')
|
2
2
|
require File.join(File.dirname(__FILE__), 'output/fields')
|
3
3
|
require File.join(File.dirname(__FILE__), 'output/formatters')
|
4
|
+
require File.join(File.dirname(__FILE__), 'output/generators')
|
4
5
|
require File.join(File.dirname(__FILE__), 'output/adapter')
|
5
6
|
require File.join(File.dirname(__FILE__), 'output/definition')
|
6
7
|
require File.join(File.dirname(__FILE__), 'output/dsl')
|
7
8
|
require File.join(File.dirname(__FILE__), 'output/record_collection')
|
8
9
|
require File.join(File.dirname(__FILE__), 'output/field_filter')
|
9
|
-
|
data/lib/hammer_cli/version.rb
CHANGED
data/man/hammer.1.gz
CHANGED
Binary file
|
@@ -266,13 +266,13 @@ describe HammerCLI::Output::Definition do
|
|
266
266
|
end
|
267
267
|
definition.fields += [new_field, cont_field]
|
268
268
|
|
269
|
-
sets_table = "
|
270
|
-
|
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
269
|
+
sets_table = "---------|-----|---------|----\n" \
|
270
|
+
"FIELDS | ALL | DEFAULT | SET\n" \
|
271
|
+
"---------|-----|---------|----\n" \
|
272
|
+
"newfield | x | x | \n" \
|
273
|
+
"cf/abc | | | x \n" \
|
274
|
+
"cf/bca | | | x \n" \
|
275
|
+
"---------|-----|---------|----\n"
|
276
276
|
|
277
277
|
definition.sets_table.must_equal sets_table
|
278
278
|
end
|