helium-console 0.1.6 → 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e6e87b40fb5846db36163081102d3a414bdecdbdc782f6f4c0da3f0001536fd7
4
- data.tar.gz: 1ba3c78a6f962b1a64e883d350c35618e6bbcad4de3a2ac789058a90f0efceff
3
+ metadata.gz: 2775258ea31537dc27077753497d8c6fef2d34f31e6470c17574cd30395675e9
4
+ data.tar.gz: eafd3659c58654fc6566359f52093c928d7ecfc4bab9c23df74330f814760c6c
5
5
  SHA512:
6
- metadata.gz: 9dd45ae18f91693ef71bff49a32a7fa09d0a0fa1d2eae81ed92b0d1b34f110085eea6004fcfe2241f372f3407d0aedb42d4bb521eaf40de71be43bb8e7cd265e
7
- data.tar.gz: 0e7a3083dc49d4e3798ac54e0c6636a589f783b4faa41c544d9970bcc694d7ea22b82297f757c788965e9908675b6df340149521cb9dd97ab7e9200cd2bc0200
6
+ metadata.gz: a18002a4319bb7154810167feb4888ab5199034f001262614d291725623a856fd0a18fea8c362573fb049b823b10753a928bc3401fefcda8be66258f3420bb39
7
+ data.tar.gz: 56b961dcbfab3a0b39041abba71a36e90166cb7f409f9ea54d3957b2c147ba304e5c35d6c1dcb5c0415e9b0d3f5ea71bbdfb14494ca08a15526b5e43dfa2a89b
data/.rubocop.yml ADDED
@@ -0,0 +1,70 @@
1
+ require:
2
+ - rubocop-rspec
3
+ - rubocop-rake
4
+
5
+ AllCops:
6
+ NewCops: enable
7
+
8
+ Style/LambdaCall:
9
+ EnforcedStyle: braces
10
+
11
+ Layout/CaseIndentation:
12
+ EnforcedStyle: end
13
+ IndentOneStep: true
14
+
15
+ Style/NestedParenthesizedCalls:
16
+ AllowedMethods:
17
+ - be
18
+ - be_a
19
+ - be_an
20
+ - be_between
21
+ - be_falsey
22
+ - be_kind_of
23
+ - be_instance_of
24
+ - be_truthy
25
+ - be_within
26
+ - eq
27
+ - eql
28
+ - end_with
29
+ - expect
30
+ - include
31
+ - match
32
+ - raise_error
33
+ - respond_to
34
+ - start_with
35
+
36
+ Layout/EndAlignment:
37
+ EnforcedStyleAlignWith: start_of_line
38
+
39
+ Layout/MultilineMethodCallIndentation:
40
+ EnforcedStyle: indented
41
+
42
+ Style/Documentation:
43
+ Enabled: false
44
+
45
+ Bundler/OrderedGems:
46
+ Enabled: false
47
+
48
+ Style/EmptyMethod:
49
+ EnforcedStyle: expanded
50
+
51
+ Metrics/BlockLength:
52
+ IgnoredMethods:
53
+ - define_formatter_for
54
+ Exclude:
55
+ - "*.gemspec"
56
+ - "spec/**/*_spec.rb"
57
+
58
+ Metrics/MethodLength:
59
+ Max: 15
60
+
61
+ RSpec/MultipleExpectations:
62
+ Enabled: false
63
+
64
+ Style/NumericPredicate:
65
+ Enabled: false
66
+
67
+ Style/EachWithObject:
68
+ Enabled: false
69
+
70
+
data/Gemfile CHANGED
@@ -1,9 +1,14 @@
1
- source "https://rubygems.org"
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in helium-console.gemspec
4
6
  gemspec
5
7
 
6
- gem "rake", "~> 12.0"
7
- gem "rspec", "~> 3.0"
8
- gem "ffaker"
9
- gem "byebug"
8
+ gem 'rake', '~> 12.0'
9
+ gem 'rspec', '~> 3.0'
10
+ gem 'ffaker'
11
+ gem 'byebug'
12
+ gem 'rubocop', require: false
13
+ gem 'rubocop-rspec', require: false
14
+ gem 'rubocop-rake', require: false
data/Gemfile.lock CHANGED
@@ -1,23 +1,30 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- helium-console (0.1.6)
4
+ helium-console (0.1.10)
5
5
  colorize
