Class: Helium::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/helium/console.rb,
lib/helium/console/table.rb,
lib/helium/console/printer.rb,
lib/helium/console/version.rb,
lib/helium/console/registry.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/table.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/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, Registry, Table

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry:) ⇒ Console

Returns a new instance of Console.



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

def initialize(registry:)
  @registry = registry
end

Class Method Details

.instanceObject



27
28
29
# File 'lib/helium/console.rb', line 27

def instance
  @instance ||= new(registry: Registry.new)
end

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



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

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

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

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

Returns:

  • (Boolean)


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

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

Instance Method Details

#default_optionsObject



72
73
74
75
76
77
78
79
80
# File 'lib/helium/console.rb', line 72

def default_options
  {
    overflow: :wrap,
    indent: 0,
    max_width: `tput cols`.chomp.to_i,
    level: 1,
    ignore_objects: []
  }
end

#define_formatter_for(klass, &handler) ⇒ Object



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

def define_formatter_for(klass, &handler)
  registry.define(klass, &handler)
end

#format(object, **options) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/helium/console.rb', line 46

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

  handler = registry.handler_for(object, **options)

  if handler
    handler.()
  else
    format(object.inspect, **options)
  end
end

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/helium/console.rb', line 82

def format_string(string, ellipses: '...', **options)
  options = default_options.merge(options)

  formatters = [
    Formatters::Overflow.get(options[:overflow]).new(max_width: options[:max_width] - options[: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

#register(klass, &handler) ⇒ Object



59
60
61
# File 'lib/helium/console.rb', line 59

def register(klass, &handler)
  registry.add(klass, &handler)
end

#simple?(object) ⇒ Boolean

Returns:

  • (Boolean)


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

def simple?(object)
  SIMPLE_OBJECTS.any? { |simple_obj_class| object.is_a? simple_obj_class } ||
    registry.handler_for(object).simple?
end