output_mode 1.1.1 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c5a9cfc28a765bc0e31db2d2247f346f127cf99c4f4e630048b4882a539c1a03
4
- data.tar.gz: 168a9b20db06828042673f7b4826f77367a10d283d2212a20d88eca3232fcde4
3
+ metadata.gz: ec56475261c0c87de303f06bb9d5c046c71b39a5cdecaaab8c2a6ba0fc9d1602
4
+ data.tar.gz: 6912cc2ae659c9c1ea89c0d2fe38b03537f3de9f54d6b981fc958c4519f78bcd
5
5
  SHA512:
6
- metadata.gz: bbefcfef7c8d46b95113b0877e84de60f852b8c3bac4d74575414215235796195c4f8447044ee92db11029e9edbfc269f092776c69e714fc8d91fc43319230bd
7
- data.tar.gz: 3ff9cc52f21ab132ae2a673100354ff7e84eee824388315abf5563aeb181b35f82920d778e7e89bfbaa1e832bd31d377ce2865e1f5c73d5e88f7d6bafcdbfe6f
6
+ metadata.gz: 0e439ef9c8b4a68bc567d1d7b01d38b0582a85de7e08355c301a6012ef0c95c6c42af1f2c4ee4088cd2edd64566956d9b8e4a3ad94b48eded57f3cfafa0b987d
7
+ data.tar.gz: 321eed2286b0e87ef5f45b2336a8a4c95e477258a00744087bafd85fbd3dfad1a3ee2f8b220cddcaf0a9e6193301de1e708e1890aaf10a5497c08ea4f44e150e
data/README.md CHANGED
@@ -63,5 +63,5 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/Willia
63
63
 
64
64
  ## Copyright and License
65
65
 
66
- See [LICENSE](LICENSE.txt) for dual licensing details.
66
+ See [LICENSE](LICENSE.txt) for licensing details.
67
67
 
data/bin/demo CHANGED
@@ -1,21 +1,42 @@
1
1
  #!/usr/bin/env ruby
2
2
  #==============================================================================
3
- # Refer to LICENSE.txt for licensing terms
3
+ # Copyright 2020 William McCumstie
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # 1. Redistributions of source code must retain the above copyright notice,
9
+ # this list of conditions and the following disclaimer.
10
+ #
11
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ # this list of conditions and the following disclaimer in the documentation
13
+ # and/or other materials provided with the distribution.
14
+ #
15
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25
+ # POSSIBILITY OF SUCH DAMAGE.
4
26
  #==============================================================================
5
27
 
6
-
7
28
  require "bundler/setup"
8
29
  require "output_mode"
9
30
 
10
31
  module DemoIndex
11
32
  extend OutputMode::TLDR::Index
12
33
 
13
- register_callable(header: 'Integer') { |i| i }
14
- register_callable(header: 'Standard') { 'always visible' }
34
+ register_callable(header: 'Integer', row_color: [:yellow, :bold]) { |i| i }
35
+ register_callable(header: 'Standard', header_color: [:strikethrough] ) { 'always visible' }
15
36
  register_callable(header: 'Verbose', verbose: true) { 'verbose visible' }
16
37
  register_callable(header: 'Simplified', verbose: false) { 'simplified visible' }
17
38
  register_callable(header: 'Yes/True') { true }
18
- register_callable(header: 'No/False') { false }
39
+ register_callable(header: 'No/False', row_color: [:clear]) { false }
19
40
  register_callable(header: 'Missing') { nil }
20
41
  end
21
42
 
@@ -34,6 +55,12 @@ end
34
55
  data = [1, 2, 3]
35
56
 
36
57
  puts <<~EOF
58
+ #==============================================================================
59
+ #==============================================================================
60
+ # INDEX OUTPUTS
61
+ #==============================================================================
62
+ #==============================================================================
63
+
37
64
  #==============================================================================
38
65
  # Demo Verbose Index
39
66
  #==============================================================================
@@ -45,6 +72,29 @@ puts <<~EOF
45
72
  #==============================================================================
46
73
  #{DemoIndex.build_output(verbose: false).render(*data)}
47
74
 
