helium-console 0.1.7 → 0.1.11
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/.rubocop.yml +70 -0
- data/Gemfile +10 -5
- data/Gemfile.lock +29 -1
- data/Rakefile +5 -3
- data/bin/console +3 -8
- data/helium-console.gemspec +16 -14
- data/lib/helium/console/formatters/indent.rb +3 -1
- data/lib/helium/console/formatters/max_lines.rb +4 -2
- data/lib/helium/console/formatters/overflow/wrap.rb +4 -2
- data/lib/helium/console/formatters/overflow.rb +3 -2
- data/lib/helium/console/printer.rb +8 -3
- data/lib/helium/console/registry/array.rb +42 -37
- data/lib/helium/console/registry/booleans.rb +2 -0
- data/lib/helium/console/registry/date.rb +11 -0
- data/lib/helium/console/registry/hash.rb +54 -43
- data/lib/helium/console/registry/module.rb +12 -4
- data/lib/helium/console/registry/nil.rb +2 -0
- data/lib/helium/console/registry/numeric.rb +2 -0
- data/lib/helium/console/registry/object.rb +30 -38
- data/lib/helium/console/registry/pathname.rb +11 -0
- data/lib/helium/console/registry/set.rb +80 -0
- data/lib/helium/console/registry/string.rb +6 -4
- data/lib/helium/console/registry/symbol.rb +3 -1
- data/lib/helium/console/registry/table.rb +42 -36
- data/lib/helium/console/registry/time.rb +11 -0
- data/lib/helium/console/registry.rb +47 -14
- data/lib/helium/console/table.rb +3 -2
- data/lib/helium/console/version.rb +3 -1
- data/lib/helium/console.rb +25 -19
- metadata +11 -6
@@ -1,80 +1,91 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Helium
|
2
4
|
class Console
|
3
5
|
define_formatter_for Hash do
|
4
6
|
def call
|
5
|
-
return
|
6
|
-
|
7
|
+
return '{}' if object.none?
|
8
|
+
return inline_with_truncation if force_inline?
|
9
|
+
|
10
|
+
inline_without_truncation || format_as_table
|
7
11
|
end
|
8
12
|
|
9
|
-
|
13
|
+
private
|
10
14
|
|
11
15
|
def format_as_table
|
12
|
-
table = Table.new(runner: ' ', after_key: after_key, format_keys:
|
16
|
+
table = Table.new(runner: ' ', after_key: after_key, format_keys: true)
|
13
17
|
object.each do |key, value|
|
14
18
|
key = light_blue(key.to_s) if all_symbol?
|
15
19
|
table.row(key, value)
|
16
20
|
end
|
17
21
|
|
18
|
-
|
19
|
-
|
20
|
-
format(table
|
21
|
-
|
22
|
-
|
22
|
+
yield_lines do |y|
|
23
|
+
y << '{'
|
24
|
+
format(table).lines.each { |line| y << line }
|
25
|
+
y << '}'
|
26
|
+
end
|
23
27
|
end
|
24
28
|
|
25
|
-
def
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
+
def inline_with_truncation
|
30
|
+
truncated = formatted_inline_elements.with_index.inject([]) do |collected, (formatted, index)|
|
31
|
+
new_collected = [*collected[0..-2], formatted, trunc_text(index + 1)].compact
|
32
|
+
break collected if new_collected.join(', ').length > max_width - 4
|
29
33
|
|
30
|
-
|
31
|
-
|
32
|
-
formatted_value = format(value, max_lines: 1, nesting: 1, max_width: 15)
|
33
|
-
|
34
|
-
formatted = "#{formatted_key} => #{formatted_value}"
|
34
|
+
new_collected
|
35
|
+
end
|
35
36
|
|
36
|
-
|
37
|
-
|
37
|
+
['{ ', truncated.join(', '), ' }'].compact.join
|
38
|
+
end
|
38
39
|
|
39
|
-
|
40
|
+
def inline_without_truncation
|
41
|
+
formatted = formatted_inline_elements.inject([]) do |collected, element|
|
42
|
+
collected << element
|
43
|
+
break if collected.join(', ').length > max_width - 4
|
40
44
|
|
41
|
-
|
42
|
-
trunc = new_trunc
|
45
|
+
collected
|
43
46
|
end
|
44
47
|
|
45
|
-
|
46
|
-
|
48
|
+
return if formatted.nil?
|
49
|
+
|
50
|
+
['{ ', formatted.join(', '), ' }'].compact.join
|
51
|
+
end
|
52
|
+
|
53
|
+
def force_inline?
|
54
|
+
level > 2
|
47
55
|
end
|
48
56
|
|
49
|
-
def
|
50
|
-
|
51
|
-
|
57
|
+
def formatted_inline_elements
|
58
|
+
object.each.lazy.map do |key, value|
|
59
|
+
formatted_key = format_key(key, max_lines: 1, nesting: 1, max_with: 15)
|
60
|
+
formatted_value = format_nested(value, max_lines: 1, nesting: 1, max_width: 15)
|
61
|
+
[formatted_key, after_key, formatted_value].join
|
62
|
+
end
|
63
|
+
end
|
52
64
|
|
53
|
-
|
54
|
-
|
65
|
+
def all_symbol?
|
66
|
+
return false
|
67
|
+
return @all_symbol if defined?(@all_symbol)
|
55
68
|
|
56
|
-
|
57
|
-
|
58
|
-
formatted = "#{formatted_key} => #{formatted_value}"
|
69
|
+
@all_symbol = object.keys.all? { |key| key.is_a? Symbol }
|
70
|
+
end
|
59
71
|
|
60
|
-
|
72
|
+
def format_key(key, **options)
|
73
|
+
return light_blue(key.to_s) if all_symbol?
|
61
74
|
|
62
|
-
|
63
|
-
end
|
64
|
-
joined = " #{joined} " if joined
|
65
|
-
["{", joined, "}"].compact.join
|
75
|
+
format_nested(key, **options)
|
66
76
|
end
|
67
77
|
|
68
|
-
def
|
69
|
-
|
78
|
+
def total_elements
|
79
|
+
@total_elements ||= object.length
|
70
80
|
end
|
71
81
|
|
72
|
-
def
|
73
|
-
|
82
|
+
def trunc_text(count)
|
83
|
+
truncated_elements = total_elements - count
|
84
|
+
light_black("(#{truncated_elements} #{count.zero? ? 'elements' : 'more'})")
|
74
85
|
end
|
75
86
|
|
76
87
|
def after_key
|
77
|
-
all_symbol? ? light_blue(
|
88
|
+
all_symbol? ? light_blue(': ') : light_black(' => ')
|
78
89
|
end
|
79
90
|
end
|
80
91
|
end
|
@@ -1,15 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Helium
|
2
4
|
class Console
|
3
5
|
define_formatter_for Module do
|
4
6
|
def call
|
5
|
-
light_yellow(object.name ||
|
7
|
+
light_yellow(object.name || anonymous_text)
|
6
8
|
end
|
7
9
|
|
8
10
|
private
|
9
11
|
|
10
|
-
def
|
11
|
-
closest = object.ancestors.find(&:name)
|
12
|
-
|
12
|
+
def anonymous_text
|
13
|
+
closest = (object.ancestors.grep(Class) - [Object, BasicObject]).find(&:name)&.name
|
14
|
+
|
15
|
+
signature = if closest
|
16
|
+
"subclass of #{closest}"
|
17
|
+
else
|
18
|
+
object.class.name.downcase
|
19
|
+
end
|
20
|
+
"(anonymous #{signature})"
|
13
21
|
end
|
14
22
|
end
|
15
23
|
end
|
@@ -1,61 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Helium
|
2
4
|
class Console
|
3
5
|
define_formatter_for Object do
|
4
6
|
def call
|
5
|
-
return
|
7
|
+
return inline_with_truncation if force_inline?
|
8
|
+
|
6
9
|
format_as_table
|
7
10
|
end
|
8
11
|
|
9
|
-
|
12
|
+
private
|
10
13
|
|
11
14
|
def format_as_table
|
12
|
-
table = Table.new(runner: light_black('| '), after_key: light_black(
|
15
|
+
table = Table.new(runner: light_black('| '), after_key: light_black(': '), format_keys: false)
|
13
16
|
|
14
17
|
object.instance_variables.each do |inst|
|
15
18
|
table.row(magenta(inst.to_s), object.instance_variable_get(inst))
|
16
19
|
end
|
17
20
|
|
18
|
-
|
19
|
-
|
20
|
-
format(table)
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
def format_inline_with_truncation
|
25
|
-
"#{object.class.name}##{object.object_id.to_s(16)}"
|
21
|
+
yield_lines do |y|
|
22
|
+
y << "#{light_black '#'} #{class_name}"
|
23
|
+
format(table).lines.each {|line| y << line }
|
24
|
+
end
|
26
25
|
end
|
27
26
|
|
28
|
-
def
|
29
|
-
|
30
|
-
|
31
|
-
object.each do |key, value|
|
32
|
-
return unless simple?(value)
|
27
|
+
def inline_with_truncation
|
28
|
+
class_name = class_name(short: true)
|
33
29
|
|
34
|
-
|
35
|
-
|
36
|
-
|
30
|
+
vars = formatted_instance_variables(max_width: 15, max_lines: 1).inject([]) do |collected, element|
|
31
|
+
new_collected = [*collected, element]
|
32
|
+
break collected if new_collected.join(', ').length > max_width - length_of(class_name) - 2
|
37
33
|
|
38
|
-
|
39
|
-
|
40
|
-
return if joined.length > max_width - 4
|
34
|
+
new_collected
|
41
35
|
end
|
42
|
-
joined = " #{joined} " if joined
|
43
|
-
["{", joined, "}"].compact.join
|
44
|
-
end
|
45
36
|
|
46
|
-
|
47
|
-
|
48
|
-
when Class then :class
|
49
|
-
when Module then :module
|
50
|
-
else :instance
|
51
|
-
end
|
52
|
-
klass = type == :instance ? object.class : object
|
53
|
-
klass_name = klass.name
|
54
|
-
if !klass_name
|
55
|
-
named_ancestor = klass.ancestors.find(&:name)
|
56
|
-
klass_name = ['anonymous', named_ancestor&.name].compact.join(" ")
|
57
|
-
end
|
58
|
-
"#{light_black('#')} #{light_yellow(klass_name)} #{type}"
|
37
|
+
formatted_vars = "( #{vars.join(', ')} )" if vars.any?
|
38
|
+
[class_name, formatted_vars].compact.join
|
59
39
|
end
|
60
40
|
|
61
41
|
def force_inline?
|
@@ -65,6 +45,18 @@ module Helium
|
|
65
45
|
def all_symbol?
|
66
46
|
object.keys.all? { |key| key.is_a? Symbol }
|
67
47
|
end
|
48
|
+
|
49
|
+
def formatted_instance_variables(**options)
|
50
|
+
object.instance_variables.sort.each.lazy.map do |var_name|
|
51
|
+
value = object.instance_variable_get(var_name)
|
52
|
+
"#{magenta(var_name.to_s)} = #{format_nested(value, **options)}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def class_name(short: false)
|
57
|
+
formatted = format(object.class, short: short)
|
58
|
+
short ? "##{formatted}" : "#{formatted} instance"
|
59
|
+
end
|
68
60
|
end
|
69
61
|
end
|
70
62
|
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Helium
|
4
|
+
class Console
|
5
|
+
define_formatter_for Set do
|
6
|
+
def call
|
7
|
+
return "#{light_magenta('Set:')} #{red 'empty'}" if object.none?
|
8
|
+
return inline_with_truncation if force_inline?
|
9
|
+
|
10
|
+
inline_without_truncation || format_as_table
|
11
|
+
end
|
12
|
+
|
13
|
+
def simple?
|
14
|
+
object.none?
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def format_as_table
|
20
|
+
table = Table.new(runner: '', format_keys: false)
|
21
|
+
object.each do |element|
|
22
|
+
table.row(light_magenta('-'), element)
|
23
|
+
end
|
24
|
+
|
25
|
+
[
|
26
|
+
light_magenta('Set: {'),
|
27
|
+
format(table),
|
28
|
+
light_magenta('}')
|
29
|
+
].join("\n")
|
30
|
+
end
|
31
|
+
|
32
|
+
def inline_with_truncation
|
33
|
+
formatted = formatted_elements.with_index.inject([]) do |joined, (element, index)|
|
34
|
+
new_joined = [*joined[0..-2], element, trunc_message(object_size - index - 1, all_truncated: index.zero?)]
|
35
|
+
break joined if too_long?(new_joined, max_width: max_width - 9)
|
36
|
+
|
37
|
+
new_joined
|
38
|
+
end
|
39
|
+
|
40
|
+
"#{light_magenta('Set: {')} #{formatted.join(light_magenta(' | '))} #{light_magenta('}')}"
|
41
|
+
end
|
42
|
+
|
43
|
+
def inline_without_truncation
|
44
|
+
return unless object.all? { |element| Helium::Console.simple? element }
|
45
|
+
|
46
|
+
formatted = formatted_elements.inject([]) do |joined, element|
|
47
|
+
joined = [*joined, element]
|
48
|
+
break if too_long?(joined, max_width: max_width - 4)
|
49
|
+
|
50
|
+
joined
|
51
|
+
end
|
52
|
+
|
53
|
+
"#{light_magenta('Set: {')} #{formatted.join(light_magenta(' | '))} #{light_magenta('}')}" unless formatted.nil?
|
54
|
+
end
|
55
|
+
|
56
|
+
def too_long?(object, max_width:)
|
57
|
+
string = object.respond_to?(:join) ? object.join(' | ') : object
|
58
|
+
length_of(string) > max_width - 4
|
59
|
+
end
|
60
|
+
|
61
|
+
def formatted_elements(**options)
|
62
|
+
object.sort.each.lazy.map { |element| format_nested(element, **options) }
|
63
|
+
end
|
64
|
+
|
65
|
+
def trunc_message(count, all_truncated: false)
|
66
|
+
return if count < 1
|
67
|
+
|
68
|
+
light_black "(#{count} #{all_truncated ? 'elements' : 'more'})"
|
69
|
+
end
|
70
|
+
|
71
|
+
def object_size
|
72
|
+
@object_size ||= object.size
|
73
|
+
end
|
74
|
+
|
75
|
+
def force_inline?
|
76
|
+
level > 2
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Helium
|
2
4
|
class Console
|
3
5
|
define_formatter_for String do
|
@@ -7,7 +9,7 @@ module Helium
|
|
7
9
|
max_width: max_width,
|
8
10
|
max_lines: max_lines,
|
9
11
|
overflow: overflow,
|
10
|
-
ellipses: "
|
12
|
+
ellipses: '..."'
|
11
13
|
)
|
12
14
|
|
13
15
|
formatted.lines
|
@@ -17,9 +19,9 @@ module Helium
|
|
17
19
|
|
18
20
|
def max_lines
|
19
21
|
case level
|
20
|
-
|
21
|
-
|
22
|
-
|
22
|
+
when 1 then nil
|
23
|
+
when 2 then 3
|
24
|
+
else 1
|
23
25
|
end
|
24
26
|
end
|
25
27
|
end
|
@@ -1,38 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Helium
|
2
4
|
class Console
|
3
5
|
define_formatter_for Table do
|
4
6
|
def call
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
7
|
+
yield_lines do |y|
|
8
|
+
rows.each do |key, value, options = {}|
|
9
|
+
format_pair(key, value, **options) do |line|
|
10
|
+
y << line
|
11
|
+
end
|
12
|
+
end
|
13
|
+
y << truncation if truncation
|
14
|
+
end
|
9
15
|
end
|
10
16
|
|
11
|
-
|
12
|
-
rows.flat_map do |key, value, options = {}|
|
13
|
-
formatted_value = format_nested(value, max_width: max_value_width, **options)
|
17
|
+
private
|
14
18
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
def format_pair(key, value, **options)
|
20
|
+
formatted_value = format_nested(value, 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
|
23
29
|
end
|
24
30
|
end
|
25
31
|
|
26
|
-
def
|
27
|
-
return unless
|
32
|
+
def text_or_blank(text, blank:)
|
33
|
+
return text unless blank
|
28
34
|
|
29
|
-
|
30
|
-
object.runner,
|
31
|
-
light_black("(#{object.rows.length - rows.length} more)")
|
32
|
-
].join
|
35
|
+
' ' * length_of(text)
|
33
36
|
end
|
34
37
|
|
35
|
-
|
36
38
|
def key_width
|
37
39
|
@key_width ||= rows
|
38
40
|
.map(&:first)
|
@@ -50,26 +52,30 @@ module Helium
|
|
50
52
|
end
|
51
53
|
|
52
54
|
def rjust(string, width)
|
53
|
-
|
54
|
-
end
|
55
|
-
|
56
|
-
def length_of(string)
|
57
|
-
if string.respond_to?(:uncolorize)
|
58
|
-
string.uncolorize.length
|
59
|
-
else
|
60
|
-
string.length
|
61
|
-
end
|
55
|
+
' ' * (width - length_of(string)) + string
|
62
56
|
end
|
63
57
|
|
64
58
|
def rows
|
65
59
|
@rows ||= case level
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
else
|
70
|
-
object.rows.count < 3 ? object.rows : object.rows.first(2)
|
60
|
+
when 1 then object.rows
|
61
|
+
when 2 then rows_limited_by(10)
|
62
|
+
else rows_limited_by(3)
|
71
63
|
end
|
72
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
|
73
79
|
end
|
74
80
|
end
|
75
81
|
end
|