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.
Files changed (148) hide show
  1. checksums.yaml +4 -4
  2. data/doc/release_notes.md +4 -0
  3. data/lib/hammer_cli/output/adapter/table.rb +6 -75
  4. data/lib/hammer_cli/output/definition.rb +11 -56
  5. data/lib/hammer_cli/output/generators/table.rb +121 -0
  6. data/lib/hammer_cli/output/generators.rb +1 -0
  7. data/lib/hammer_cli/output.rb +1 -1
  8. data/lib/hammer_cli/version.rb +1 -1
  9. data/man/hammer.1.gz +0 -0
  10. data/test/unit/output/definition_test.rb +7 -7
  11. metadata +67 -340
  12. data/test/reports/TEST-Fields-ContainerField-display-.xml +0 -7
  13. data/test/reports/TEST-Fields-ContainerField-display-blank-is-allowed.xml +0 -15
  14. data/test/reports/TEST-Fields-ContainerField-display-blank-is-not-allowed.xml +0 -15
  15. data/test/reports/TEST-Fields-ContainerField.xml +0 -7
  16. data/test/reports/TEST-Fields-Field-display-.xml +0 -7
  17. data/test/reports/TEST-Fields-Field-display-blank-is-allowed.xml +0 -11
  18. data/test/reports/TEST-Fields-Field-display-blank-is-not-allowed.xml +0 -11
  19. data/test/reports/TEST-Fields-Field-hide-blank-.xml +0 -11
  20. data/test/reports/TEST-Fields-Field-parameters.xml +0 -9
  21. data/test/reports/TEST-Fields-Field.xml +0 -13
  22. data/test/reports/TEST-HammerCLI-AbstractCommand-build-options.xml +0 -15
  23. data/test/reports/TEST-HammerCLI-AbstractCommand-exception-handler.xml +0 -13
  24. data/test/reports/TEST-HammerCLI-AbstractCommand-logging.xml +0 -21
  25. data/test/reports/TEST-HammerCLI-AbstractCommand-option-builder.xml +0 -11
  26. data/test/reports/TEST-HammerCLI-AbstractCommand-options.xml +0 -11
  27. data/test/reports/TEST-HammerCLI-AbstractCommand-output.xml +0 -19
  28. data/test/reports/TEST-HammerCLI-AbstractCommand-subcommand-behavior-remove-subcommand.xml +0 -11
  29. data/test/reports/TEST-HammerCLI-AbstractCommand-subcommand-behavior-subcommand-.xml +0 -13
  30. data/test/reports/TEST-HammerCLI-AbstractCommand-subcommand-behavior-subcommand.xml +0 -11
  31. data/test/reports/TEST-HammerCLI-AbstractCommand-subcommand-behavior.xml +0 -7
  32. data/test/reports/TEST-HammerCLI-AbstractCommand.xml +0 -11
  33. data/test/reports/TEST-HammerCLI-Apipie-Command-options.xml +0 -11
  34. data/test/reports/TEST-HammerCLI-Apipie-Command-resource-defined.xml +0 -9
  35. data/test/reports/TEST-HammerCLI-Apipie-Command-setting-resources.xml +0 -19
  36. data/test/reports/TEST-HammerCLI-Apipie-Command.xml +0 -9
  37. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-aliasing-resources.xml +0 -13
  38. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-filtering-options.xml +0 -15
  39. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-required-options.xml +0 -11
  40. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-setting-correct-normalizers.xml +0 -9
  41. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-with-hash-params.xml +0 -11
  42. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-with-one-simple-param.xml +0 -15
  43. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder.xml +0 -7
  44. data/test/reports/TEST-HammerCLI-Completer-command-completion.xml +0 -29
  45. data/test/reports/TEST-HammerCLI-Completer-option-value-completion.xml +0 -17
  46. data/test/reports/TEST-HammerCLI-Completer-subcommand-completion.xml +0 -19
  47. data/test/reports/TEST-HammerCLI-Completer.xml +0 -7
  48. data/test/reports/TEST-HammerCLI-CompleterLine-line-complete.xml +0 -25
  49. data/test/reports/TEST-HammerCLI-CompleterLine-splitting-words.xml +0 -29
  50. data/test/reports/TEST-HammerCLI-CompleterLine.xml +0 -7
  51. data/test/reports/TEST-HammerCLI-CompleterWord-complete-.xml +0 -23
  52. data/test/reports/TEST-HammerCLI-CompleterWord-quote.xml +0 -15
  53. data/test/reports/TEST-HammerCLI-CompleterWord-quoted-.xml +0 -13
  54. data/test/reports/TEST-HammerCLI-CompleterWord.xml +0 -7
  55. data/test/reports/TEST-HammerCLI-Connection.xml +0 -21
  56. data/test/reports/TEST-HammerCLI-ExceptionHandler.xml +0 -21
  57. data/test/reports/TEST-HammerCLI-I18n.xml +0 -11
  58. data/test/reports/TEST-HammerCLI-MainCommand-loading-context-password.xml +0 -11
  59. data/test/reports/TEST-HammerCLI-MainCommand-loading-context-username.xml +0 -11
  60. data/test/reports/TEST-HammerCLI-MainCommand-loading-context-verbose.xml +0 -9
  61. data/test/reports/TEST-HammerCLI-MainCommand-loading-context.xml +0 -7
  62. data/test/reports/TEST-HammerCLI-MainCommand.xml +0 -7
  63. data/test/reports/TEST-HammerCLI-Modules-find-by-name.xml +0 -13
  64. data/test/reports/TEST-HammerCLI-Modules-load-a-module-module-not-found.xml +0 -13
  65. data/test/reports/TEST-HammerCLI-Modules-load-a-module-module-runtime-exception.xml +0 -13
  66. data/test/reports/TEST-HammerCLI-Modules-load-a-module-success.xml +0 -15
  67. data/test/reports/TEST-HammerCLI-Modules-load-a-module.xml +0 -7
  68. data/test/reports/TEST-HammerCLI-Modules-load-all-modules.xml +0 -9
  69. data/test/reports/TEST-HammerCLI-Modules-names.xml +0 -13
  70. data/test/reports/TEST-HammerCLI-Modules.xml +0 -7
  71. data/test/reports/TEST-HammerCLI-OptionBuilderContainer.0.xml +0 -7
  72. data/test/reports/TEST-HammerCLI-OptionBuilderContainer.xml +0 -11
  73. data/test/reports/TEST-HammerCLI-Options-Normalizers-abstract.xml +0 -9
  74. data/test/reports/TEST-HammerCLI-Options-Normalizers-bool.xml +0 -31
  75. data/test/reports/TEST-HammerCLI-Options-Normalizers-datetime.xml +0 -17
  76. data/test/reports/TEST-HammerCLI-Options-Normalizers-enum.xml +0 -15
  77. data/test/reports/TEST-HammerCLI-Options-Normalizers-enumlist.xml +0 -21
  78. data/test/reports/TEST-HammerCLI-Options-Normalizers-json-input.xml +0 -15
  79. data/test/reports/TEST-HammerCLI-Options-Normalizers-key-value-list.xml +0 -17
  80. data/test/reports/TEST-HammerCLI-Options-Normalizers-list.xml +0 -15
  81. data/test/reports/TEST-HammerCLI-Options-Normalizers.xml +0 -7
  82. data/test/reports/TEST-HammerCLI-Options-OptionDefinition-context.xml +0 -9
  83. data/test/reports/TEST-HammerCLI-Options-OptionDefinition-formatters.xml +0 -11
  84. data/test/reports/TEST-HammerCLI-Options-OptionDefinition.xml +0 -7
  85. data/test/reports/TEST-HammerCLI-Output-Adapter-Abstract-error-messages.xml +0 -15
  86. data/test/reports/TEST-HammerCLI-Output-Adapter-Abstract-messages.xml +0 -11
  87. data/test/reports/TEST-HammerCLI-Output-Adapter-Abstract-test-data-for-field.xml +0 -15
  88. data/test/reports/TEST-HammerCLI-Output-Adapter-Abstract.xml +0 -17
  89. data/test/reports/TEST-HammerCLI-Output-Adapter-Base-print-collection-show-ids.xml +0 -9
  90. data/test/reports/TEST-HammerCLI-Output-Adapter-Base-print-collection.xml +0 -27
  91. data/test/reports/TEST-HammerCLI-Output-Adapter-Base.xml +0 -7
  92. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection-formatters.xml +0 -11
  93. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection-handle-fields-with-collections.xml +0 -13
  94. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection-handle-fields-with-containers.xml +0 -11
  95. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection-handle-ids.xml +0 -11
  96. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection.xml +0 -11
  97. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-message.xml +0 -11
  98. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues.xml +0 -7
  99. data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection-column-width.xml +0 -15
  100. data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection-formatters.xml +0 -11
  101. data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection-handle-ids.xml +0 -11
  102. data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection-sort-columns.xml +0 -9
  103. data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection.xml +0 -11
  104. data/test/reports/TEST-HammerCLI-Output-Adapter-Table.xml +0 -7
  105. data/test/reports/TEST-HammerCLI-Output-Definition-empty-.xml +0 -11
  106. data/test/reports/TEST-HammerCLI-Output-Definition.xml +0 -11
  107. data/test/reports/TEST-HammerCLI-Output-Dsl-collection.xml +0 -13
  108. data/test/reports/TEST-HammerCLI-Output-Dsl-custom-fields.xml +0 -11
  109. data/test/reports/TEST-HammerCLI-Output-Dsl-fields.xml +0 -15
  110. data/test/reports/TEST-HammerCLI-Output-Dsl-label.xml +0 -13
  111. data/test/reports/TEST-HammerCLI-Output-Dsl-path-definition.xml +0 -13
  112. data/test/reports/TEST-HammerCLI-Output-Dsl.xml +0 -9
  113. data/test/reports/TEST-HammerCLI-Output-FieldFilter.xml +0 -13
  114. data/test/reports/TEST-HammerCLI-Output-Formatters-BooleanFormatter.xml +0 -11
  115. data/test/reports/TEST-HammerCLI-Output-Formatters-ColorFormatter.xml +0 -9
  116. data/test/reports/TEST-HammerCLI-Output-Formatters-DateFormatter.xml +0 -11
  117. data/test/reports/TEST-HammerCLI-Output-Formatters-FieldFormatter.xml +0 -13
  118. data/test/reports/TEST-HammerCLI-Output-Formatters-FormatterContainer.xml +0 -13
  119. data/test/reports/TEST-HammerCLI-Output-Formatters-FormatterLibrary.xml +0 -11
  120. data/test/reports/TEST-HammerCLI-Output-Formatters-KeyValueFormatter.xml +0 -13
  121. data/test/reports/TEST-HammerCLI-Output-Formatters-ListFormatter.xml +0 -13
  122. data/test/reports/TEST-HammerCLI-Output-Formatters-LongTextFormatter.xml +0 -13
  123. data/test/reports/TEST-HammerCLI-Output-Output-adapters.xml +0 -17
  124. data/test/reports/TEST-HammerCLI-Output-Output-data.xml +0 -15
  125. data/test/reports/TEST-HammerCLI-Output-Output-formatters.xml +0 -9
  126. data/test/reports/TEST-HammerCLI-Output-Output-messages.xml +0 -19
  127. data/test/reports/TEST-HammerCLI-Output-Output.xml +0 -7
  128. data/test/reports/TEST-HammerCLI-Output-RecordCollection.xml +0 -13
  129. data/test/reports/TEST-HammerCLI-Settings-load-from-paths.xml +0 -15
  130. data/test/reports/TEST-HammerCLI-Settings.xml +0 -25
  131. data/test/reports/TEST-HammerCLI-ShellHistory-loading-old-history.xml +0 -11
  132. data/test/reports/TEST-HammerCLI-ShellHistory-saving-history.xml +0 -15
  133. data/test/reports/TEST-HammerCLI-ShellHistory.xml +0 -7
  134. data/test/reports/TEST-MiniTest-Spec.xml +0 -7
  135. data/test/reports/TEST-String-camelize.xml +0 -11
  136. data/test/reports/TEST-String-formatting.xml +0 -17
  137. data/test/reports/TEST-String-indent.xml +0 -11
  138. data/test/reports/TEST-String-interactive-.xml +0 -13
  139. data/test/reports/TEST-String.xml +0 -7
  140. data/test/reports/TEST-constraints-HammerCLI-Validator-AllConstraint-exist-.xml +0 -13
  141. data/test/reports/TEST-constraints-HammerCLI-Validator-AllConstraint.xml +0 -7
  142. data/test/reports/TEST-constraints-HammerCLI-Validator-AnyConstraint-exist-.xml +0 -13
  143. data/test/reports/TEST-constraints-HammerCLI-Validator-AnyConstraint.xml +0 -7
  144. data/test/reports/TEST-constraints-HammerCLI-Validator-BaseConstraint-exist-.xml +0 -9
  145. data/test/reports/TEST-constraints-HammerCLI-Validator-BaseConstraint-rejected.xml +0 -13
  146. data/test/reports/TEST-constraints-HammerCLI-Validator-BaseConstraint-required.xml +0 -13
  147. data/test/reports/TEST-constraints-HammerCLI-Validator-BaseConstraint.xml +0 -7
  148. data/test/reports/TEST-constraints.xml +0 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 257e54094a8995f71d00dbe29299e84801b68404271de7baaad58b6a9207fe93