75
+ #==============================================================================
76
+ # Force Interactive
77
+ # Always print as if the shell is interactive
78
+ #==============================================================================
79
+ #{DemoIndex.build_output(interactive: true).render(*data)}
80
+
81
+ #==============================================================================
82
+ # Force Non-Interactive
83
+ # Always print as if the shell is non-interactive
84
+ #==============================================================================
85
+ #{DemoIndex.build_output(interactive: false).render(*data)}
86
+
87
+ #==============================================================================
88
+ # Demo ASCII Index
89
+ #==============================================================================
90
+ #{DemoIndex.build_output(ascii: true).render(*data)}
91
+
92
+ #==============================================================================
93
+ #==============================================================================
94
+ # SHOW OUTPUTS
95
+ #==============================================================================
96
+ #==============================================================================
97
+
48
98
  #==============================================================================
49
99
  # Demo Verbose Show
50
100
  #==============================================================================
@@ -55,6 +105,21 @@ puts <<~EOF
55
105
  # NOTE: Disabled for non-interactive shell, shows the verbose output instead
56
106
  #==============================================================================
57
107
  #{DemoShow.build_output(verbose: false).render(*data)}
58
- EOF
59
108
 
109
+ #==============================================================================
110
+ # Force Interactive
111
+ # Always print as if the shell is interactive
112
+ #==============================================================================
113
+ #{DemoShow.build_output(interactive: true).render(*data)}
114
+
115
+ #==============================================================================
116
+ # Force Non-Interactive
117
+ # Always print as if the shell is non-interactive
118
+ #==============================================================================
119
+ #{DemoShow.build_output(interactive: false).render(*data)}
60
120
 
121
+ #==============================================================================
122
+ # Demo ASCII Index
123
+ #==============================================================================
124
+ #{DemoShow.build_output(ascii: true).render(*data)}
125
+ EOF
@@ -29,14 +29,20 @@ require 'tty-table'
29
29
  module OutputMode
30
30
  module Outputs
31
31
  class Tabulated < Output
32
- attr_reader :renderer, :header, :default, :block, :yes, :no
33
-
34
32
  # @!attribute [r] renderer
35
33
  # @return [Symbol] the renderer type, see: https://github.com/piotrmurach/tty-table#32-renderer
36
34
  # @!attribute [r] header
37
35
  # @return [Array] An optional header row for the table
38
36
  # @!attribute [r] block
39
37
  # @return [#call] an optional block of code that configures the renderer
38
+ # @!attribute [r] colorize
39
+ # @return [Boolean] enable or disabled the colorization
40
+ # @!attribute [r] header_color
41
+ # @return An optional header color or array of colors
42
+ # @!attribute [r] row_color
43
+ # @return An optional data color or array of colors
44
+ attr_reader :renderer, :header, :default, :block, :yes, :no,
45
+ :header_color, :row_color, :colorize
40
46
 
41
47
  # @return [Hash] additional options to +TTY::Table+ renderer
42
48
  # @see https://github.com/piotrmurach/tty-table#33-options
@@ -50,12 +56,18 @@ module OutputMode
50
56
  # @yieldparam tty_table_renderer [TTY::Table::Renderer::Base] optional access the underlining TTY::Table renderer
51
57
  def initialize(*procs,
52
58
  renderer: :unicode,
59
+ colorize: false,
53
60
  header: nil,
61
+ header_color: nil,
62
+ row_color: nil,
54
63
  **config,
55
64
  &block)
56
65
  @header = header
57
66
  @renderer = renderer
58
67
  @block = block
68
+ @header_color = header_color
69
+ @row_color = row_color
70
+ @colorize = colorize
59
71
  super(*procs, **config)
60
72
  end
61
73
 
@@ -63,10 +75,46 @@ module OutputMode
63
75
  # @see OutputMode::Outputs::Base#render
64
76
  # @see https://github.com/piotrmurach/tty-table
65
77
  def render(*data)
66
- table = TTY::Table.new header: header
67
- data.each { |d| table << generate(d) }
78
+ table = TTY::Table.new header: processed_header
79
+ data.each { |d| table << process_row(generate(d)) }
68
80
  table.render(renderer, **config, &block) || ''
69
81
  end
