output_mode 1.2.2 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ec56475261c0c87de303f06bb9d5c046c71b39a5cdecaaab8c2a6ba0fc9d1602
4
- data.tar.gz: 6912cc2ae659c9c1ea89c0d2fe38b03537f3de9f54d6b981fc958c4519f78bcd
3
+ metadata.gz: aba3d3f1c1ef5bd16dc8397b9500218ccc652af7dc6c11d85dfbb16cd7ec0ee0
4
+ data.tar.gz: 9568bee91c9a89576fa24a97e8d0fba2cbf86f28fb4d2f6ede3093b773bade17
5
5
  SHA512:
6
- metadata.gz: 0e439ef9c8b4a68bc567d1d7b01d38b0582a85de7e08355c301a6012ef0c95c6c42af1f2c4ee4088cd2edd64566956d9b8e4a3ad94b48eded57f3cfafa0b987d
7
- data.tar.gz: 321eed2286b0e87ef5f45b2336a8a4c95e477258a00744087bafd85fbd3dfad1a3ee2f8b220cddcaf0a9e6193301de1e708e1890aaf10a5497c08ea4f44e150e
6
+ metadata.gz: 409dc9ad72f0a1771ad325995905bde702dcae4d3839b983c7cf02a5ace8927ef9cecc70954666dbce3a634b144d0ee8e8bf3f7981a55edcc52c480545ce36eb
7
+ data.tar.gz: 8663a174778b268e3ea596a81856ff2f53b3a4d3387e68e329bddb38f6d3e002bfc8838d3634304a7680cf8a998db99468dc18b210356259522b1c69aea34d00
data/bin/demo CHANGED
@@ -27,6 +27,7 @@
27
27
 
28
28
  require "bundler/setup"
29
29
  require "output_mode"
30
+ require 'erb'
30
31
 
31
32
  module DemoIndex
32
33
  extend OutputMode::TLDR::Index
@@ -47,13 +48,25 @@ module DemoShow
47
48
  register_callable(header: 'Standard') { 'always visible' }
48
49
  register_callable(header: 'Verbose', verbose: true) { 'verbose visible' }
49
50
  register_callable(header: 'Simplified', verbose: false) { 'simplified visible' }
50
- register_callable(header: 'Yes/True') { true }
51
- register_callable(header: 'No/False') { false }
51
+ register_callable(header: 'Yes/True', section: :boolean) { true }
52
+ register_callable(header: 'No/False', section: :boolean) { false }
52
53
  register_callable(header: 'Missing') { nil }
53
54
  end
54
55
 
55
56
  data = [1, 2, 3]
56
57
 
58
+ other_template = ERB.new(<<~TEMPLATE, nil, '-')
59
+ # Non boolean values
60
+ <% each(:other) do |value, field:, padding:, **_| -%>
61
+ <%= padding -%><%= pastel.blue.bold field -%><%= pastel.bold ':' -%> <%= pastel.green value %>
62
+ <% end -%>
63
+
64
+ # Boolean Values
65
+ <% each(:boolean) do |value, field:, padding:, **_| -%>
66
+ <%= padding -%><%= pastel.blue.bold field -%><%= pastel.bold ':' -%> <%= pastel.green value %>
67
+ <% end -%>
68
+ TEMPLATE
69
+
57
70
  puts <<~EOF
58
71
  #==============================================================================
59
72
  #==============================================================================
@@ -122,4 +135,11 @@ puts <<~EOF
122
135
  # Demo ASCII Index
123
136
  #==============================================================================
124
137
  #{DemoShow.build_output(ascii: true).render(*data)}
138
+
139
+ #==============================================================================
140
+ # Group the boolean value separately
141
+ # NOTE: This only occurs in interactive mode
142
+ # Non-Interactive sessions have a fix order
143
+ #==============================================================================
144
+ #{DemoShow.build_output(template: other_template).render(*data)}
125
145
  EOF
@@ -38,9 +38,23 @@ module OutputMode
38
38
  # @yieldparam field: An optional field header for the value
39
39
  # @yieldparam padding: A padding string which will right align the +field+
40
40
  # @yieldparam **config TBA
41
- def each
42
- max = output.max_field_length
43
- output.generate(model).each_with_index do |value, idx|
41
+ def each(section = nil)
42
+ # Select the indices for the relevant section
43
+ indices = (0...output.procs.length).to_a
44
+ if section
45
+ indices.select! do |idx|
46
+ output.index_selector(:sections, idx) == section
47
+ end
48
+ end
49
+
50
+ # Find the max field length
51
+ max = indices.map do |idx|
52
+ output.index_selector(:fields, idx).to_s.length
53
+ end.max
54
+
55
+ # Yield each selected attribute
56
+ indices.each do |idx|
57
+ value = generated[idx]
44
58
  field = output.index_selector(:fields, idx)
45
59
  padding = ' ' * (max - field.to_s.length)
46
60
  yield(value, field: field, padding: padding)
@@ -61,6 +75,12 @@ module OutputMode
61
75
  def pastel
