helium-console 0.1.7 → 0.1.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Helium
2
4
  class Console
3
5
  define_formatter_for TrueClass do
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Helium
4
+ class Console
5
+ define_formatter_for 'Date' do
6
+ def call
7
+ blue object.strftime('%A, %d %b %Y')
8
+ end
9
+ end
10
+ end
11
+ end
@@ -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 format_inline_with_truncation if force_inline?
6
- format_inline_with_no_truncation || format_as_table
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
- private
13
+ private
10
14
 
11
15
  def format_as_table
12
- table = Table.new(runner: ' ', after_key: after_key, format_keys: !all_symbol?)
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, **options),
21
- "}"
22
- ].join($/)
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 format_inline_with_truncation
26
- joined = nil
27
- trunc = nil
28
- total = object.length
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
- 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}"
34
+ new_collected
35
+ end
35
36
 
36
- new_joined = [joined, formatted].compact.join(", ")
37
- new_trunc = (", (#{total - index - 1} #{index.zero? ? 'elements' : 'more'})" unless index == total - 1)
37
+ ['{ ', truncated.join(', '), ' }'].compact.join
38
+ end
38
39
 
39
- break if new_joined.length > max_width - (new_trunc&.length || 0) - 4
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
- joined = new_joined
42
- trunc = new_trunc
45
+ collected
43
46
  end
44
47
 
45
- joined = [" ", joined, trunc, " "].compact.join if joined
46
- ["{", joined, "}"].compact.join
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 format_inline_with_no_truncation
50
- joined = nil
51
- one_complex = false
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
- object.each do |key, value|
54
- return unless simple?(value)
65
+ def all_symbol?
66
+ return false
67
+ return @all_symbol if defined?(@all_symbol)
55
68
 
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}"
69
+ @all_symbol = object.keys.all? { |key| key.is_a? Symbol }
70
+ end
59
71
 
60
- joined = [joined, formatted].compact.join(", ")
72
+ def format_key(key, **options)
73
+ return light_blue(key.to_s) if all_symbol?
61
74
 
62
- return if joined.length > max_width - 4
63
- end
64
- joined = " #{joined} " if joined
65
- ["{", joined, "}"].compact.join
75
+ format_nested(key, **options)
66
76
  end
67
77
 
68
- def force_inline?
69
- level > 2
78
+ def total_elements
79
+ @total_elements ||= object.length
70
80
  end
71
81
 
72
- def all_symbol?
73
- object.keys.all? { |key| key.is_a? Symbol }
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(": ") : light_black(" => ")
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 || anonymus_text)
7
+ light_yellow(object.name || anonymous_text)
6
8
  end
7
9
 
8
10
  private
9
11
 
10
- def anonymus_text
11
- closest = object.ancestors.find(&:name).name
12
- "(anonymous #{closest})"
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,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Helium
2
4
  class Console
3
5
  define_formatter_for NilClass do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Helium
2
4
  class Console
3
5
  define_formatter_for Numeric do
@@ -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 format_inline_with_truncation if force_inline?
7
+ return inline_with_truncation if force_inline?
8
+
6
9
  format_as_table
7
10
  end
8
11
 
9
- private
12
+ private
10
13
 
11
14
  def format_as_table
12
- table = Table.new(runner: light_black('| '), after_key: light_black(": "), format_keys: false)
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
- 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)}"
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 format_inline_with_no_truncation
29
- joined = nil
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
- 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}"
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
- joined = [joined, formatted].compact.join(", ")
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
- def table_header
47
- type = case object
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,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Helium
4
+ class Console
5
+ define_formatter_for 'Pathname' do
6
+ def call
7
+ "#{format(Pathname, short: true)}: #{yellow(object.to_s)}"
8
+ end
9
+ end
10
+ end
11
+ 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
- when 1 then nil
21
- when 2 then 3
22
- else 1
22
+ when 1 then nil
23
+ when 2 then 3
24
+ else 1
23
25
  end
24
26
  end
25
27
  end
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Helium
2
4
  class Console
3
5
  define_formatter_for Symbol do
4
6
  def call
5
- light_blue(':' + object.to_s)
7
+ light_blue(":#{object}")
6
8
  end
7
9
  end
8
10
  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
- *formatted_values,
7
- truncation,
8
- ].compact.join($/)
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
- def formatted_values
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
- 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
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 truncation
27
- return unless object.rows.length > rows.length
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
- " " * (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
55
+ ' ' * (width - length_of(string)) + string
62
56
  end
63
57
 
64
58
  def rows
65
59
  @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)
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