helium-console 0.1.12 → 0.1.13
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 +4 -4
- data/.github/workflows/pull_request.yml +37 -0
- data/.rspec +0 -1
- data/.rubocop.yml +18 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +9 -1
- data/lib/helium/console/colorized_string.rb +13 -1
- data/lib/helium/console/formatter.rb +13 -2
- data/lib/helium/console/formatters/overflow/wrap.rb +2 -0
- data/lib/helium/console/key_value.rb +130 -0
- data/lib/helium/console/printer.rb +8 -6
- data/lib/helium/console/prompt.rb +4 -2
- data/lib/helium/console/registry/array.rb +5 -4
- data/lib/helium/console/registry/date.rb +1 -1
- data/lib/helium/console/registry/hash.rb +10 -7
- data/lib/helium/console/registry/method.rb +26 -15
- data/lib/helium/console/registry/numeric.rb +6 -0
- data/lib/helium/console/registry/object.rb +10 -8
- data/lib/helium/console/registry/set.rb +9 -5
- data/lib/helium/console/registry.rb +1 -1
- data/lib/helium/console/version.rb +1 -1
- data/lib/helium/console.rb +16 -15
- metadata +5 -5
- data/lib/helium/console/registry/table.rb +0 -81
- data/lib/helium/console/table.rb +0 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a7a2bce9c0f3fe1f94baf382a4f23d10b9fab87164dddbce7736a00a10357cf4
|
4
|
+
data.tar.gz: 420ff6d68c766405c2f29a7ee37cd7fa4d78341ac52769341c4ed54f995ccd24
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 83528cf238a38ca7de2a49534233a1e8c2bce5c761291b1115114e7ac814096e669008445f063fe2eb1f3cdd4ef12de02258547344098cf776bed721ed95d666
|
7
|
+
data.tar.gz: 19f87c531bf302a9d66d8eced560fe81303d728ea778937396d4d439fc987d5fd9a97a88c39976df3431a65ca469c702f0aacca232e57587bbade9fd4ba99de0
|
@@ -0,0 +1,37 @@
|
|
1
|
+
name: Pull request
|
2
|
+
|
3
|
+
on:
|
4
|
+
pull_request:
|
5
|
+
|
6
|
+
jobs:
|
7
|
+
test:
|
8
|
+
name: Spec
|
9
|
+
runs-on: ubuntu-latest
|
10
|
+
strategy:
|
11
|
+
matrix:
|
12
|
+
ruby-version: ['2.6', '2.7', '3.0']
|
13
|
+
fail-fast: false
|
14
|
+
|
15
|
+
steps:
|
16
|
+
- uses: actions/checkout@v2
|
17
|
+
- name: Set up Ruby
|
18
|
+
uses: ruby/setup-ruby@v1
|
19
|
+
with:
|
20
|
+
ruby-version: ${{ matrix.ruby-version }}
|
21
|
+
bundler-cache: true
|
22
|
+
- name: Run tests
|
23
|
+
run: bundle exec rspec
|
24
|
+
|
25
|
+
lint:
|
26
|
+
name: Lint
|
27
|
+
runs-on: ubuntu-latest
|
28
|
+
|
29
|
+
steps:
|
30
|
+
- uses: actions/checkout@v2
|
31
|
+
- name: Set up Ruby
|
32
|
+
uses: ruby/setup-ruby@v1
|
33
|
+
with:
|
34
|
+
ruby-version: 2.7
|
35
|
+
bundler-cache: true
|
36
|
+
- name: Run tests
|
37
|
+
run: bundle exec rubocop
|
data/.rspec
CHANGED
data/.rubocop.yml
CHANGED
@@ -67,4 +67,22 @@ Style/NumericPredicate:
|
|
67
67
|
Style/EachWithObject:
|
68
68
|
Enabled: false
|
69
69
|
|
70
|
+
RSpec/MultipleMemoizedHelpers:
|
71
|
+
Enabled: false
|
72
|
+
|
73
|
+
RSpec/FilePath:
|
74
|
+
Enabled: false
|
75
|
+
|
76
|
+
RSpec/DescribeMethod:
|
77
|
+
Enabled: false
|
78
|
+
|
79
|
+
RSpec/DescribeClass:
|
80
|
+
Enabled: false
|
81
|
+
|
82
|
+
Metrics/ParameterLists:
|
83
|
+
CountKeywordArgs: false
|
84
|
+
|
85
|
+
Metrics/AbcSize:
|
86
|
+
Max: 20
|
87
|
+
|
70
88
|
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
helium-console (0.1.
|
4
|
+
helium-console (0.1.13)
|
5
5
|
colorize
|
6
6
|
pry
|
7
7
|
|
@@ -13,6 +13,7 @@ GEM
|
|
13
13
|
coderay (1.1.3)
|
14
14
|
colorize (0.8.1)
|
15
15
|
diff-lcs (1.4.4)
|
16
|
+
docile (1.4.0)
|
16
17
|
ffaker (2.18.0)
|
17
18
|
method_source (1.0.0)
|
18
19
|
parallel (1.20.1)
|
@@ -55,6 +56,12 @@ GEM
|
|
55
56
|
rubocop (~> 1.0)
|
56
57
|
rubocop-ast (>= 1.1.0)
|
57
58
|
ruby-progressbar (1.11.0)
|
59
|
+
simplecov (0.21.2)
|
60
|
+
docile (~> 1.1)
|
61
|
+
simplecov-html (~> 0.11)
|
62
|
+
simplecov_json_formatter (~> 0.1)
|
63
|
+
simplecov-html (0.12.3)
|
64
|
+
simplecov_json_formatter (0.1.3)
|
58
65
|
unicode-display_width (2.0.0)
|
59
66
|
|
60
67
|
PLATFORMS
|
@@ -69,6 +76,7 @@ DEPENDENCIES
|
|
69
76
|
rubocop
|
70
77
|
rubocop-rake
|
71
78
|
rubocop-rspec
|
79
|
+
simplecov
|
72
80
|
|
73
81
|
BUNDLED WITH
|
74
82
|
2.1.4
|
@@ -14,6 +14,18 @@ module Helium
|
|
14
14
|
|
15
15
|
super
|
16
16
|
end
|
17
|
+
|
18
|
+
module Helpers
|
19
|
+
ColorizedString.colors.each do |color|
|
20
|
+
define_method(color) do |string|
|
21
|
+
ColorizedString.new(string).colorize(color)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def length_of(string)
|
26
|
+
ColorizedString.new(string).length
|
27
|
+
end
|
28
|
+
end
|
17
29
|
end
|
18
30
|
end
|
19
|
-
end
|
31
|
+
end
|
@@ -38,7 +38,8 @@ module Helium
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def render
|
41
|
-
|
41
|
+
style, options = optimal_format
|
42
|
+
format(object, style, **options)
|
42
43
|
end
|
43
44
|
|
44
45
|
def render_full
|
@@ -91,6 +92,16 @@ module Helium
|
|
91
92
|
@options.key?(name) || super
|
92
93
|
end
|
93
94
|
|
95
|
+
def build_key_value(&block)
|
96
|
+
key_value = KeyValue.new
|
97
|
+
block.(key_value)
|
98
|
+
key_value
|
99
|
+
end
|
100
|
+
|
101
|
+
def format_key_value(key_value, **options)
|
102
|
+
key_value.as_table(level: level, console: @console, max_width: max_width, **options)
|
103
|
+
end
|
104
|
+
|
94
105
|
private
|
95
106
|
|
96
107
|
def nested_objects
|
@@ -113,4 +124,4 @@ module Helium
|
|
113
124
|
end
|
114
125
|
end
|
115
126
|
end
|
116
|
-
end
|
127
|
+
end
|
@@ -0,0 +1,130 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Helium
|
4
|
+
class Console
|
5
|
+
class KeyValue
|
6
|
+
def row(key, value, format_key: {}, format_value: {})
|
7
|
+
rows << [key, value, { format_key: format_key, format_value: format_value }]
|
8
|
+
end
|
9
|
+
|
10
|
+
def rows
|
11
|
+
@rows ||= []
|
12
|
+
end
|
13
|
+
|
14
|
+
def as_table(console:, max_width:, level:, left: '|', between: ' ', right: '', format_keys: {}, format_values: {})
|
15
|
+
TableFormatter.(
|
16
|
+
self,
|
17
|
+
console: console,
|
18
|
+
left: left,
|
19
|
+
between: between,
|
20
|
+
right: right,
|
21
|
+
key_options: format_keys,
|
22
|
+
value_options: format_values,
|
23
|
+
max_width: max_width,
|
24
|
+
level: level
|
25
|
+
)
|
26
|
+
end
|
27
|
+
|
28
|
+
class TableFormatter
|
29
|
+
include ColorizedString::Helpers
|
30
|
+
|
31
|
+
def self.call(*args, **options)
|
32
|
+
new(*args, **options).()
|
33
|
+
end
|
34
|
+
|
35
|
+
def initialize(
|
36
|
+
key_value,
|
37
|
+
console:,
|
38
|
+
level:,
|
39
|
+
left:,
|
40
|
+
between:,
|
41
|
+
right:,
|
42
|
+
max_width:,
|
43
|
+
key_options: {},
|
44
|
+
value_options: {}
|
45
|
+
)
|
46
|
+
@key_value = key_value
|
47
|
+
@console = console
|
48
|
+
@left = left
|
49
|
+
@between = between
|
50
|
+
@right = right
|
51
|
+
@max_width = max_width
|
52
|
+
@key_options = key_options
|
53
|
+
@level = level
|
54
|
+
@value_options = value_options
|
55
|
+
|
56
|
+
set_width_limits
|
57
|
+
end
|
58
|
+
|
59
|
+
def call
|
60
|
+
Formatter::LazyStringEvaluator.new do |y|
|
61
|
+
key_value.rows.each do |key, value, options|
|
62
|
+
format_entry(y, key, value, **options)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
attr_reader :key_value, :console, :left, :between, :right, :key_options, :value_options, :max_width
|
70
|
+
|
71
|
+
def format_entry(yielder, key, value, format_key:, format_value:)
|
72
|
+
key = format(key, merge_options(key_options, format_key))
|
73
|
+
value = format(value, merge_options(value_options, format_value))
|
74
|
+
zip_lines(key, value) do |key_line, value_line, index|
|
75
|
+
yielder << [
|
76
|
+
left,
|
77
|
+
rjust(key_line, key_width),
|
78
|
+
index.zero? ? between : ' ' * length_of(between),
|
79
|
+
value_line,
|
80
|
+
right
|
81
|
+
].join
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def key_width
|
86
|
+
@key_width ||= key_value.rows.first(100).map do |key, _, format_key:, **|
|
87
|
+
length_of(format(key, merge_options(key_options, format_key)))
|
88
|
+
end.max
|
89
|
+
end
|
90
|
+
|
91
|
+
def format(object, options = {})
|
92
|
+
return object unless options
|
93
|
+
|
94
|
+
console.format(object, options.delete(:style), level: @level + 1, **options)
|
95
|
+
end
|
96
|
+
|
97
|
+
def merge_options(opt1, opt2)
|
98
|
+
return false unless opt1 && opt2
|
99
|
+
|
100
|
+
opt1.merge(opt2)
|
101
|
+
end
|
102
|
+
|
103
|
+
def set_default(hash, key, &block)
|
104
|
+
hash[key] = block.() if hash && !hash[key]
|
105
|
+
end
|
106
|
+
|
107
|
+
def zip_lines(str1, str2)
|
108
|
+
str1 = (str1.lines.each + [nil].cycle).lazy
|
109
|
+
str2 = (str2.lines.each + [nil].cycle).lazy
|
110
|
+
str1.zip(str2).with_index.each do |(line1, line2), index|
|
111
|
+
break unless line1 || line2
|
112
|
+
|
113
|
+
yield [line1 || '', line2 || '', index]
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def set_width_limits
|
118
|
+
set_default(key_options, :max_width) { max_width / 3 }
|
119
|
+
set_default(value_options, :max_width) do
|
120
|
+
max_width - key_width - length_of(left) - length_of(between) - length_of(right)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def rjust(string, length)
|
125
|
+
' ' * (length - length_of(string)) + string
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
@@ -12,11 +12,7 @@ module Helium
|
|
12
12
|
|
13
13
|
def pp(object)
|
14
14
|
formatted = Helium::Console.format(object, max_width: maxwidth)
|
15
|
-
|
16
|
-
byebug if !formatted
|
17
|
-
start_new_line ||= formatted.lines.count > 1
|
18
|
-
start_new_line ||= length_of(formatted.chomp) >= maxwidth - 2
|
19
|
-
output << "\n" if start_new_line
|
15
|
+
output << "\n" if multiline?(formatted)
|
20
16
|
|
21
17
|
formatted.lines.each.with_index do |line, index|
|
22
18
|
output << "\n" unless index.zero?
|
@@ -24,9 +20,15 @@ module Helium
|
|
24
20
|
end
|
25
21
|
end
|
26
22
|
|
23
|
+
def multiline?(formatted)
|
24
|
+
result = formatted.is_a?(Formatter::LazyStringEvaluator)
|
25
|
+
result ||= formatted.lines.count > 1
|
26
|
+
result || length_of(formatted.chomp) >= maxwidth - 2
|
27
|
+
end
|
28
|
+
|
27
29
|
def length_of(str)
|
28
30
|
ColorizedString.new(str).uncolorize.length
|
29
31
|
end
|
30
32
|
end
|
31
33
|
end
|
32
|
-
end
|
34
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Helium
|
2
4
|
class Console
|
3
5
|
class Prompt
|
@@ -28,7 +30,7 @@ module Helium
|
|
28
30
|
"#{str}> "
|
29
31
|
end
|
30
32
|
|
31
|
-
def wait_prompt(context,
|
33
|
+
def wait_prompt(context, _nesting, _pry)
|
32
34
|
@line += 1
|
33
35
|
str = [
|
34
36
|
ColorizedString.new("[#{@line}]").light_black,
|
@@ -39,4 +41,4 @@ module Helium
|
|
39
41
|
end
|
40
42
|
end
|
41
43
|
end
|
42
|
-
end
|
44
|
+
end
|
@@ -27,14 +27,15 @@ module Helium
|
|
27
27
|
private
|
28
28
|
|
29
29
|
def format_as_table
|
30
|
-
|
31
|
-
|
32
|
-
|
30
|
+
key_value = build_key_value do |kv|
|
31
|
+
object.each.with_index do |element, index|
|
32
|
+
kv.row(light_black("[#{index}]:"), element)
|
33
|
+
end
|
33
34
|
end
|
34
35
|
|
35
36
|
yield_lines do |y|
|
36
37
|
y << '['
|
37
|
-
|
38
|
+
format_key_value(key_value, left: ' ', format_keys: false).lines.each { |line| y << line }
|
38
39
|
y << ']'
|
39
40
|
end
|
40
41
|
end
|
@@ -4,7 +4,7 @@ module Helium
|
|
4
4
|
class Console
|
5
5
|
define_formatter_for Hash do
|
6
6
|
def render_compact
|
7
|
-
return
|
7
|
+
return '{}' unless object.any?
|
8
8
|
|
9
9
|
"##{format object.class, :compact}#{light_black "[#{object.size}]"}"
|
10
10
|
end
|
@@ -19,15 +19,19 @@ module Helium
|
|
19
19
|
private
|
20
20
|
|
21
21
|
def format_as_table
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
key_value = build_key_value do |kv|
|
23
|
+
object.each do |key, value|
|
24
|
+
key = light_blue(key.to_s) if all_symbol?
|
25
|
+
kv.row(key, value)
|
26
|
+
end
|
26
27
|
end
|
27
28
|
|
28
29
|
yield_lines do |y|
|
29
30
|
y << '{'
|
30
|
-
|
31
|
+
format_key_value(key_value, left: ' ', between: after_key,
|
32
|
+
format_keys: all_symbol? ? false : {}).lines.each do |line|
|
33
|
+
y << line
|
34
|
+
end
|
31
35
|
y << '}'
|
32
36
|
end
|
33
37
|
end
|
@@ -70,7 +74,6 @@ module Helium
|
|
70
74
|
end
|
71
75
|
|
72
76
|
def all_symbol?
|
73
|
-
return false
|
74
77
|
return @all_symbol if defined?(@all_symbol)
|
75
78
|
|
76
79
|
@all_symbol = object.keys.all? { |key| key.is_a? Symbol }
|
@@ -17,31 +17,42 @@ module Helium
|
|
17
17
|
|
18
18
|
private
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
20
|
+
# TODO: he method below breaks ABCSize, most likely due to an amount of colouring method calls.
|
21
|
+
# This should be simplified by altering table handling: https://github.com/helium-rb/console/issues/10
|
22
|
+
def as_table(source:) # rubocop:disable Metrics/AbcSize
|
23
|
+
key_value = build_key_value do |kv|
|
24
|
+
kv.row(magenta('receiver'), object.receiver)
|
25
|
+
kv.row(magenta('location'), object.source_location&.join(':'))
|
26
|
+
kv.row(magenta('source'), trimmed_source, format_value: false) if source && object.source_location
|
27
|
+
end
|
25
28
|
|
26
29
|
yield_lines do |y|
|
27
30
|
y << "#{light_black '#'} #{render_compact}"
|
28
|
-
|
31
|
+
format_key_value(key_value, left: light_black('| '), format_keys: false, between: light_black(': '))
|
32
|
+
.lines.each { |line| y << line }
|
29
33
|
end
|
30
34
|
end
|
31
35
|
|
32
36
|
def title
|
33
|
-
|
34
|
-
|
37
|
+
formatted_owner = format(owner, :compact)
|
38
|
+
formatted_owner = "(#{formatted_owner})" unless owner.is_a? Module
|
35
39
|
|
36
|
-
|
37
|
-
|
38
|
-
owner = owner.ancestors.find { |ancestor| ancestor.singleton_class == object.owner } if owner.is_a? Class
|
39
|
-
end
|
40
|
+
"#{formatted_owner}#{yellow(singleton_owner? ? '.' : '#')}#{yellow object.name.to_s}"
|
41
|
+
end
|
40
42
|
|
41
|
-
|
42
|
-
|
43
|
+
def owner
|
44
|
+
return @owner if defined?(@owner)
|
45
|
+
|
46
|
+
@owner = object.owner
|
47
|
+
if singleton_owner?
|
48
|
+
@owner = object.receiver
|
49
|
+
@owner = owner.ancestors.find { |ancestor| ancestor.singleton_class == object.owner } if owner.is_a? Class
|
50
|
+
end
|
51
|
+
@owner
|
52
|
+
end
|
43
53
|
|
44
|
-
|
54
|
+
def singleton_owner?
|
55
|
+
object.owner.singleton_class?
|
45
56
|
end
|
46
57
|
|
47
58
|
def trimmed_source
|
@@ -27,22 +27,24 @@ module Helium
|
|
27
27
|
[class_name, formatted_vars].compact.join
|
28
28
|
end
|
29
29
|
|
30
|
-
|
31
30
|
def render_partial
|
32
|
-
table = Table.new(runner: light_black('| '), after_key: light_black(': '), format_keys: false)
|
33
|
-
|
34
|
-
object.instance_variables.each do |inst|
|
35
|
-
table.row(magenta(inst.to_s), object.instance_variable_get(inst))
|
36
|
-
end
|
37
|
-
|
38
31
|
yield_lines do |y|
|
39
32
|
y << "#{light_black '#'} #{class_name}"
|
40
|
-
|
33
|
+
as_table.lines.each { |line| y << line }
|
41
34
|
end
|
42
35
|
end
|
43
36
|
|
44
37
|
private
|
45
38
|
|
39
|
+
def as_table
|
40
|
+
key_value = build_key_value do |kv|
|
41
|
+
object.instance_variables.each do |inst|
|
42
|
+
kv.row(magenta(inst.to_s), object.instance_variable_get(inst))
|
43
|
+
end
|
44
|
+
end
|
45
|
+
format_key_value(key_value, left: light_black('| '), between: light_black(': '), format_keys: false)
|
46
|
+
end
|
47
|
+
|
46
48
|
def formatted_instance_variables(**options)
|
47
49
|
object.instance_variables.sort.each.lazy.map do |var_name|
|
48
50
|
value = object.instance_variable_get(var_name)
|
@@ -2,15 +2,15 @@
|
|
2
2
|
|
3
3
|
module Helium
|
4
4
|
class Console
|
5
|
-
define_formatter_for Set do
|
5
|
+
define_formatter_for 'Set' do
|
6
6
|
def render_compact
|
7
|
-
return
|
7
|
+
return render_empty if object.none?
|
8
8
|
|
9
9
|
"#{light_magenta('Set:')} #{trunc_message(object.count, all_truncated: true)}"
|
10
10
|
end
|
11
11
|
|
12
12
|
def render_partial
|
13
|
-
return
|
13
|
+
return render_empty if object.none?
|
14
14
|
return inline_with_truncation if options[:max_lines] == 1
|
15
15
|
|
16
16
|
inline_without_truncation || format_as_table
|
@@ -23,7 +23,7 @@ module Helium
|
|
23
23
|
private
|
24
24
|
|
25
25
|
def render_empty
|
26
|
-
"#{light_magenta('Set:')} #{red 'empty'}"
|
26
|
+
"#{light_magenta('Set:')} #{red 'empty'}"
|
27
27
|
end
|
28
28
|
|
29
29
|
def format_as_table
|
@@ -69,7 +69,11 @@ module Helium
|
|
69
69
|
end
|
70
70
|
|
71
71
|
def formatted_elements(**options)
|
72
|
-
sorted =
|
72
|
+
sorted = begin
|
73
|
+
object.sort
|
74
|
+
rescue StandardError
|
75
|
+
object
|
76
|
+
end
|
73
77
|
sorted.each.lazy.map { |element| format_nested(element, **options) }
|
74
78
|
end
|
75
79
|
|
data/lib/helium/console.rb
CHANGED
@@ -6,7 +6,8 @@ require 'helium/console/version'
|
|
6
6
|
require 'helium/console/formatters/indent'
|
7
7
|
require 'helium/console/formatters/overflow'
|
8
8
|
require 'helium/console/formatters/max_lines'
|
9
|
-
require 'helium/console/
|
9
|
+
require 'helium/console/colorized_string'
|
10
|
+
require 'helium/console/key_value'
|
10
11
|
require 'helium/console/registry'
|
11
12
|
|
12
13
|
require 'helium/console/printer'
|
@@ -25,9 +26,6 @@ module Helium
|
|
25
26
|
].freeze
|
26
27
|
|
27
28
|
def self.start(target = nil, options = {})
|
28
|
-
prompt = ColorizedString.new("He\u269B").light_blue
|
29
|
-
line = 0
|
30
|
-
|
31
29
|
options = {
|
32
30
|
print: ColorPrinter.method(:default),
|
33
31
|
prompt: Prompt.new.pry_prompt
|
@@ -44,16 +42,15 @@ module Helium
|
|
44
42
|
Registry.class_formatters.define(klass, &handler)
|
45
43
|
end
|
46
44
|
|
47
|
-
|
48
45
|
class << self
|
49
46
|
def instance
|
50
47
|
@instance ||= new
|
51
48
|
end
|
52
49
|
|
53
|
-
def method_missing(name, *args, &block)
|
50
|
+
def method_missing(name, *args, **keywords, &block)
|
54
51
|
super unless instance.respond_to?(name)
|
55
52
|
|
56
|
-
instance.public_send(name, *args, &block)
|
53
|
+
instance.public_send(name, *args, **keywords, &block)
|
57
54
|
end
|
58
55
|
|
59
56
|
def respond_to_missing?(name, private = false)
|
@@ -61,6 +58,11 @@ module Helium
|
|
61
58
|
end
|
62
59
|
end
|
63
60
|
|
61
|
+
def initialize(instance_registry: nil, class_registry: nil)
|
62
|
+
@instance_registry = instance_registry || Registry.instance_formatters
|
63
|
+
@class_registry = class_registry || Registry.class_formatters
|
64
|
+
end
|
65
|
+
|
64
66
|
def format(object, style = nil, **options)
|
65
67
|
options = default_options.merge(options)
|
66
68
|
return '(...)' if options[:ignore_objects].include?(object.object_id)
|
@@ -85,7 +87,9 @@ module Helium
|
|
85
87
|
|
86
88
|
def format_string(string, ellipses: '...', **options)
|
87
89
|
options = default_options.merge(options)
|
90
|
+
max_width_without_indent = (options[:max_width] - options[:indent]) if options[:max_width]
|
88
91
|
formatters = [
|
92
|
+
Formatters::Overflow.get(options[:overflow]).new(max_width: max_width_without_indent),
|
89
93
|
Formatters::Indent.new(options[:indent]),
|
90
94
|
Formatters::MaxLines.new(
|
91
95
|
max_lines: options[:max_lines],
|
@@ -93,11 +97,6 @@ module Helium
|
|
93
97
|
ellipses: ellipses
|
94
98
|
)
|
95
99
|
]
|
96
|
-
if options[:max_width]
|
97
|
-
formatters.unshift(
|
98
|
-
Formatters::Overflow.get(options[:overflow]).new(max_width: options[:max_width] - options[:indent]),
|
99
|
-
)
|
100
|
-
end
|
101
100
|
|
102
101
|
formatters.inject(string) do |str, formatter|
|
103
102
|
formatter.(str)
|
@@ -106,13 +105,15 @@ module Helium
|
|
106
105
|
|
107
106
|
private
|
108
107
|
|
109
|
-
|
108
|
+
attr_reader :instance_registry, :class_registry
|
109
|
+
|
110
110
|
def handler_for(object, style, **options)
|
111
111
|
formatter_class = if object.is_a? Module
|
112
|
-
|
112
|
+
class_registry.handler_for(object)
|
113
113
|
else
|
114
|
-
|
114
|
+
instance_registry.handler_for(object.class)
|
115
115
|
end
|
116
|
+
|
116
117
|
formatter_class&.new(object, style, self, **options)
|
117
118
|
end
|
118
119
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: helium-console
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.13
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stanislaw Klajn
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-02-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: colorize
|
@@ -45,6 +45,7 @@ executables: []
|
|
45
45
|
extensions: []
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
|
+
- ".github/workflows/pull_request.yml"
|
48
49
|
- ".gitignore"
|
49
50
|
- ".rspec"
|
50
51
|
- ".rubocop.yml"
|
@@ -65,6 +66,7 @@ files:
|
|
65
66
|
- lib/helium/console/formatters/max_lines.rb
|
66
67
|
- lib/helium/console/formatters/overflow.rb
|
67
68
|
- lib/helium/console/formatters/overflow/wrap.rb
|
69
|
+
- lib/helium/console/key_value.rb
|
68
70
|
- lib/helium/console/printer.rb
|
69
71
|
- lib/helium/console/prompt.rb
|
70
72
|
- lib/helium/console/registry.rb
|
@@ -81,9 +83,7 @@ files:
|
|
81
83
|
- lib/helium/console/registry/set.rb
|
82
84
|
- lib/helium/console/registry/string.rb
|
83
85
|
- lib/helium/console/registry/symbol.rb
|
84
|
-
- lib/helium/console/registry/table.rb
|
85
86
|
- lib/helium/console/registry/time.rb
|
86
|
-
- lib/helium/console/table.rb
|
87
87
|
- lib/helium/console/version.rb
|
88
88
|
homepage: https://github.com/helium-rb/console
|
89
89
|
licenses:
|
@@ -106,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
106
106
|
- !ruby/object:Gem::Version
|
107
107
|
version: '0'
|
108
108
|
requirements: []
|
109
|
-
rubygems_version: 3.
|
109
|
+
rubygems_version: 3.1.4
|
110
110
|
signing_key:
|
111
111
|
specification_version: 4
|
112
112
|
summary: Collection of tools for smooth integration with console
|
@@ -1,81 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Helium
|
4
|
-
class Console
|
5
|
-
define_formatter_for Table do
|
6
|
-
def call
|
7
|
-
yield_lines do |y|
|
8
|
-
rows.each do |key, value, style, options = {}|
|
9
|
-
format_pair(key, value, style, **options) do |line|
|
10
|
-
y << line
|
11
|
-
end
|
12
|
-
end
|
13
|
-
y << truncation if truncation
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
private
|
18
|
-
|
19
|
-
def format_pair(key, value, style, **options)
|
20
|
-
formatted_value = format_nested(value, style, max_width: max_value_width, **options)
|
21
|
-
|
22
|
-
formatted_value.lines.each.with_index.map do |line, index|
|
23
|
-
yield [
|
24
|
-
object.runner,
|
25
|
-
text_or_blank(rjust(format_key(key), key_width), blank: index > 0),
|
26
|
-
text_or_blank(object.after_key, blank: index > 0),
|
27
|
-
line.chomp
|
28
|
-
].join
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
def text_or_blank(text, blank:)
|
33
|
-
return text unless blank
|
34
|
-
|
35
|
-
' ' * length_of(text)
|
36
|
-
end
|
37
|
-
|
38
|
-
def key_width
|
39
|
-
@key_width ||= rows
|
40
|
-
.map(&:first)
|
41
|
-
.map(&method(:format_key))
|
42
|
-
.map(&method(:length_of))
|
43
|
-
.max
|
44
|
-
end
|
45
|
-
|
46
|
-
def max_value_width
|
47
|
-
max_width - length_of(object.runner) - key_width - length_of(object.after_key)
|
48
|
-
end
|
49
|
-
|
50
|
-
def format_key(key)
|
51
|
-
object.format_keys ? format(key, max_width: 15, level: 3) : key
|
52
|
-
end
|
53
|
-
|
54
|
-
def rjust(string, width)
|
55
|
-
' ' * (width - length_of(string)) + string
|
56
|
-
end
|
57
|
-
|
58
|
-
def rows
|
59
|
-
@rows ||= case level
|
60
|
-
when 1 then object.rows
|
61
|
-
when 2 then rows_limited_by(10)
|
62
|
-
else rows_limited_by(3)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
def rows_limited_by(number)
|
67
|
-
object.rows.count <= number ? object.rows : object.rows.first(number - 1)
|
68
|
-
end
|
69
|
-
|
70
|
-
def truncation
|
71
|
-
return unless object.rows.length > rows.length
|
72
|
-
|
73
|
-
[
|
74
|
-
object.runner,
|
75
|
-
light_black("(#{object.rows.length - rows.length} more)"),
|
76
|
-
"\n"
|
77
|
-
].join
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
data/lib/helium/console/table.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Helium
|
4
|
-
class Console
|
5
|
-
class Table
|
6
|
-
def initialize(runner: '|', after_key: ' ', format_keys: true)
|
7
|
-
@runner = runner
|
8
|
-
@after_key = after_key
|
9
|
-
@format_keys = format_keys
|
10
|
-
end
|
11
|
-
|
12
|
-
attr_reader :runner, :after_key, :format_keys
|
13
|
-
|
14
|
-
def row(key, value, style = nil, **options)
|
15
|
-
rows << [key, value, style, options]
|
16
|
-
end
|
17
|
-
|
18
|
-
def rows
|
19
|
-
@rows ||= []
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|