helium-console 0.1.0 → 0.1.1
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/.gitignore +2 -0
- data/Gemfile.lock +7 -3
- data/bin/console +3 -8
- data/helium-console.gemspec +1 -1
- data/lib/helium/console.rb +86 -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 +58 -0
- data/lib/helium/console/registry/array.rb +69 -0
- data/lib/helium/console/registry/hash.rb +77 -0
- data/lib/helium/console/registry/object.rb +15 -0
- data/lib/helium/console/registry/string.rb +24 -0
- data/lib/helium/console/registry/table.rb +40 -0
- data/lib/helium/console/table.rb +22 -0
- data/lib/helium/console/version.rb +2 -2
- metadata +11 -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: 54fc81366dbb279e9171184f31ff7135a786bc4d580c87d09c2d2c7b6b2356aa
|
4
|
+
data.tar.gz: dc4b4b8558dd42e2bffa3e9a7d08c0219d69ab5acb8b508742552924dfe70c7e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a76d12e67666b760c17c7b3688cba45e47313a29d03f6c08a7bb9e278c55b2abbb7de882eb3f258a4ae61614afb53c41dc288966945bf66ff65601f88819bac4
|
7
|
+
data.tar.gz: 4fec3cb33980a14cf3e9f7cbbf09a93f4707438076b1009bccfdc02f61184a002b6fb37c19a12cc993f5d4f1a1f1fe276e481bda1e1a09166251cd0532090e6b
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,15 +1,20 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
helium-console (0.1.
|
5
|
-
|
4
|
+
helium-console (0.1.1)
|
5
|
+
pry
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
10
|
byebug (11.1.3)
|
11
|
+
coderay (1.1.3)
|
11
12
|
diff-lcs (1.4.4)
|
12
13
|
ffaker (2.18.0)
|
14
|
+
method_source (1.0.0)
|
15
|
+
pry (0.14.1)
|
16
|
+
coderay (~> 1.1)
|
17
|
+
method_source (~> 1.0)
|
13
18
|
rake (12.3.3)
|
14
19
|
rspec (3.10.0)
|
15
20
|
rspec-core (~> 3.10.0)
|
@@ -24,7 +29,6 @@ GEM
|
|
24
29
|
diff-lcs (>= 1.2.0, < 2.0)
|
25
30
|
rspec-support (~> 3.10.0)
|
26
31
|
rspec-support (3.10.2)
|
27
|
-
ruby-terminfo (0.1.1)
|
28
32
|
|
29
33
|
PLATFORMS
|
30
34
|
ruby
|
data/bin/console
CHANGED
@@ -3,12 +3,7 @@
|
|
3
3
|
require "bundler/setup"
|
4
4
|
require "helium/console"
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
require 'byebug'
|
7
|
+
require 'ffaker'
|
8
8
|
|
9
|
-
|
10
|
-
# require "pry"
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require "irb"
|
14
|
-
IRB.start(__FILE__)
|
9
|
+
Pry.start
|
data/helium-console.gemspec
CHANGED
data/lib/helium/console.rb
CHANGED
@@ -1,26 +1,99 @@
|
|
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
|
+
handler = registry.handler_for(object, **options)
|
43
|
+
|
44
|
+
if handler
|
45
|
+
handler.call
|
46
|
+
else
|
47
|
+
format(object.inspect, **options)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def register(klass, &handler)
|
52
|
+
registry.add(klass, &handler)
|
53
|
+
end
|
54
|
+
|
55
|
+
def define_formatter_for(klass, &handler)
|
56
|
+
registry.define(klass, &handler)
|
57
|
+
end
|
58
|
+
|
59
|
+
def simple?(object)
|
60
|
+
SIMPLE_OBJECTS.any? {|simple_obj_class| object.is_a? simple_obj_class }
|
61
|
+
end
|
62
|
+
|
63
|
+
def default_options
|
64
|
+
{
|
65
|
+
overflow: :wrap,
|
66
|
+
indent: 0,
|
67
|
+
max_lines: nil,
|
68
|
+
max_width: `tput cols`.chomp.to_i,
|
69
|
+
level: 1
|
70
|
+
}
|
71
|
+
end
|
72
|
+
|
73
|
+
def format_string(string, ellipses: "...", **options)
|
74
|
+
options = default_options.merge(options)
|
75
|
+
|
76
|
+
formatters = [
|
77
|
+
Formatters::Overflow.get(options[:overflow]).new(max_width: options[:max_width] - options[:indent]),
|
78
|
+
Formatters::Indent.new(options[:indent]),
|
79
|
+
Formatters::MaxLines.new(
|
80
|
+
max_lines: options[:max_lines],
|
81
|
+
max_width: options[:max_width],
|
82
|
+
ellipses: ellipses
|
83
|
+
)
|
84
|
+
]
|
85
|
+
|
86
|
+
formatters.inject(string) do |string, formatter|
|
87
|
+
formatter.call(string)
|
23
88
|
end
|
24
89
|
end
|
90
|
+
|
91
|
+
private
|
92
|
+
|
93
|
+
attr_reader :registry
|
25
94
|
end
|
26
95
|
end
|
96
|
+
|
97
|
+
Dir.glob(File.join(File.dirname(__FILE__), "console", "registry", "**/*.rb")).each do |file|
|
98
|
+
require file
|
99
|
+
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,58 @@
|
|
1
|
+
module Helium
|
2
|
+
class Console
|
3
|
+
class Registry
|
4
|
+
class Element
|
5
|
+
def initialize(object, **options)
|
6
|
+
@object = object
|
7
|
+
@options = options
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_reader :object, :options
|
11
|
+
|
12
|
+
def format(object, **options)
|
13
|
+
Helium::Console.format(object, **nested_opts(options))
|
14
|
+
end
|
15
|
+
|
16
|
+
def format_string(string, **options)
|
17
|
+
Helium::Console.format_string(string, **options)
|
18
|
+
end
|
19
|
+
|
20
|
+
def simple?(object)
|
21
|
+
Helium::Console.simple?(object)
|
22
|
+
end
|
23
|
+
|
24
|
+
def method_missing(name, *args)
|
25
|
+
return @options[name] if @options.key?(name)
|
26
|
+
super
|
27
|
+
end
|
28
|
+
|
29
|
+
def nested_opts(options)
|
30
|
+
{ level: @options[:level] + 1 }.merge(options)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def add(klass, &handler)
|
35
|
+
define(klass) do
|
36
|
+
define_method(:call, &handler)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def define(klass, &block)
|
41
|
+
handlers[klass] = Class.new(Element, &block)
|
42
|
+
end
|
43
|
+
|
44
|
+
def handler_for(object, **options)
|
45
|
+
object.class.ancestors.each do |ancestor|
|
46
|
+
return handlers[ancestor].new(object, **options) if handlers.key?(ancestor)
|
47
|
+
end
|
48
|
+
nil
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def handlers
|
54
|
+
@handlers ||= {}
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module Helium
|
2
|
+
class Console
|
3
|
+
define_formatter_for Array 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: ' ', format_keys: false)
|
13
|
+
object.each.with_index do |element, index|
|
14
|
+
table.row("[#{index}]:", element)
|
15
|
+
end
|
16
|
+
|
17
|
+
[
|
18
|
+
"[",
|
19
|
+
format(table, **options),
|
20
|
+
"]"
|
21
|
+
].join($/)
|
22
|
+
end
|
23
|
+
|
24
|
+
def format_inline_with_truncation
|
25
|
+
joined = nil
|
26
|
+
trunc = nil
|
27
|
+
total = object.length
|
28
|
+
|
29
|
+
object.each.with_index do |element, index|
|
30
|
+
formatted = format(element, max_lines: 1, nesting: 1, max_width: 15)
|
31
|
+
|
32
|
+
new_joined = [joined, formatted].compact.join(" | ")
|
33
|
+
new_trunc = (" | (#{total - index - 1} #{index.zero? ? 'elements' : 'more'})" unless index == total - 1)
|
34
|
+
|
35
|
+
break if new_joined.length > max_width - (new_trunc&.length || 0) - 4
|
36
|
+
|
37
|
+
joined = new_joined
|
38
|
+
trunc = new_trunc
|
39
|
+
end
|
40
|
+
|
41
|
+
if joined
|
42
|
+
joined = [" ", joined, trunc, " "].compact.join if joined
|
43
|
+
["[", joined, "]"].compact.join
|
44
|
+
else
|
45
|
+
"[...(#{object.length})]"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def format_inline_with_no_truncation
|
50
|
+
joined = nil
|
51
|
+
|
52
|
+
object.each do |element|
|
53
|
+
return unless simple?(element)
|
54
|
+
|
55
|
+
formatted = format(element)
|
56
|
+
joined = [joined, formatted].compact.join(" | ")
|
57
|
+
|
58
|
+
return if joined.length > max_width - 4
|
59
|
+
end
|
60
|
+
joined = " #{joined} " if joined
|
61
|
+
["[", joined, "]"].compact.join
|
62
|
+
end
|
63
|
+
|
64
|
+
def force_inline?
|
65
|
+
level > 2
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,77 @@
|
|
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: all_symbol? ? ": " : " => ", format_keys: !all_symbol?)
|
13
|
+
object.each do |key, value|
|
14
|
+
key = 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
|
+
(max_lines && max_lines < 5) || level > 2
|
70
|
+
end
|
71
|
+
|
72
|
+
def all_symbol?
|
73
|
+
object.keys.all? { |key| key.is_a? Symbol }
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Helium
|
2
|
+
class Console
|
3
|
+
register Object do
|
4
|
+
formatters = [
|
5
|
+
Formatters::Overflow.get(overflow).new(max_width: max_width - indent),
|
6
|
+
Formatters::Indent.new(indent),
|
7
|
+
Formatters::MaxLines.new(max_lines: max_lines, max_width: max_width, ellipses: "...")
|
8
|
+
]
|
9
|
+
|
10
|
+
formatters.inject(object.inspect) do |string, formatter|
|
11
|
+
formatter.call(string)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Helium
|
2
|
+
class Console
|
3
|
+
define_formatter_for String do
|
4
|
+
def call
|
5
|
+
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
|
+
end
|
13
|
+
|
14
|
+
def max_lines
|
15
|
+
return options[:max_lines] if options[:max_lines]
|
16
|
+
case level
|
17
|
+
when 1 then nil
|
18
|
+
when 2 then 3
|
19
|
+
else 1
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Helium
|
2
|
+
class Console
|
3
|
+
define_formatter_for Table do
|
4
|
+
def call
|
5
|
+
formatted_values.join($/)
|
6
|
+
end
|
7
|
+
|
8
|
+
def formatted_values
|
9
|
+
object.rows.flat_map do |key, value|
|
10
|
+
formatted_value = format(value, max_width: max_value_width)
|
11
|
+
|
12
|
+
formatted_value.lines.map.with_index do |line, index|
|
13
|
+
[
|
14
|
+
object.runner,
|
15
|
+
index.zero? ? format_key(key).rjust(key_width) : " " * key_width,
|
16
|
+
index.zero? ? object.after_key : " " * object.after_key.length,
|
17
|
+
line.chomp,
|
18
|
+
].join
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def key_width
|
24
|
+
@key_width ||= object.rows
|
25
|
+
.map(&:first)
|
26
|
+
.map(&method(:format_key))
|
27
|
+
.map(&:length).max
|
28
|
+
end
|
29
|
+
|
30
|
+
def max_value_width
|
31
|
+
max_width - object.runner.length - key_width - object.after_key.length
|
32
|
+
end
|
33
|
+
|
34
|
+
def format_key(key)
|
35
|
+
return key unless object.format_keys
|
36
|
+
format(key, max_width: 15, level: 3)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
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,17 @@
|
|
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.1
|
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-05
|
11
|
+
date: 2021-06-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: pry
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
@@ -31,7 +31,6 @@ executables: []
|
|
31
31
|
extensions: []
|
32
32
|
extra_rdoc_files: []
|
33
33
|
files:
|
34
|
-
- ".byebug_history"
|
35
34
|
- ".gitignore"
|
36
35
|
- ".rspec"
|
37
36
|
- ".travis.yml"
|
@@ -49,6 +48,14 @@ files:
|
|
49
48
|
- lib/helium/console/formatters/max_lines.rb
|
50
49
|
- lib/helium/console/formatters/overflow.rb
|
51
50
|
- lib/helium/console/formatters/overflow/wrap.rb
|
51
|
+
- lib/helium/console/printer.rb
|
52
|
+
- lib/helium/console/registry.rb
|
53
|
+
- lib/helium/console/registry/array.rb
|
54
|
+
- lib/helium/console/registry/hash.rb
|
55
|
+
- lib/helium/console/registry/object.rb
|
56
|
+
- lib/helium/console/registry/string.rb
|
57
|
+
- lib/helium/console/registry/table.rb
|
58
|
+
- lib/helium/console/table.rb
|
52
59
|
- lib/helium/console/version.rb
|
53
60
|
homepage: https://github.com/helium-rb/console
|
54
61
|
licenses:
|