helium-console 0.1.0 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/Gemfile.lock +9 -3
- data/bin/console +8 -7
- data/helium-console.gemspec +2 -1
- data/lib/helium/console.rb +89 -13
- data/lib/helium/console/formatters/indent.rb +1 -1
- data/lib/helium/console/formatters/max_lines.rb +6 -4
- data/lib/helium/console/formatters/overflow.rb +1 -1
- data/lib/helium/console/formatters/overflow/wrap.rb +1 -1
- data/lib/helium/console/printer.rb +20 -0
- data/lib/helium/console/registry.rb +74 -0
- data/lib/helium/console/registry/array.rb +75 -0
- data/lib/helium/console/registry/booleans.rb +15 -0
- data/lib/helium/console/registry/hash.rb +81 -0
- data/lib/helium/console/registry/module.rb +16 -0
- data/lib/helium/console/registry/nil.rb +9 -0
- data/lib/helium/console/registry/numeric.rb +9 -0
- data/lib/helium/console/registry/object.rb +64 -0
- data/lib/helium/console/registry/string.rb +27 -0
- data/lib/helium/console/registry/symbol.rb +9 -0
- data/lib/helium/console/registry/table.rb +75 -0
- data/lib/helium/console/table.rb +22 -0
- data/lib/helium/console/version.rb +2 -2
- metadata +30 -4
- data/.byebug_history +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6cc078f13cb540c2c2c9d14814cfe31da3b50ebdb59051918db1ac3d3fc63b51
|
4
|
+
data.tar.gz: c9fd23846bad166de9fd715c4166e60a162df08a6f820277c0815f539dac6306
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9259047186f54a53e8378d8fc0c94c35a895334acfb97950ba22511453aa4a0b855866c94d22b323c2249847cf112bcec126ef63ec0c71764fc3dce6c8038213
|
7
|
+
data.tar.gz: 475285e8aae66982bfc86cf3250fbb6e97f88eddf7e7c14228b7158fd8694981c227cc8185e9794869d1c0be27ae6a7f97b789e15269c61a575c850a64ad1263
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,15 +1,22 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
helium-console (0.1.
|
5
|
-
|
4
|
+
helium-console (0.1.5)
|
5
|
+
colorize
|
6
|
+
pry
|
6
7
|
|
7
8
|
GEM
|
8
9
|
remote: https://rubygems.org/
|
9
10
|
specs:
|
10
11
|
byebug (11.1.3)
|
12
|
+
coderay (1.1.3)
|
13
|
+
colorize (0.8.1)
|
11
14
|
diff-lcs (1.4.4)
|
12
15
|
ffaker (2.18.0)
|
16
|
+
method_source (1.0.0)
|
17
|
+
pry (0.14.1)
|
18
|
+
coderay (~> 1.1)
|
19
|
+
method_source (~> 1.0)
|
13
20
|
rake (12.3.3)
|
14
21
|
rspec (3.10.0)
|
15
22
|
rspec-core (~> 3.10.0)
|
@@ -24,7 +31,6 @@ GEM
|
|
24
31
|
diff-lcs (>= 1.2.0, < 2.0)
|
25
32
|
rspec-support (~> 3.10.0)
|
26
33
|
rspec-support (3.10.2)
|
27
|
-
ruby-terminfo (0.1.1)
|
28
34
|
|
29
35
|
PLATFORMS
|
30
36
|
ruby
|
data/bin/console
CHANGED
@@ -3,12 +3,13 @@
|
|
3
3
|
require "bundler/setup"
|
4
4
|
require "helium/console"
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
require 'byebug'
|
7
|
+
require 'ffaker'
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
class A
|
10
|
+
def initialize(a)
|
11
|
+
@a = a
|
12
|
+
end
|
13
|
+
end
|
12
14
|
|
13
|
-
|
14
|
-
IRB.start(__FILE__)
|
15
|
+
Pry.start
|
data/helium-console.gemspec
CHANGED
@@ -22,5 +22,6 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.bindir = "exe"
|
23
23
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
24
|
spec.require_paths = ["lib"]
|
25
|
-
spec.add_dependency "
|
25
|
+
spec.add_dependency "pry"
|
26
|
+
spec.add_dependency "colorize"
|
26
27
|
end
|
data/lib/helium/console.rb
CHANGED
@@ -1,26 +1,102 @@
|
|
1
|
-
require "
|
1
|
+
require "pry"
|
2
2
|
|
3
|
+
require "helium/console/version"
|
3
4
|
require "helium/console/formatters/indent"
|
4
5
|
require "helium/console/formatters/overflow"
|
5
6
|
require "helium/console/formatters/max_lines"
|
6
|
-
require "
|
7
|
+
require "helium/console/table"
|
8
|
+
require "helium/console/registry"
|
9
|
+
|
10
|
+
require "helium/console/printer"
|
7
11
|
|
8
12
|
module Helium
|
9
|
-
|
13
|
+
class Console
|
10
14
|
Error = Class.new(StandardError)
|
11
15
|
|
16
|
+
SIMPLE_OBJECTS = [
|
17
|
+
Numeric,
|
18
|
+
NilClass,
|
19
|
+
FalseClass,
|
20
|
+
TrueClass,
|
21
|
+
Symbol
|
22
|
+
]
|
23
|
+
|
12
24
|
class << self
|
13
|
-
def
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
25
|
+
def instance
|
26
|
+
@instance ||= new(registry: Registry.new)
|
27
|
+
end
|
28
|
+
|
29
|
+
def method_missing(name, *args, &block)
|
30
|
+
super unless instance.respond_to?(name)
|
31
|
+
|
32
|
+
instance.public_send(name, *args, &block)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def initialize(registry:)
|
37
|
+
@registry = registry
|
38
|
+
end
|
39
|
+
|
40
|
+
def format(object, **options)
|
41
|
+
options = default_options.merge(options)
|
42
|
+
return "(...)" if options[:ignore_objects].include?(object.object_id)
|
43
|
+
|
44
|
+
handler = registry.handler_for(object, **options)
|
45
|
+
|
46
|
+
if handler
|
47
|
+
handler.call
|
48
|
+
else
|
49
|
+
format(object.inspect, **options)
|
23
50
|
end
|
24
51
|
end
|
52
|
+
|
53
|
+
def register(klass, &handler)
|
54
|
+
registry.add(klass, &handler)
|
55
|
+
end
|
56
|
+
|
57
|
+
def define_formatter_for(klass, &handler)
|
58
|
+
registry.define(klass, &handler)
|
59
|
+
end
|
60
|
+
|
61
|
+
def simple?(object)
|
62
|
+
SIMPLE_OBJECTS.any? {|simple_obj_class| object.is_a? simple_obj_class } ||
|
63
|
+
registry.handler_for(object).is_simple
|
64
|
+
end
|
65
|
+
|
66
|
+
def default_options
|
67
|
+
{
|
68
|
+
overflow: :wrap,
|
69
|
+
indent: 0,
|
70
|
+
max_width: `tput cols`.chomp.to_i,
|
71
|
+
level: 1,
|
72
|
+
ignore_objects: [],
|
73
|
+
}
|
74
|
+
end
|
75
|
+
|
76
|
+
def format_string(string, ellipses: "...", **options)
|
77
|
+
options = default_options.merge(options)
|
78
|
+
|
79
|
+
formatters = [
|
80
|
+
Formatters::Overflow.get(options[:overflow]).new(max_width: options[:max_width] - options[:indent]),
|
81
|
+
Formatters::Indent.new(options[:indent]),
|
82
|
+
Formatters::MaxLines.new(
|
83
|
+
max_lines: options[:max_lines],
|
84
|
+
max_width: options[:max_width],
|
85
|
+
ellipses: ellipses
|
86
|
+
)
|
87
|
+
]
|
88
|
+
|
89
|
+
formatters.inject(string) do |string, formatter|
|
90
|
+
formatter.call(string)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
private
|
95
|
+
|
96
|
+
attr_reader :registry
|
25
97
|
end
|
26
98
|
end
|
99
|
+
|
100
|
+
Dir.glob(File.join(File.dirname(__FILE__), "console", "registry", "**/*.rb")).each do |file|
|
101
|
+
require file
|
102
|
+
end
|
@@ -1,11 +1,13 @@
|
|
1
1
|
module Helium
|
2
|
-
|
2
|
+
class Console
|
3
3
|
module Formatters
|
4
4
|
class MaxLines
|
5
|
-
ELLIPSES = "
|
6
|
-
|
5
|
+
ELLIPSES = "...\""
|
6
|
+
|
7
|
+
def initialize(max_lines:, max_width:, ellipses:)
|
7
8
|
@max_lines = max_lines
|
8
9
|
@max_width = max_width
|
10
|
+
@ellipses = ellipses
|
9
11
|
end
|
10
12
|
|
11
13
|
def call(string)
|
@@ -13,7 +15,7 @@ module Helium
|
|
13
15
|
|
14
16
|
lines = string.lines.first(@max_lines)
|
15
17
|
last_line = lines.pop
|
16
|
-
lines << last_line.chars.first(@max_width -
|
18
|
+
lines << last_line.chars.first(@max_width - @ellipses.length).join + @ellipses
|
17
19
|
lines.join()
|
18
20
|
end
|
19
21
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Helium
|
2
|
+
class Console
|
3
|
+
class ColorPrinter < Pry::ColorPrinter
|
4
|
+
def self.pp(obj, output = $DEFAULT_OUTPUT, max_width = 79)
|
5
|
+
queue = ColorPrinter.new(output, max_width, "\n")
|
6
|
+
queue.guard_inspect_key { queue.pp(obj) }
|
7
|
+
queue.flush
|
8
|
+
output << "\n"
|
9
|
+
end
|
10
|
+
|
11
|
+
def pp(object)
|
12
|
+
formatted = Helium::Console.format(object)
|
13
|
+
formatted = $/ + formatted if formatted.lines.count > 1
|
14
|
+
output << formatted
|
15
|
+
end
|
16
|
+
|
17
|
+
::Pry.config.print = self.method(:default)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'colorized_string'
|
2
|
+
|
3
|
+
module Helium
|
4
|
+
class Console
|
5
|
+
class Registry
|
6
|
+
class Element
|
7
|
+
def initialize(object, **options)
|
8
|
+
@object = object
|
9
|
+
@options = options
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_reader :object, :options
|
13
|
+
|
14
|
+
def format_nested(other_object, **options)
|
15
|
+
Helium::Console.format(other_object, **nested_opts(options))
|
16
|
+
end
|
17
|
+
|
18
|
+
def format(other_object, **options)
|
19
|
+
Helium::Console.format(other_object, **nested_opts(options, increase_level: false))
|
20
|
+
end
|
21
|
+
|
22
|
+
def format_string(string, **options)
|
23
|
+
Helium::Console.format_string(string, **options)
|
24
|
+
end
|
25
|
+
|
26
|
+
def simple?(object)
|
27
|
+
Helium::Console.simple?(object)
|
28
|
+
end
|
29
|
+
|
30
|
+
def is_simple
|
31
|
+
false
|
32
|
+
end
|
33
|
+
|
34
|
+
def method_missing(name, *args)
|
35
|
+
return @options[name] if @options.key?(name)
|
36
|
+
if ColorizedString.colors.include?(name)
|
37
|
+
return ColorizedString.new(*args).colorize(name)
|
38
|
+
end
|
39
|
+
super
|
40
|
+
end
|
41
|
+
|
42
|
+
def nested_opts(new_options, increase_level: true)
|
43
|
+
new_options = options.merge(new_options)
|
44
|
+
new_options[:level] += 1 if increase_level
|
45
|
+
new_options[:ignore_objects] << object.object_id
|
46
|
+
new_options
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def add(klass, &handler)
|
51
|
+
define(klass) do
|
52
|
+
define_method(:call, &handler)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def define(klass, &block)
|
57
|
+
handlers[klass] = Class.new(Element, &block)
|
58
|
+
end
|
59
|
+
|
60
|
+
def handler_for(object, **options)
|
61
|
+
object.class.ancestors.each do |ancestor|
|
62
|
+
return handlers[ancestor].new(object, **options) if handlers.key?(ancestor)
|
63
|
+
end
|
64
|
+
nil
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def handlers
|
70
|
+
@handlers ||= {}
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module Helium
|
2
|
+
class Console
|
3
|
+
define_formatter_for Array do
|
4
|
+
def call
|
5
|
+
return "[]" if object.none?
|
6
|
+
return format_inline_with_truncation if force_inline?
|
7
|
+
|
8
|
+
format_inline_with_no_truncation || format_as_table
|
9
|
+
end
|
10
|
+
|
11
|
+
def is_simple
|
12
|
+
object.none?
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def format_as_table
|
18
|
+
table = Table.new(runner: ' ', format_keys: false)
|
19
|
+
object.each.with_index do |element, index|
|
20
|
+
table.row(light_black("[#{index}]:"), element)
|
21
|
+
end
|
22
|
+
|
23
|
+
[
|
24
|
+
"[",
|
25
|
+
format(table),
|
26
|
+
"]"
|
27
|
+
].join($/)
|
28
|
+
end
|
29
|
+
|
30
|
+
def format_inline_with_truncation
|
31
|
+
joined = nil
|
32
|
+
trunc = nil
|
33
|
+
total = object.length
|
34
|
+
|
35
|
+
object.each.with_index do |element, index|
|
36
|
+
formatted = format_nested(element, max_lines: 1, nesting: 1, max_width: 15)
|
37
|
+
|
38
|
+
new_joined = [joined, formatted].compact.join(" | ")
|
39
|
+
new_trunc = (" | (#{total - index - 1} #{index.zero? ? 'elements' : 'more'})" unless index == total - 1)
|
40
|
+
|
41
|
+
break if new_joined.length > max_width - (new_trunc&.length || 0) - 4
|
42
|
+
|
43
|
+
joined = new_joined
|
44
|
+
trunc = new_trunc
|
45
|
+
end
|
46
|
+
|
47
|
+
if joined
|
48
|
+
joined = [" ", joined, trunc, " "].compact.join if joined
|
49
|
+
["[", joined, "]"].compact.join
|
50
|
+
else
|
51
|
+
"[...(#{object.length})]"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def format_inline_with_no_truncation
|
56
|
+
joined = nil
|
57
|
+
|
58
|
+
object.each do |element|
|
59
|
+
return unless simple?(element)
|
60
|
+
|
61
|
+
formatted = format_nested(element)
|
62
|
+
joined = [joined, formatted].compact.join(" | ")
|
63
|
+
|
64
|
+
return if joined.length > max_width - 4
|
65
|
+
end
|
66
|
+
joined = " #{joined} " if joined
|
67
|
+
["[", joined, "]"].compact.join
|
68
|
+
end
|
69
|
+
|
70
|
+
def force_inline?
|
71
|
+
level > 2
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
module Helium
|
2
|
+
class Console
|
3
|
+
define_formatter_for Hash do
|
4
|
+
def call
|
5
|
+
return format_inline_with_truncation if force_inline?
|
6
|
+
format_inline_with_no_truncation || format_as_table
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def format_as_table
|
12
|
+
table = Table.new(runner: ' ', after_key: after_key, format_keys: !all_symbol?)
|
13
|
+
object.each do |key, value|
|
14
|
+
key = light_blue(key.to_s) if all_symbol?
|
15
|
+
table.row(key, value)
|
16
|
+
end
|
17
|
+
|
18
|
+
[
|
19
|
+
"{",
|
20
|
+
format(table, **options),
|
21
|
+
"}"
|
22
|
+
].join($/)
|
23
|
+
end
|
24
|
+
|
25
|
+
def format_inline_with_truncation
|
26
|
+
joined = nil
|
27
|
+
trunc = nil
|
28
|
+
total = object.length
|
29
|
+
|
30
|
+
object.each.with_index do |(key, value), index|
|
31
|
+
formatted_key = format(key, max_lines: 1, nesting: 1, max_with: 15)
|
32
|
+
formatted_value = format(value, max_lines: 1, nesting: 1, max_width: 15)
|
33
|
+
|
34
|
+
formatted = "#{formatted_key} => #{formatted_value}"
|
35
|
+
|
36
|
+
new_joined = [joined, formatted].compact.join(", ")
|
37
|
+
new_trunc = (", (#{total - index - 1} #{index.zero? ? 'elements' : 'more'})" unless index == total - 1)
|
38
|
+
|
39
|
+
break if new_joined.length > max_width - (new_trunc&.length || 0) - 4
|
40
|
+
|
41
|
+
joined = new_joined
|
42
|
+
trunc = new_trunc
|
43
|
+
end
|
44
|
+
|
45
|
+
joined = [" ", joined, trunc, " "].compact.join if joined
|
46
|
+
["{", joined, "}"].compact.join
|
47
|
+
end
|
48
|
+
|
49
|
+
def format_inline_with_no_truncation
|
50
|
+
joined = nil
|
51
|
+
one_complex = false
|
52
|
+
|
53
|
+
object.each do |key, value|
|
54
|
+
return unless simple?(value)
|
55
|
+
|
56
|
+
formatted_key = format(key, max_lines: 1, nesting: 1, max_with: 15)
|
57
|
+
formatted_value = format(value, max_lines: 1, nesting: 1, max_width: 15)
|
58
|
+
formatted = "#{formatted_key} => #{formatted_value}"
|
59
|
+
|
60
|
+
joined = [joined, formatted].compact.join(", ")
|
61
|
+
|
62
|
+
return if joined.length > max_width - 4
|
63
|
+
end
|
64
|
+
joined = " #{joined} " if joined
|
65
|
+
["{", joined, "}"].compact.join
|
66
|
+
end
|
67
|
+
|
68
|
+
def force_inline?
|
69
|
+
level > 2
|
70
|
+
end
|
71
|
+
|
72
|
+
def all_symbol?
|
73
|
+
object.keys.all? { |key| key.is_a? Symbol }
|
74
|
+
end
|
75
|
+
|
76
|
+
def after_key
|
77
|
+
all_symbol? ? light_blue(": ") : light_black(" => ")
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Helium
|
2
|
+
class Console
|
3
|
+
define_formatter_for Module do
|
4
|
+
def call
|
5
|
+
light_yellow(object.name || anonymus_text)
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def anonymus_text
|
11
|
+
closest = object.ancestors.find(&:name).name
|
12
|
+
"(ananymus #{closest})"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module Helium
|
2
|
+
class Console
|
3
|
+
define_formatter_for Object do
|
4
|
+
def call
|
5
|
+
return format_inline_with_truncation if force_inline?
|
6
|
+
format_as_table
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def format_as_table
|
12
|
+
table = Table.new(runner: light_black('| '), after_key: light_black(": "), format_keys: false)
|
13
|
+
|
14
|
+
object.instance_variables.each do |inst|
|
15
|
+
table.row(magenta(inst.to_s), object.instance_variable_get(inst))
|
16
|
+
end
|
17
|
+
|
18
|
+
[
|
19
|
+
table_header,
|
20
|
+
format(table),
|
21
|
+
].reject(&:empty?).join($/)
|
22
|
+
end
|
23
|
+
|
24
|
+
def format_inline_with_truncation
|
25
|
+
"#{object.class.name}##{object.object_id.to_s(16)}"
|
26
|
+
end
|
27
|
+
|
28
|
+
def format_inline_with_no_truncation
|
29
|
+
joined = nil
|
30
|
+
|
31
|
+
object.each do |key, value|
|
32
|
+
return unless simple?(value)
|
33
|
+
|
34
|
+
formatted_key = format(key, level: 3, max_with: 15)
|
35
|
+
formatted_value = format(value, level: 3, max_width: 15)
|
36
|
+
formatted = "#{formatted_key} => #{formatted_value}"
|
37
|
+
|
38
|
+
joined = [joined, formatted].compact.join(", ")
|
39
|
+
|
40
|
+
return if joined.length > max_width - 4
|
41
|
+
end
|
42
|
+
joined = " #{joined} " if joined
|
43
|
+
["{", joined, "}"].compact.join
|
44
|
+
end
|
45
|
+
|
46
|
+
def table_header
|
47
|
+
type = case object
|
48
|
+
when Class then :class
|
49
|
+
when Module then :module
|
50
|
+
else :instance
|
51
|
+
end
|
52
|
+
"#{light_black('#')} #{light_yellow(type == :instance ? object.class.name : object.name)} #{type}"
|
53
|
+
end
|
54
|
+
|
55
|
+
def force_inline?
|
56
|
+
level > 2
|
57
|
+
end
|
58
|
+
|
59
|
+
def all_symbol?
|
60
|
+
object.keys.all? { |key| key.is_a? Symbol }
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Helium
|
2
|
+
class Console
|
3
|
+
define_formatter_for String do
|
4
|
+
def call
|
5
|
+
formatted = Helium::Console.format_string(
|
6
|
+
object.dump.gsub('\n', "\n"),
|
7
|
+
max_width: max_width,
|
8
|
+
max_lines: max_lines,
|
9
|
+
overflow: overflow,
|
10
|
+
ellipses: "...\""
|
11
|
+
)
|
12
|
+
|
13
|
+
formatted.lines
|
14
|
+
.map { |line| light_green(line) }
|
15
|
+
.join
|
16
|
+
end
|
17
|
+
|
18
|
+
def max_lines
|
19
|
+
case level
|
20
|
+
when 1 then nil
|
21
|
+
when 2 then 3
|
22
|
+
else 1
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module Helium
|
2
|
+
class Console
|
3
|
+
define_formatter_for Table do
|
4
|
+
def call
|
5
|
+
[
|
6
|
+
*formatted_values,
|
7
|
+
truncation,
|
8
|
+
].compact.join($/)
|
9
|
+
end
|
10
|
+
|
11
|
+
def formatted_values
|
12
|
+
rows.flat_map do |key, value|
|
13
|
+
formatted_value = format_nested(value, max_width: max_value_width)
|
14
|
+
|
15
|
+
formatted_value.lines.map.with_index do |line, index|
|
16
|
+
[
|
17
|
+
object.runner,
|
18
|
+
index.zero? ? rjust(format_key(key), key_width) : " " * key_width,
|
19
|
+
index.zero? ? object.after_key : " " * length_of(object.after_key),
|
20
|
+
line.chomp,
|
21
|
+
].join
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def truncation
|
27
|
+
return unless object.rows.length > rows.length
|
28
|
+
|
29
|
+
[
|
30
|
+
object.runner,
|
31
|
+
light_black("(#{object.rows.length - rows.length} more)")
|
32
|
+
].join
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
def key_width
|
37
|
+
@key_width ||= rows
|
38
|
+
.map(&:first)
|
39
|
+
.map(&method(:format_key))
|
40
|
+
.map(&method(:length_of))
|
41
|
+
.max
|
42
|
+
end
|
43
|
+
|
44
|
+
def max_value_width
|
45
|
+
max_width - length_of(object.runner) - key_width - length_of(object.after_key)
|
46
|
+
end
|
47
|
+
|
48
|
+
def format_key(key)
|
49
|
+
object.format_keys ? format(key, max_width: 15, level: 3) : key
|
50
|
+
end
|
51
|
+
|
52
|
+
def rjust(string, width)
|
53
|
+
" " * (width - length_of(string)) + string
|
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
|
62
|
+
end
|
63
|
+
|
64
|
+
def rows
|
65
|
+
@rows ||= case level
|
66
|
+
when 1 then object.rows
|
67
|
+
when 2
|
68
|
+
object.rows.count < 10 ? object.rows : object.rows.first(9)
|
69
|
+
else
|
70
|
+
object.rows.count < 3 ? object.rows : object.rows.first(2)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Helium
|
2
|
+
class Console
|
3
|
+
class Table
|
4
|
+
|
5
|
+
def initialize(runner: "|", after_key: " ", format_keys: true)
|
6
|
+
@runner = runner
|
7
|
+
@after_key = after_key
|
8
|
+
@format_keys = format_keys
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_reader :runner, :after_key, :format_keys
|
12
|
+
|
13
|
+
def row(key, value)
|
14
|
+
rows << [key, value]
|
15
|
+
end
|
16
|
+
|
17
|
+
def rows
|
18
|
+
@rows ||= []
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
metadata
CHANGED
@@ -1,17 +1,31 @@
|
|
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.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stanislaw Klajn
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-06-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: pry
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: colorize
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
16
30
|
requirements:
|
17
31
|
- - ">="
|
@@ -31,7 +45,6 @@ executables: []
|
|
31
45
|
extensions: []
|
32
46
|
extra_rdoc_files: []
|
33
47
|
files:
|
34
|
-
- ".byebug_history"
|
35
48
|
- ".gitignore"
|
36
49
|
- ".rspec"
|
37
50
|
- ".travis.yml"
|
@@ -49,6 +62,19 @@ files:
|
|
49
62
|
- lib/helium/console/formatters/max_lines.rb
|
50
63
|
- lib/helium/console/formatters/overflow.rb
|
51
64
|
- lib/helium/console/formatters/overflow/wrap.rb
|
65
|
+
- lib/helium/console/printer.rb
|
66
|
+
- lib/helium/console/registry.rb
|
67
|
+
- lib/helium/console/registry/array.rb
|
68
|
+
- lib/helium/console/registry/booleans.rb
|
69
|
+
- lib/helium/console/registry/hash.rb
|
70
|
+
- lib/helium/console/registry/module.rb
|
71
|
+
- lib/helium/console/registry/nil.rb
|
72
|
+
- lib/helium/console/registry/numeric.rb
|
73
|
+
- lib/helium/console/registry/object.rb
|
74
|
+
- lib/helium/console/registry/string.rb
|
75
|
+
- lib/helium/console/registry/symbol.rb
|
76
|
+
- lib/helium/console/registry/table.rb
|
77
|
+
- lib/helium/console/table.rb
|
52
78
|
- lib/helium/console/version.rb
|
53
79
|
homepage: https://github.com/helium-rb/console
|
54
80
|
licenses:
|