6
6
  pry
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
+ ast (2.4.2)
11
12
  byebug (11.1.3)
12
13
  coderay (1.1.3)
13
14
  colorize (0.8.1)
14
15
  diff-lcs (1.4.4)
15
16
  ffaker (2.18.0)
16
17
  method_source (1.0.0)
18
+ parallel (1.20.1)
19
+ parser (3.0.2.0)
20
+ ast (~> 2.4.1)
17
21
  pry (0.14.1)
18
22
  coderay (~> 1.1)
19
23
  method_source (~> 1.0)
24
+ rainbow (3.0.0)
20
25
  rake (12.3.3)
26
+ regexp_parser (2.1.1)
27
+ rexml (3.2.5)
21
28
  rspec (3.10.0)
22
29
  rspec-core (~> 3.10.0)
23
30
  rspec-expectations (~> 3.10.0)
@@ -31,6 +38,24 @@ GEM
31
38
  diff-lcs (>= 1.2.0, < 2.0)
32
39
  rspec-support (~> 3.10.0)
33
40
  rspec-support (3.10.2)
41
+ rubocop (1.19.0)
42
+ parallel (~> 1.10)
43
+ parser (>= 3.0.0.0)
44
+ rainbow (>= 2.2.2, < 4.0)
45
+ regexp_parser (>= 1.8, < 3.0)
46
+ rexml
47
+ rubocop-ast (>= 1.9.1, < 2.0)
48
+ ruby-progressbar (~> 1.7)
49
+ unicode-display_width (>= 1.4.0, < 3.0)
50
+ rubocop-ast (1.10.0)
51
+ parser (>= 3.0.1.1)
52
+ rubocop-rake (0.6.0)
53
+ rubocop (~> 1.0)
54
+ rubocop-rspec (2.4.0)
55
+ rubocop (~> 1.0)
56
+ rubocop-ast (>= 1.1.0)
57
+ ruby-progressbar (1.11.0)
58
+ unicode-display_width (2.0.0)
34
59
 
35
60
  PLATFORMS
36
61
  ruby
@@ -41,6 +66,9 @@ DEPENDENCIES
41
66
  helium-console!
42
67
  rake (~> 12.0)
43
68
  rspec (~> 3.0)
69
+ rubocop
70
+ rubocop-rake
71
+ rubocop-rspec
44
72
 
45
73
  BUNDLED WITH
46
74
  2.1.4
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
6
- task :default => :spec
8
+ task default: :spec
data/bin/console CHANGED
@@ -1,15 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- require "bundler/setup"
4
- require "helium/console"
4
+ require 'bundler/setup'
5
+ require 'helium/console'
5
6
 
6
7
  require 'byebug'
7
8
  require 'ffaker'
8
9
 
9
- class A
10
- def initialize(a)
11
- @a = a
12
- end
13
- end
14
-
15
10
  Pry.start
@@ -1,27 +1,29 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'lib/helium/console/version'
2
4
 
3
5
  Gem::Specification.new do |spec|
4
- spec.name = "helium-console"
6
+ spec.name = 'helium-console'
5
7
  spec.version = Helium::Console::VERSION
6
- spec.authors = ["Stanislaw Klajn"]
7
- spec.email = ["sklajn@gmail.com"]
8
+ spec.authors = ['Stanislaw Klajn']
9
+ spec.email = ['sklajn@gmail.com']
8
10
 
9
- spec.summary = "Collection of tools for smooth integration with console"
10
- spec.homepage = "https://github.com/helium-rb/console"
11
- spec.license = "MIT"
12
- spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
11
+ spec.summary = 'Collection of tools for smooth integration with console'
12
+ spec.homepage = 'https://github.com/helium-rb/console'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
13
15
 
14
- spec.metadata["homepage_uri"] = spec.homepage
15
- spec.metadata["source_code_uri"] = "https://github.com/helium-rb/console"
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = 'https://github.com/helium-rb/console'
16
18
 
17
19
  # Specify which files should be added to the gem when it is released.
18
20
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
22
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
23
  end
22
- spec.bindir = "exe"
24
+ spec.bindir = 'exe'
23
25
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
- spec.require_paths = ["lib"]
25
- spec.add_dependency "pry"
26
- spec.add_dependency "colorize"
26
+ spec.require_paths = ['lib']
27
+ spec.add_dependency 'colorize'
28
+ spec.add_dependency 'pry'
27
29
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Helium
2
4
  class Console