4
- data.tar.gz: f14b2acba71f1a4ad5a237f7dc6da746a23245ba30c504e689bf5a319e5e86b2
3
+ metadata.gz: fba56b0d34d9914c40df8f424e68d9268d7e2a38e5f2dd421e10991f477fbd1d
4
+ data.tar.gz: 5b1e6d8ea29af809ab09884cc2579c3f328439a7d102335940820bd32ec3289d
5
5
  SHA512:
6
- metadata.gz: 559d6704483f5dd8bb2f27ba7c0f8d3c4212033297168fad4cdea0696ee4cd31f8421f1e79cd758b9f4228ff45d2c41751f7509091de0f92ab86a1dab4d97649
7
- data.tar.gz: 2396627dc3f2b9d97dca8b0501dc2260b6c02d4a59fbf64e2fbfcdf625d913703dd02d47557193d575156f5a2b1270c81c57ff8caae08345675567d063ecc892
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
- line = hline_bits.join(LINE_SEPARATOR)
43
- unless @context[:no_headers]
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
- formatted_collection.collect do |row|
50
- row_bits = fields.map do |f|
51
- normalize_column(widths[f.label], row[f.label] || "")
52
- end
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
- fields_col_size = max_label_length || _('Fields').size
63
- fields_col = normalize_column(fields_col_size, _('Fields'), centralize: true)
64
- fields_col += ' ' unless (fields_col_size - fields_col.size).zero?
65
- header_bits = [fields_col]
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 max_label_length
84
- field_labels(@fields, full_labels: true).map(&:size).max
85
- end
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
- def normalize_column(width, col, centralize: false)
88
- padding = width - HammerCLI::Output::Utils.real_length(col)
89
- if padding >= 0
90
- if centralize
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'
@@ -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
-
@@ -1,5 +1,5 @@
1
1
  module HammerCLI
2
2
  def self.version
3
- @version ||= Gem::Version.new "0.19.0"
3
+ @version ||= Gem::Version.new "0.19.1"
4
4
  end
5
5
  end
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
- | Fields | ALL | DEFAULT | SET |
271
- +----------+-----+---------+-----+
272
- | newfield | x | x | |
273
- | cf/abc | | | x |
274
- | cf/bca | | | x |
275
- +----------+-----+---------+-----+\n"
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