82
+
83
+ private
84
+
85
+ # Colorizes the header when requested
86
+ def processed_header
87
+ header&.each_with_index&.map do |h, idx|
88
+ color = index_selector(:header_color, idx)
89
+ case color
90
+ when nil
91
+ h.to_s
92
+ when Array
93
+ pastel.decorate(h.to_s, *color)
94
+ else
95
+ pastel.decorate(h.to_s, color)
96
+ end
97
+ end
98
+ end
99
+
100
+ # Colorizes the row when requested
101
+ def process_row(data)
102
+ data.each_with_index.map do |d, idx|
103
+ color = index_selector(:row_color, idx)
104
+ case color
105
+ when NilClass
106
+ d.to_s
107
+ when Array
108
+ pastel.decorate(d.to_s, *color)
109
+ else
110
+ pastel.decorate(d.to_s, color)
111
+ end
112
+ end
113
+ end
114
+
115
+ def pastel
116
+ @pastel ||= Pastel::Color.new(enabled: colorize)
117
+ end
70
118
  end
71
119
  end
72
120
  end
@@ -1,7 +1,31 @@
1
1
  #==============================================================================
2
- # Refer to LICENSE.txt for licensing terms
2
+ # Copyright 2020 William McCumstie
3
+ #
4
+ # Redistribution and use in source and binary forms, with or without
5
+ # modification, are permitted provided that the following conditions are met:
6
+ #
7
+ # 1. Redistributions of source code must retain the above copyright notice,
8
+ # this list of conditions and the following disclaimer.
9
+ #
10
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
11
+ # this list of conditions and the following disclaimer in the documentation
12
+ # and/or other materials provided with the distribution.
13
+ #
14
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24
+ # POSSIBILITY OF SUCH DAMAGE.
3
25
  #==============================================================================
4
26
 
27
+ require 'tty-color'
28
+
5
29
  module OutputMode
6
30
  module TLDR
7
31
  module Index
@@ -11,9 +35,12 @@ module OutputMode
11
35
  # @overload register_callable(header:, verbose: true)
12
36
  # @param header: The column's header field when displaying to humans
13
37
  # @param verbose: Whether the column will be shown in the verbose output
38
+ # @param header_color: Override the default color for the header
39
+ # @param row_color: Override the default color for the row
14
40
  # @yieldparam model The subject the column is describing, some sort of data model
15
- def register_callable(header:, verbose: nil, &b)
16
- super(modes: { verbose: verbose }, header: header, &b)
41
+ def register_callable(header:, verbose: nil, header_color: nil, row_color: nil, &b)
42
+ super(modes: { verbose: verbose }, header: header,
43
+ header_color: header_color, row_color: row_color, &b)
17
44
  end
18
45
  alias_method :register_column, :register_callable
19
46
 
@@ -21,6 +48,12 @@ module OutputMode
21
48
  # +$stdout+ as part of it's output class discovery logic. It does not
22
49
  # print to the output directly
23
50
  #
51
+ # The +ascii+ flag disables the unicode formatting in interactive shells.
52
+ # Non interactive shells use ASCII by default.
53
+ #
54
+ # The +verbose+ flag toggles the simplified and verbose outputs in the
55
+ # interactive output. Non-interactive outputs are always verbose
56
+ #
24
57
  # If +$stdout+ is an interactive shell (aka a TTY), then it will display using
25
58
  # {OutputMode::Outputs::Tabulated}. This is intended for human consumption
26
59
  # and will obey the provided +verbose+ flag.
@@ -29,26 +62,39 @@ module OutputMode
29
62
  # {OutputMode::Outputs::Delimited} using tab delimiters. This is intended
30
63
  # for consumption by machines. This output ignores the provided +verbose+
31
64
  # flag as it is always verbose.
32
- def build_output(verbose: false)
65
+ #
66
+ # An interative/ non-interactive output can be forced by setting the
67
+ # +interactive+ flag to +true+/+false+ respectively
68
+ def build_output(verbose: false, ascii: false, interactive: nil, header_color: [:blue, :bold], row_color: :green)
33
69
  callables = if verbose || !$stdout.tty?
34
70
  # Filter out columns that are explicitly not verbose
35
- output_callables.select(&:verbose!)
71
+ output_callables.select { |o| o.verbose?(true) }
36
72
  else
37
73
  # Filter out columns that are explicitly verbose
38
74
  output_callables.reject(&:verbose?)
39
75
  end
40
76
 
41
- if $stdout.tty?
77
+ if interactive || (interactive.nil? && $stdout.tty?)
42
78
  # Creates the human readable output