3
5
  module Formatters
@@ -7,7 +9,7 @@ module Helium
7
9
  end
8
10
 
9
11
  def call(string)
10
- string.lines.map {|line| ' ' * @indent + line }.join
12
+ string.lines.map { |line| ' ' * @indent + line }.join
11
13
  end
12
14
  end
13
15
  end
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Helium
2
4
  class Console
3
5
  module Formatters
4
6
  class MaxLines
5
- ELLIPSES = "...\""
7
+ ELLIPSES = '..."'
6
8
 
7
9
  def initialize(max_lines:, max_width:, ellipses:)
8
10
  @max_lines = max_lines
@@ -16,7 +18,7 @@ module Helium
16
18
  lines = string.lines.first(@max_lines)
17
19
  last_line = lines.pop
18
20
  lines << last_line.chars.first(@max_width - @ellipses.length).join + @ellipses
19
- lines.join()
21
+ lines.join
20
22
  end
21
23
  end
22
24
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Helium
2
4
  class Console
3
5
  module Formatters
@@ -11,8 +13,8 @@ module Helium
11
13
  result = string.lines.flat_map do |line|
12
14
  line.chomp.chars.each_slice(@max_width).map(&:join)
13
15
  end
14
- result = result.join($/)
15
- result += $/ if string.end_with?($/)
16
+ result = result.join("\n")
17
+ result += "\n" if string.end_with?("\n")
16
18
  result
17
19
  end
18
20
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  module Helium
3
4
  class Console
@@ -6,8 +7,8 @@ module Helium
6
7
  def self.get(type)
7
8
  require "helium/console/formatters/overflow/#{type}"
8
9
  const_get(type.to_s.split('_').map(&:capitalize).join)
9
- rescue
10
- raise Error.new("Unknown overflow option: #{type}")
10
+ rescue LoadError
11
+ raise Error, "Unknown overflow option: #{type}"
11
12
  end
12
13
  end
13
14
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Helium
2
4
  class Console
3
5
  class ColorPrinter < Pry::ColorPrinter
@@ -10,11 +12,11 @@ module Helium
10
12
 
11
13
  def pp(object)
12
14
  formatted = Helium::Console.format(object)
13
- formatted = $/ + formatted if formatted.lines.count > 1
15
+ formatted = "\n#{formatted}" if formatted.lines.count > 1
14
16
  output << formatted
15
17
  end
16
18
 
17
- ::Pry.config.print = self.method(:default)
19
+ ::Pry.config.print = method(:default)
18
20
  end
19
21
  end
20
22
  end
@@ -1,18 +1,20 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Helium
2
4
  class Console
3
5
  define_formatter_for Array do
4
6
  def call
5
- return "[]" if object.none?
6
- return format_inline_with_truncation if force_inline?
7
+ return '[]' if object.none?
8
+ return inline_with_truncation if force_inline?
7
9
 
8
- format_inline_with_no_truncation || format_as_table
10
+ inline_without_truncation || format_as_table
9
11
  end
10
12
 
11
- def is_simple
13
+ def simple?
12
14
  object.none?
13
15
  end
14
16
 
15
- private
17
+ private
16
18
 
17
19
  def format_as_table
18
20
  table = Table.new(runner: ' ', format_keys: false)
@@ -21,50 +23,53 @@ module Helium
21
23
  end
22
24
 
23
25
  [
24
- "[",
26
+ '[',
25
27
  format(table),
26
- "]"
27
- ].join($/)
28
+ ']'
29
+ ].join("\n")
28
30
  end
29
31
 
30
- def format_inline_with_truncation
31
- joined = nil
32
- trunc = nil
33
- total = object.length
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 - 4)
34
36
 
35
- object.each.with_index do |element, index|
36
- formatted = format_nested(element, max_lines: 1, nesting: 1, max_width: 15)
37
+ new_joined
38
+ end
37
39
 
38
- new_joined = [joined, formatted].compact.join(" | ")
39
- new_trunc = (" | (#{total - index - 1} #{index.zero? ? 'elements' : 'more'})" unless index == total - 1)
40
+ "[ #{formatted.compact.join(' | ')} ]"
41
+ end
40
42
 