62
76
  @pastel ||= Pastel.new(enabled: colorize)
63
77
  end
78
+
79
+ private
80
+
81
+ def generated
82
+ @generated ||= output.generate(model)
83
+ end
64
84
  end
65
85
 
66
86
  # @!attribute [r] erb
@@ -68,7 +88,8 @@ module OutputMode
68
88
  # @!attribute [r] separator
69
89
  # @!attribute [r] fields
70
90
  # @!attribute [r] colorize
71
- attr_reader :erb, :fields, :separator, :colorize
91
+ # @!attribute [r] sections
92
+ attr_reader :erb, :fields, :separator, :colorize, :sections
72
93
 
73
94
  # Create a new +output+ which will render using +ERB+. The provided +template+ should
74
95
  # only render the +output+ for a single +entry+ (aka model, record, data object, etc).
@@ -84,23 +105,32 @@ module OutputMode
84
105
  #
85
106
  # @overload initialize(*procs, template: nil, fields: nil, seperator: "\n", yes: 'true', no: 'false', **config)
86
107
  # @param [Array] *procs see {OutputMode::Output#initialize}
87
- # @param [String] template: A string to be converted into +ERB+
88
108
  # @param [ERB] template: The +template+ object used by the renderer
89
109
  # @param [Array] fields: An optional array of field headers that map to the procs, repeating the last value if required
90
110
  # @param fields: A static value to use as all field headers
91
111
  # @param separator: The character(s) used to join the "entries" together
92
112
  # @param colorize: Flags if the caller wants the colorized version, this maybe ignored by +template+
113
+ # @param sections: An optional array that groups the procs into sections. This is ignored by default
93
114
  # @param [Hash] **config see {OutputMode::Output#initialize}
94
115
  def initialize(*procs,
95
116
  template: nil,
96
117
  fields: nil,
97
118
  separator: "\n",
98
119
  colorize: false,
120
+ sections: nil,
99
121
  **config)
100
- @erb = DEFAULT_ERB
122
+ @erb = case template
123
+ when String
124
+ ERB.new(template, nil, '-')
125
+ when ERB
126
+ template
127
+ else
128
+ DEFAULT_ERB
129
+ end
101
130
  @fields = fields
102
131
  @separator = separator
103
132
  @colorize = colorize
133
+ @sections = sections
104
134
  super(*procs, **config)
105
135
  end
106
136
 
@@ -35,9 +35,10 @@ module OutputMode
35
35
  # @overload register_callable(header:, verbose: true)
36
36
  # @param header: The human readable key to the field, uses the term 'header' for consistency
37
37
  # @param verbose: Whether the field will be shown in the verbose output
38
+ # @param section: Define the grouping a callable belongs to. Ignored by default
38
39
  # @yieldparam model The subject the column is describing, some sort of data model
39
- def register_callable(header:, verbose: nil, &b)
40
- super(modes: { verbose: verbose }, header: header, &b)
40
+ def register_callable(header:, verbose: nil, section: :other, &b)
41
+ super(modes: { verbose: verbose }, header: header, section: section, &b)
41
42
  end
42
43
  alias_method :register_attribute, :register_callable
43
44
 
@@ -60,9 +61,11 @@ module OutputMode
60
61
  # for consumption by machines. This output ignores the provided +verbose+
61
62
  # flag as it is always verbose.
62
63
  #
64
+ # The +template+ overrides the default erb template for the output
65
+ #
63
66
  # An interative/ non-interactive output can be forced by setting the
64
67
  # +interactive+ flag to +true+/+false+ respectively
65
- def build_output(verbose: false, ascii: false, interactive: nil)
68
+ def build_output(verbose: false, ascii: false, interactive: nil, template: nil)
66
69
  callables = if verbose || !$stdout.tty?
67
70
  # Filter out columns that are explicitly not verbose
68
71
  output_callables.select { |o| o.verbose?(true) }
@@ -73,15 +76,19 @@ module OutputMode
73
76
 
74
77
  if interactive || (interactive.nil? && $stdout.tty?)
75
78
  # Creates the human readable output
76
- opts = if ascii
77
- { yes: 'yes', no: 'no', colorize: false }
78
- else
79
- { yes: '✓', no: '✕', colorize: TTY::Color.color? }
79
+ opts = if ascii
80
+ { yes: 'yes', no: 'no', colorize: false }
81
+ else
82
+ { yes: '✓', no: '✕', colorize: TTY::Color.color? }
80
83
  end
81
84
 
85
+ sections = callables.map { |o| o.config[:section] }
86
+
82
87
  Outputs::Templated.new(*callables,
83
88
  fields: callables.map { |c| c.config.fetch(:header, 'missing') },
84
89
  default: '(none)',
90
+ sections: sections,
91
+ template: template,
85
92
  **opts)
86
93
  else
87
94
  # Creates the machine readable output
@@ -25,5 +25,5 @@
25
25
  #==============================================================================
26
26
 
27
27
  module OutputMode
28
- VERSION = "1.2.2"
28
+ VERSION = "1.3.0"
29
29
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: output_mode
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - William McCumsite