helium-console 0.1.9 → 0.1.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,10 +6,12 @@ require 'helium/console/version'
6
6
  require 'helium/console/formatters/indent'
7
7
  require 'helium/console/formatters/overflow'
8
8
  require 'helium/console/formatters/max_lines'
9
- require 'helium/console/table'
9
+ require 'helium/console/colorized_string'
10
+ require 'helium/console/key_value'
10
11
  require 'helium/console/registry'
11
12
 
12
13
  require 'helium/console/printer'
14
+ require 'helium/console/prompt'
13
15
 
14
16
  module Helium
15
17
  class Console
@@ -23,15 +25,32 @@ module Helium
23
25
  Symbol
24
26
  ].freeze
25
27
 
28
+ def self.start(target = nil, options = {})
29
+ options = {
30
+ print: ColorPrinter.method(:default),
31
+ prompt: Prompt.new.pry_prompt
32
+ }.merge(options)
33
+
34
+ Pry.start(target, options)
35
+ end
36
+
37
+ def self.define_formatter_for(klass, &handler)
38
+ Registry.instance_formatters.define(klass, &handler)
39
+ end
40
+
41
+ def self.define_class_formatter_for(klass, &handler)
42
+ Registry.class_formatters.define(klass, &handler)
43
+ end
44
+
26
45
  class << self
27
46
  def instance
28
- @instance ||= new(registry: Registry.new)
47
+ @instance ||= new
29
48
  end
30
49
 
31
- def method_missing(name, *args, &block)
50
+ def method_missing(name, *args, **keywords, &block)
32
51
  super unless instance.respond_to?(name)
33
52
 
34
- instance.public_send(name, *args, &block)
53
+ instance.public_send(name, *args, **keywords, &block)
35
54
  end
36
55
 
37
56
  def respond_to_missing?(name, private = false)
@@ -39,51 +58,38 @@ module Helium
39
58
  end
40
59
  end
41
60
 
42
- def initialize(registry:)
43
- @registry = registry
61
+ def initialize(instance_registry: nil, class_registry: nil)
62
+ @instance_registry = instance_registry || Registry.instance_formatters
63
+ @class_registry = class_registry || Registry.class_formatters
44
64
  end
45
65
 
46
- def format(object, **options)
66
+ def format(object, style = nil, **options)
47
67
  options = default_options.merge(options)
48
68
  return '(...)' if options[:ignore_objects].include?(object.object_id)
49
69
 
50
- handler = registry.handler_for(object, **options)
51
-
52
- if handler
53
- handler.()
54
- else
55
- format(object.inspect, **options)
56
- end
57
- end
58
-
59
- def register(klass, &handler)
60
- registry.add(klass, &handler)
70
+ handler_for(object, style, **options).()
61
71
  end
62
72
 
63
- def define_formatter_for(klass, &handler)
64
- registry.define(klass, &handler)
65
- end
66
-
67
- def simple?(object)
73
+ def simple?(object, style = nil, **options)
68
74
  SIMPLE_OBJECTS.any? { |simple_obj_class| object.is_a? simple_obj_class } ||
69
- registry.handler_for(object).simple?
75
+ handler_for(object, style, **options).simple?
70
76
  end
71
77
 
72
78
  def default_options
73
79
  {
74
80
  overflow: :wrap,
75
81
  indent: 0,
76
- max_width: `tput cols`.chomp.to_i,
77
82
  level: 1,
78
- ignore_objects: []
83
+ ignore_objects: [],
84
+ short: false
79
85
  }
80
86
  end
81
87
 
82
88
  def format_string(string, ellipses: '...', **options)
83
89
  options = default_options.merge(options)