41
- break if new_joined.length > max_width - (new_trunc&.length || 0) - 4
43
+ def inline_without_truncation
44
+ return unless object.all? { |element| Helium::Console.simple? element }
42
45
 
43
- joined = new_joined
44
- trunc = new_trunc
45
- end
46
+ formatted = formatted_elements.inject([]) do |joined, element|
47
+ joined = [*joined, element]
48
+ break if too_long?(joined, max_width: max_width - 4)
46
49
 
47
- if joined
48
- joined = [" ", joined, trunc, " "].compact.join if joined
49
- ["[", joined, "]"].compact.join
50
- else
51
- "[...(#{object.length})]"
50
+ joined
52
51
  end
52
+
53
+ "[ #{formatted.join(' | ')} ]" unless formatted.nil?
53
54
  end
54
55
 
55
- def format_inline_with_no_truncation
56
- joined = nil
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
57
60
 
58
- object.each do |element|
59
- return unless simple?(element)
61
+ def formatted_elements(**options)
62
+ object.each.lazy.map { |element| format_nested(element, **options) }
63
+ end
60
64
 
61
- formatted = format_nested(element)
62
- joined = [joined, formatted].compact.join(" | ")
65
+ def trunc_message(count, all_truncated: false)
66
+ return if count < 1
63
67
 
64
- return if joined.length > max_width - 4
65
- end
66
- joined = " #{joined} " if joined
67
- ["[", joined, "]"].compact.join
68
+ "(#{count} #{all_truncated ? 'elements' : 'more'})"
69
+ end
70
+
71
+ def object_size
72
+ @object_size ||= object.size
68
73
  end
69
74
 
70
75
  def force_inline?
@@ -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,12 +1,16 @@
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
16
  table = Table.new(runner: ' ', after_key: after_key, format_keys: !all_symbol?)
@@ -16,65 +20,69 @@ module Helium
16
20
  end
17
21
 
18
22
  [
19
- "{",
23
+ '{',
20
24
  format(table, **options),
21
- "}"
22
- ].join($/)
25
+ '}'
26
+ ].join("\n")
23
27
  end
24
28
 
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)
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
38
33
 
39
- break if new_joined.length > max_width - (new_trunc&.length || 0) - 4
40
-
41
- joined = new_joined
42
- trunc = new_trunc
34
+ new_collected
43
35
  end
44
36
 
45
- joined = [" ", joined, trunc, " "].compact.join if joined
46
- ["{", joined, "}"].compact.join
37
+ ['{ ', truncated.join(', '), ' }'].compact.join
47
38
  end
48
39
 
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)
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
55
44
 
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}"
45
+ collected
46
+ end
59
47
 
60
- joined = [joined, formatted].compact.join(", ")
48
+ return if formatted.nil?
61
49
 
62
- return if joined.length > max_width - 4
63
- end
64
- joined = " #{joined} " if joined
65
- ["{", joined, "}"].compact.join
50
+ ['{ ', formatted.join(', '), ' }'].compact.join
66
51
  end
67
52
 
68
53
  def force_inline?
69
54
  level > 2
70
55
  end
71
56
 
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
64
+
72
65
  def all_symbol?
73
66
  object.keys.all? { |key| key.is_a? Symbol }
74
67
  end
75
68
 
69
+ def format_key(key, **options)
70
+ return light_blue(key.to_s) if all_symbol?
71
+
72
+ format_nested(key, **options)
73
+ end
74
+
75
+ def total_elements
76
+ @total_elements ||= object.length
77
+ end
78
+
79
+ def trunc_text(count)
80
+ truncated_elements = total_elements - count
81
+ light_black("(#{truncated_elements} #{count.zero? ? 'elements' : 'more'})")
82
+ end
83
+
76
84
  def after_key
77
- all_symbol? ? light_blue(": ") : light_black(" => ")
85
+ all_symbol? ? light_blue(': ') : light_black(' => ')
78
86
  end
79
87
  end
80
88
  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,63 +1,61 @@
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
21
  [
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)}"
22
+ "#{light_black '#'} #{class_name}",
23
+ format(table)
24
+ ].reject(&:empty?).join("\n")
26
25
  end
27
26
 
28
- def format_inline_with_no_truncation
29
- joined = nil
27
+ def inline_with_truncation
28
+ class_name = class_name(short: true)
30
29
 
