Class: Helium::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/helium/console.rb,
lib/helium/console/prompt.rb,
lib/helium/console/printer.rb,
lib/helium/console/version.rb,
lib/helium/console/registry.rb,
lib/helium/console/formatter.rb,
lib/helium/console/key_value.rb,
lib/helium/console/registry/nil.rb,
lib/helium/console/registry/set.rb,
lib/helium/console/registry/date.rb,
lib/helium/console/registry/hash.rb,
lib/helium/console/registry/time.rb,
lib/helium/console/registry/array.rb,
lib/helium/console/registry/method.rb,
lib/helium/console/registry/module.rb,
lib/helium/console/registry/object.rb,
lib/helium/console/registry/string.rb,
lib/helium/console/registry/symbol.rb,
lib/helium/console/colorized_string.rb,
lib/helium/console/registry/numeric.rb,
lib/helium/console/formatters/indent.rb,
lib/helium/console/registry/booleans.rb,
lib/helium/console/registry/pathname.rb,
lib/helium/console/formatters/overflow.rb,
lib/helium/console/formatters/max_lines.rb,
lib/helium/console/formatters/overflow/wrap.rb

Defined Under Namespace

Modules: Formatters Classes: ColorPrinter, ColorizedString, Formatter, KeyValue, Prompt, Registry

Constant Summary collapse

Error =
Class.new(StandardError)
SIMPLE_OBJECTS =
[
  Numeric,
  NilClass,
  FalseClass,
  TrueClass,
  Symbol
].freeze
VERSION =
'0.1.13'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance_registry: nil, class_registry: nil) ⇒ Console

Returns a new instance of Console.



61
62
63
64
# File 'lib/helium/console.rb', line 61

def initialize(instance_registry: nil, class_registry: nil)
  @instance_registry = instance_registry || Registry.instance_formatters
  @class_registry = class_registry || Registry.class_formatters
end

Class Method Details

.define_class_formatter_for(klass, &handler) ⇒ Object



41
42
43
# File 'lib/helium/console.rb', line 41

def self.define_class_formatter_for(klass, &handler)
  Registry.class_formatters.define(klass, &handler)
end

.define_formatter_for(klass, &handler) ⇒ Object



37
38
39
# File 'lib/helium/console.rb', line 37

def self.define_formatter_for(klass, &handler)
  Registry.instance_formatters.define(klass, &handler)
end

.instanceObject



46
47
48
# File 'lib/helium/console.rb', line 46

def instance
  @instance ||= new
end

.method_missing(name, *args, **keywords, &block) ⇒ Object



50
51
52
53
54
# File 'lib/helium/console.rb', line 50

def method_missing(name, *args, **keywords, &block)
  super unless instance.respond_to?(name)

  instance.public_send(name, *args, **keywords, &block)
end

.respond_to_missing?(name, private = false) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/helium/console.rb', line 56

def respond_to_missing?(name, private = false)
  instance.respond_to?(name) || super
end

.start(target = nil, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/helium/console.rb', line 28

def self.start(target = nil, options = {})
  options = {
    print: ColorPrinter.method(:default),
    prompt: Prompt.new.pry_prompt
  }.merge(options)

  Pry.start(target, options)
end

Instance Method Details

#default_optionsObject



78
79
80
81
82
83
84
85
86
# File 'lib/helium/console.rb', line 78

def default_options
  {
    overflow: :wrap,
    indent: 0,
    level: 1,
    ignore_objects: [],
    short: false
  }
end

#format(object, style = nil, **options) ⇒ Object



66
67
68
69
70
71
# File 'lib/helium/console.rb', line 66

def format(object, style = nil, **options)
  options = default_options.merge(options)
  return '(...)' if options[:ignore_objects].include?(object.object_id)

  handler_for(object, style, **options).()
end

#format_string(string, ellipses: '...', **options) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/helium/console.rb', line 88

def format_string(string, ellipses: '...', **options)
  options = default_options.merge(options)
  max_width_without_indent = (options[:max_width] - options[:indent]) if options[:max_width]
  formatters = [
    Formatters::Overflow.get(options[:overflow]).new(max_width: max_width_without_indent),
    Formatters::Indent.new(options[:indent]),
    Formatters::MaxLines.new(
      max_lines: options[:max_lines],
      max_width: options[:max_width],
      ellipses: ellipses
    )
  ]

  formatters.inject(string) do |str, formatter|
    formatter.(str)
  end
end

#simple?(object, style = nil, **options) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/helium/console.rb', line 73

def simple?(object, style = nil, **options)
  SIMPLE_OBJECTS.any? { |simple_obj_class| object.is_a? simple_obj_class } ||
    handler_for(object, style, **options).simple?
end