79
+ opts = if ascii
80
+ { yes: 'yes', no: 'no', renderer: :ascii }
81
+ else
82
+ {
83
+ yes: '✓', no: '✕', renderer: :unicode, colorize: TTY::Color.color?,
84
+ header_color: callables.map { |c| c.config[:header_color] || header_color },
85
+ row_color: callables.map { |c| c.config[:row_color] || row_color }
86
+ }
87
+ end
88
+
43
89
  Outputs::Tabulated.new(*callables,
44
90
  header: callables.map { |c| c.config.fetch(:header, 'missing') },
45
- renderer: :unicode,
46
91
  padding: [0,1],
47
92
  default: '(none)',
48
- yes: '✓', no: '✕')
93
+ **opts
94
+ )
49
95
  else
50
96
  # Creates the machine readable output
51
- Outputs::Delimited.new(*callables, col_sep: "\t", yes: 'y', no: 'n', default: '')
97
+ Outputs::Delimited.new(*callables, col_sep: "\t", yes: 'yes', no: 'no', default: '')
52
98
  end
53
99
  end
54
100
  end
@@ -1,5 +1,27 @@
1
1
  #==============================================================================
2
- # Refer to LICENSE.txt for licensing terms
2
+ # Copyright 2020 William McCumstie
3
+ #
4
+ # Redistribution and use in source and binary forms, with or without
5
+ # modification, are permitted provided that the following conditions are met:
6
+ #
7
+ # 1. Redistributions of source code must retain the above copyright notice,
8
+ # this list of conditions and the following disclaimer.
9
+ #
10
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
11
+ # this list of conditions and the following disclaimer in the documentation
12
+ # and/or other materials provided with the distribution.
13
+ #
14
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24
+ # POSSIBILITY OF SUCH DAMAGE.
3
25
  #==============================================================================
4
26
 
5
27
  require 'tty-color'
@@ -23,6 +45,12 @@ module OutputMode
23
45
  # +$stdout+ as part of it's output class discovery logic. It does not
24
46
  # print to the io directly
25
47
  #
48
+ # The +ascii+ flag disables the unicode formatting in interactive shells.
49
+ # Non interactive shells use ASCII by default.
50
+ #
51
+ # The +verbose+ flag toggles the simplified and verbose outputs in the
52
+ # interactive output. Non-interactive outputs are always verbose
53
+ #
26
54
  # If +$stdout+ is an interactive shell (aka a TTY), then it will display using
27
55
  # {OutputMode::Outputs::Templated}. This is intended for human consumption
28
56
  # and will obey the provided +verbose+ flag.
@@ -31,25 +59,33 @@ module OutputMode
31
59
  # {OutputMode::Outputs::Delimited} using tab delimiters. This is intended
32
60
  # for consumption by machines. This output ignores the provided +verbose+
33
61
  # flag as it is always verbose.
34
- def build_output(verbose: false)
62
+ #
63
+ # An interative/ non-interactive output can be forced by setting the
64
+ # +interactive+ flag to +true+/+false+ respectively
65
+ def build_output(verbose: false, ascii: false, interactive: nil)
35
66
  callables = if verbose || !$stdout.tty?
36
67
  # Filter out columns that are explicitly not verbose
37
- output_callables.select(&:verbose!)
68
+ output_callables.select { |o| o.verbose?(true) }
38
69
  else
39
70
  # Filter out columns that are explicitly verbose
40
71
  output_callables.reject(&:verbose?)
41
72
  end
42
73
 
43
- if $stdout.tty?
74
+ if interactive || (interactive.nil? && $stdout.tty?)
44
75
  # 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? }
80
+ end
81
+
45
82
  Outputs::Templated.new(*callables,
46
83
  fields: callables.map { |c| c.config.fetch(:header, 'missing') },
47
- colorize: TTY::Color.color?,
48
84
  default: '(none)',
49
- yes: '✓', no: '✕')
85
+ **opts)
50
86
  else
51
87
  # Creates the machine readable output
52
- Outputs::Delimited.new(*callables, col_sep: "\t", yes: 'y', no: 'n', default: '')
88
+ Outputs::Delimited.new(*callables, col_sep: "\t", yes: 'yes', no: 'no', default: '')
53
89
  end
54
90
  end
55
91
  end
@@ -25,5 +25,5 @@
25
25
  #==============================================================================
26
26
 
27
27
  module OutputMode
28
- VERSION = "1.1.1"
28
+ VERSION = "1.2.2"
29
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: output_mode
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - William McCumsite
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-08-21 00:00:00.000000000 Z
11
+ date: 2020-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tty-table