31
- object.each do |key, value|
32
- return unless simple?(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
33
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
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
 
41
+ # def inline_without_truncation
42
+ # joined = nil
43
+ #
44
+ # object.each do |key, value|
45
+ # return unless simple?(value)
46
+ #
47
+ # formatted_key = format(key, level: 3, max_with: 15)
48
+ # formatted_value = format(value, level: 3, max_width: 15)
49
+ # formatted = "#{formatted_key} => #{formatted_value}"
50
+ #
51
+ # joined = [joined, formatted].compact.join(', ')
52
+ #
53
+ # return if joined.length > max_width - 4
54
+ # end
55
+ # joined = " #{joined} " if joined
56
+ # ['{', joined, '}'].compact.join
57
+ # end
58
+
61
59
  def force_inline?
62
60
  level > 2
63
61
  end
@@ -65,6 +63,18 @@ module Helium
65
63
  def all_symbol?
66
64
  object.keys.all? { |key| key.is_a? Symbol }
67
65
  end
66
+
67
+ def formatted_instance_variables(**options)
68
+ object.instance_variables.sort.each.lazy.map do |var_name|
69
+ value = object.instance_variable_get(var_name)
70
+ "#{magenta(var_name.to_s)} = #{format_nested(value, **options)}"
71
+ end
72
+ end
73
+
74
+ def class_name(short: false)
75
+ formatted = format(object.class, short: short)
76
+ short ? "##{formatted}" : "#{formatted} instance"
77
+ end
68
78
  end
69
79
  end
70
80
  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,37 +1,41 @@
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
7
  [
6
8
  *formatted_values,
7
- truncation,
8
- ].compact.join($/)
9
+ truncation
10
+ ].compact.join("\n")
9
11
  end
10
12
 
11
- def formatted_values
12
- rows.flat_map do |key, value|
13
- formatted_value = format_nested(value, max_width: max_value_width)
13
+ private
14
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
15
+ def formatted_values
16
+ rows.flat_map do |key, value, options = {}|
17
+ format_pair(key, value, **options)
23
18
  end
24
19
  end
25
20
 
26
- def truncation
27
- return unless object.rows.length > rows.length
21
+ def format_pair(key, value, **options)
22
+ formatted_value = format_nested(value, max_width: max_value_width, **options)
28
23
 
29
- [
30
- object.runner,
31
- light_black("(#{object.rows.length - rows.length} more)")
32
- ].join
24
+ formatted_value.lines.map.with_index do |line, index|
25
+ [
26
+ object.runner,
27
+ text_or_blank(rjust(format_key(key), key_width), blank: index > 0),
28
+ text_or_blank(object.after_key, blank: index > 0),
29
+ line.chomp
30
+ ].join
31
+ end
33
32
  end
34
33
 
34
+ def text_or_blank(text, blank:)
35
+ return text unless blank
36
+
37
+ ' ' * length_of(text)
38
+ end
35
39
 
36
40
  def key_width
37
41
  @key_width ||= rows
@@ -50,26 +54,29 @@ module Helium
50
54
  end
51
55
 
52
56
  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
57
+ ' ' * (width - length_of(string)) + string
62
58
  end
63
59
 
64
60
  def rows
65
61
  @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)
62
+ when 1 then rows_limited_by(42)
63
+ when 2 then rows_limited_by(10)
64
+ else rows_limited_by(3)
71
65
  end
72
66
  end
67
+
68
+ def rows_limited_by(number)
69
+ object.rows.count <= number ? object.rows : object.rows.first(number - 1)
70
+ end
71
+
72
+ def truncation
73
+ return unless object.rows.length > rows.length
74
+
75
+ [
76
+ object.runner,
77
+ light_black("(#{object.rows.length - rows.length} more)")
78
+ ].join
79
+ end
73
80
  end
74
81
  end
75
82
  end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Helium
4
+ class Console
5
+ define_formatter_for Time do
6
+ def call
7
+ blue object.strftime('%A, %d %b %Y, %H:%M:%S')
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'colorized_string'
2
4
 
3
5
  module Helium
@@ -9,6 +11,9 @@ module Helium
9
11
  @options = options
10
12
  end
11
13
 
14
+ def call
15
+ end
16
+
12
17
  attr_reader :object, :options
13
18
 
14
19
  def format_nested(other_object, **options)
@@ -23,28 +28,31 @@ module Helium
23
28
  Helium::Console.format_string(string, **options)