84
-
90
+ max_width_without_indent = (options[:max_width] - options[:indent]) if options[:max_width]
85
91
  formatters = [
86
- Formatters::Overflow.get(options[:overflow]).new(max_width: options[:max_width] - options[:indent]),
92
+ Formatters::Overflow.get(options[:overflow]).new(max_width: max_width_without_indent),
87
93
  Formatters::Indent.new(options[:indent]),
88
94
  Formatters::MaxLines.new(
89
95
  max_lines: options[:max_lines],
@@ -99,7 +105,17 @@ module Helium
99
105
 
100
106
  private
101
107
 
102
- attr_reader :registry
108
+ attr_reader :instance_registry, :class_registry
109
+
110
+ def handler_for(object, style, **options)
111
+ formatter_class = if object.is_a? Module
112
+ class_registry.handler_for(object)
113
+ else
114
+ instance_registry.handler_for(object.class)
115
+ end
116
+
117
+ formatter_class&.new(object, style, self, **options)
118
+ end
103
119
  end
104
120
  end
105
121
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: helium-console
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.1.13
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-08-25 00:00:00.000000000 Z
11
+ date: 2022-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -45,6 +45,7 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
+ - ".github/workflows/pull_request.yml"
48
49
  - ".gitignore"
49
50
  - ".rspec"
50
51
  - ".rubocop.yml"
@@ -59,23 +60,30 @@ files:
59
60
  - bin/setup
60
61
  - helium-console.gemspec
61
62
  - lib/helium/console.rb
63
+ - lib/helium/console/colorized_string.rb
64
+ - lib/helium/console/formatter.rb
62
65
  - lib/helium/console/formatters/indent.rb
63
66
  - lib/helium/console/formatters/max_lines.rb
64
67
  - lib/helium/console/formatters/overflow.rb
65
68
  - lib/helium/console/formatters/overflow/wrap.rb
69
+ - lib/helium/console/key_value.rb
66
70
  - lib/helium/console/printer.rb
71
+ - lib/helium/console/prompt.rb
67
72
  - lib/helium/console/registry.rb
68
73
  - lib/helium/console/registry/array.rb
69
74
  - lib/helium/console/registry/booleans.rb
75
+ - lib/helium/console/registry/date.rb
70
76
  - lib/helium/console/registry/hash.rb
77
+ - lib/helium/console/registry/method.rb
71
78
  - lib/helium/console/registry/module.rb
72
79
  - lib/helium/console/registry/nil.rb
73
80
  - lib/helium/console/registry/numeric.rb
74
81
  - lib/helium/console/registry/object.rb
82
+ - lib/helium/console/registry/pathname.rb
83
+ - lib/helium/console/registry/set.rb
75
84
  - lib/helium/console/registry/string.rb
76
85
  - lib/helium/console/registry/symbol.rb
77
- - lib/helium/console/registry/table.rb
78
- - lib/helium/console/table.rb
86
+ - lib/helium/console/registry/time.rb
79
87
  - lib/helium/console/version.rb
80
88
  homepage: https://github.com/helium-rb/console
81
89
  licenses:
@@ -1,82 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Helium
4
- class Console
5
- define_formatter_for Table do
6
- def call
7
- [
8
- *formatted_values,
9
- truncation
10
- ].compact.join("\n")
11
- end
12
-
13
- private
14
-
15
- def formatted_values
16
- rows.flat_map do |key, value, options = {}|
17
- format_pair(key, value, **options)
18
- end
19
- end
20
-
21
- def format_pair(key, value, **options)
22
- formatted_value = format_nested(value, max_width: max_value_width, **options)
23
-
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
32
- end
33
-
34
- def text_or_blank(text, blank:)
35
- return text unless blank
36
-
37
- ' ' * length_of(text)
38
- end
39
-
40
- def key_width
41
- @key_width ||= rows
42
- .map(&:first)
43
- .map(&method(:format_key))
44
- .map(&method(:length_of))
45
- .max
46
- end
47
-
48
- def max_value_width
49
- max_width - length_of(object.runner) - key_width - length_of(object.after_key)
50
- end
51
-
52
- def format_key(key)
53
- object.format_keys ? format(key, max_width: 15, level: 3) : key
54
- end
55
-
56
- def rjust(string, width)
57
- ' ' * (width - length_of(string)) + string
58
- end
59
-
60
- def rows
61
- @rows ||= case level
62
- when 1 then object.rows
63
- when 2 then rows_limited_by(10)
64
- else rows_limited_by(3)
65
- end
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
80
- end
81
- end
82
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Helium
4
- class Console
5
- class Table
6
- def initialize(runner: '|', after_key: ' ', format_keys: true)
7
- @runner = runner
8
- @after_key = after_key
9
- @format_keys = format_keys
10
- end
11
-
12
- attr_reader :runner, :after_key, :format_keys
13
-
14
- def row(key, value, **options)
15
- rows << [key, value, options]
16
- end
17
-
18
- def rows
19
- @rows ||= []
20
- end
21
- end
22
- end
23
- end