24
29
  end
25
30
 
26
- def simple?(object)
27
- Helium::Console.simple?(object)
28
- end
29
-
30
- def is_simple
31
+ def simple?
31
32
  false
32
33
  end
33
34
 
34
35
  def method_missing(name, *args)
35
36
  return @options[name] if @options.key?(name)
36
- if ColorizedString.colors.include?(name)
37
- return ColorizedString.new(*args).colorize(name)
38
- end
37
+ return ColorizedString.new(*args).colorize(name) if ColorizedString.colors.include?(name)
38
+
39
39
  super
40
40
  end
41
41
 
42
+ def respond_to_missing?(name, private = false)
43
+ @options.key?(name) || ColorizedString.colors.include?(name) || super
44
+ end
45
+
42
46
  def nested_opts(new_options, increase_level: true)
43
47
  new_options = options.merge(new_options)
44
48
  new_options[:level] += 1 if increase_level
45
49
  new_options[:ignore_objects] << object.object_id
46
50
  new_options
47
51
  end
52
+
53
+ def length_of(string)
54
+ ColorizedString.new(string).uncolorize.length
55
+ end
48
56
  end
49
57
 
50
58
  def add(klass, &handler)
@@ -58,13 +66,16 @@ module Helium
58
66
  end
59
67
 
60
68
  def handler_for(object, **options)
61
- object.class.ancestors.each do |ancestor|
62
- return handlers[ancestor].new(object, **options) if handlers.key?(ancestor)
69
+ element_class = object.class.ancestors.find do |ancestor|
70
+ break handlers[ancestor] if handlers.key?(ancestor)
71
+ break handlers[ancestor.name] if handlers.key?(ancestor.name)
63
72
  end
64
- nil
73
+ return unless element_class
74
+
75
+ element_class.new(object, **options)
65
76
  end
66
77
 
67
- private
78
+ private
68
79
 
69
80
  def handlers
70
81
  @handlers ||= {}
@@ -1,8 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Helium
2
4
  class Console
3
5
  class Table
4
-
5
- def initialize(runner: "|", after_key: " ", format_keys: true)
6
+ def initialize(runner: '|', after_key: ' ', format_keys: true)
6
7
  @runner = runner
7
8
  @after_key = after_key
8
9
  @format_keys = format_keys
@@ -10,8 +11,8 @@ module Helium
10
11
 
11
12
  attr_reader :runner, :after_key, :format_keys
12
13
 
13
- def row(key, value)
14
- rows << [key, value]
14
+ def row(key, value, **options)
15
+ rows << [key, value, options]
15
16
  end
16
17
 
17
18
  def rows
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Helium
2
4
  class Console
3
- VERSION = "0.1.6"
5
+ VERSION = '0.1.10'
4
6
  end
5
7
  end
@@ -1,13 +1,15 @@
1
- require "pry"
1
+ # frozen_string_literal: true
2
2
 
3
- require "helium/console/version"
4
- require "helium/console/formatters/indent"
5
- require "helium/console/formatters/overflow"
6
- require "helium/console/formatters/max_lines"
7
- require "helium/console/table"
8
- require "helium/console/registry"
3
+ require 'pry'
9
4
 
10
- require "helium/console/printer"
5
+ require 'helium/console/version'
6
+ require 'helium/console/formatters/indent'
7
+ require 'helium/console/formatters/overflow'
8
+ require 'helium/console/formatters/max_lines'
9
+ require 'helium/console/table'
10
+ require 'helium/console/registry'
11
+
12
+ require 'helium/console/printer'
11
13
 
12
14
  module Helium
13
15
  class Console
@@ -19,7 +21,7 @@ module Helium
19
21
  FalseClass,
20
22
  TrueClass,
21
23
  Symbol
22
- ]
24
+ ].freeze
23
25
 
24
26
  class << self
25
27
  def instance
@@ -31,6 +33,10 @@ module Helium
31
33
 
32
34
  instance.public_send(name, *args, &block)
33
35
  end
36
+
37
+ def respond_to_missing?(name, private = false)
38
+ instance.respond_to?(name) || super
39
+ end
34
40
  end
35
41
 
36
42
  def initialize(registry:)
@@ -39,12 +45,12 @@ module Helium
39
45
 
40
46
  def format(object, **options)
41
47
  options = default_options.merge(options)
42
- return "(...)" if options[:ignore_objects].include?(object.object_id)
48
+ return '(...)' if options[:ignore_objects].include?(object.object_id)
43
49
 
44
50
  handler = registry.handler_for(object, **options)
45
51
 
46
52
  if handler
47
- handler.call
53
+ handler.()
48
54
  else
49
55
  format(object.inspect, **options)
50
56
  end
@@ -59,8 +65,8 @@ module Helium
59
65
  end
60
66
 
61
67
  def simple?(object)
62
- SIMPLE_OBJECTS.any? {|simple_obj_class| object.is_a? simple_obj_class } ||
63
- registry.handler_for(object).is_simple
68
+ SIMPLE_OBJECTS.any? { |simple_obj_class| object.is_a? simple_obj_class } ||
69
+ registry.handler_for(object).simple?
64
70
  end
65
71
 
66
72
  def default_options
@@ -69,11 +75,11 @@ module Helium
69
75
  indent: 0,
70
76
  max_width: `tput cols`.chomp.to_i,
71
77
  level: 1,
72
- ignore_objects: [],
78
+ ignore_objects: []
73
79
  }
74
80
  end
75
81
 
76
- def format_string(string, ellipses: "...", **options)
82
+ def format_string(string, ellipses: '...', **options)
77
83
  options = default_options.merge(options)
78
84
 
79
85
  formatters = [
@@ -86,17 +92,17 @@ module Helium
86
92
  )
87
93
  ]
88
94
 
89
- formatters.inject(string) do |string, formatter|
90
- formatter.call(string)
95
+ formatters.inject(string) do |str, formatter|
96
+ formatter.(str)
91
97
  end
92
98
  end
93
99
 
94
- private
100
+ private
95
101
 
96
102
  attr_reader :registry
97
103
  end
98
104
  end
99
105
 
100
- Dir.glob(File.join(File.dirname(__FILE__), "console", "registry", "**/*.rb")).each do |file|
106
+ Dir.glob(File.join(File.dirname(__FILE__), 'console', 'registry', '**/*.rb')).sort.each do |file|
101
107
  require file
102
108
  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.6
4
+ version: 0.1.10
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-07-09 00:00:00.000000000 Z
11
+ date: 2021-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: pry
14
+ name: colorize
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: colorize
28
+ name: pry
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -47,6 +47,7 @@ extra_rdoc_files: []
47
47
  files:
48
48
  - ".gitignore"
49
49
  - ".rspec"
50
+ - ".rubocop.yml"
50
51
  - ".travis.yml"
51
52
  - CODE_OF_CONDUCT.md
52
53
  - Gemfile
@@ -66,14 +67,18 @@ files:
66
67
  - lib/helium/console/registry.rb
67
68
  - lib/helium/console/registry/array.rb
68
69
  - lib/helium/console/registry/booleans.rb
70
+ - lib/helium/console/registry/date.rb
69
71
  - lib/helium/console/registry/hash.rb
70
72
  - lib/helium/console/registry/module.rb
71
73
  - lib/helium/console/registry/nil.rb
72
74
  - lib/helium/console/registry/numeric.rb
73
75
  - lib/helium/console/registry/object.rb
76
+ - lib/helium/console/registry/pathname.rb
77
+ - lib/helium/console/registry/set.rb
74
78
  - lib/helium/console/registry/string.rb
75
79
  - lib/helium/console/registry/symbol.rb
76
80
  - lib/helium/console/registry/table.rb
81
+ - lib/helium/console/registry/time.rb
77
82
  - lib/helium/console/table.rb
78
83
  - lib/helium/console/version.rb
79
84
  homepage: https://github.com/helium-rb/console
@@ -90,14 +95,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
90
95
  requirements:
91
96
  - - ">="
92
97
  - !ruby/object:Gem::Version
93
- version: 2.3.0
98
+ version: 2.5.0
94
99
  required_rubygems_version: !ruby/object:Gem::Requirement
95
100
  requirements:
96
101
  - - ">="
97
102
  - !ruby/object:Gem::Version
98
103
  version: '0'
99
104
  requirements: []
100
- rubygems_version: 3.0.6
105
+ rubygems_version: 3.1.4
101
106
  signing_key:
102
107
  specification_version: 4
103
108
  summary: Collection of tools for smooth integration with console