class Helium::Console
Constants
- Error
- SIMPLE_OBJECTS
- VERSION
Attributes
registry[R]
Public Class Methods
instance()
click to toggle source
# File lib/helium/console.rb, line 27 def instance @instance ||= new(registry: Registry.new) end
method_missing(name, *args, &block)
click to toggle source
Calls superclass method
# 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
new(registry:)
click to toggle source
# File lib/helium/console.rb, line 42 def initialize(registry:) @registry = registry end
respond_to_missing?(name, private = false)
click to toggle source
Calls superclass method
# File lib/helium/console.rb, line 37 def respond_to_missing?(name, private = false) instance.respond_to?(name) || super end
Public Instance Methods
after_key()
click to toggle source
# File lib/helium/console/registry/hash.rb, line 87 def after_key all_symbol? ? light_blue(': ') : light_black(' => ') end
all_symbol?()
click to toggle source
# File lib/helium/console/registry/hash.rb, line 65 def all_symbol? return false return @all_symbol if defined?(@all_symbol) @all_symbol = object.keys.all? { |key| key.is_a? Symbol } end
anonymous_text()
click to toggle source
# File lib/helium/console/registry/module.rb, line 12 def anonymous_text closest = (object.ancestors.grep(Class) - [Object, BasicObject]).find(&:name)&.name signature = if closest "subclass of #{closest}" else object.class.name.downcase end "(anonymous #{signature})" end
call()
click to toggle source
# File lib/helium/console/registry/array.rb, line 6 def call return '[]' if object.none? return inline_with_truncation if force_inline? inline_without_truncation || format_as_table end
class_name(short: false)
click to toggle source
# File lib/helium/console/registry/object.rb, line 56 def class_name(short: false) formatted = format(object.class, short: short) short ? "##{formatted}" : "#{formatted} instance" end
default_options()
click to toggle source
# 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)
click to toggle source
# File lib/helium/console.rb, line 63 def define_formatter_for(klass, &handler) registry.define(klass, &handler) end
force_inline?()
click to toggle source
# File lib/helium/console/registry/array.rb, line 75 def force_inline? level > 2 end
format(object, **options)
click to toggle source
# 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_as_table()
click to toggle source
# File lib/helium/console/registry/array.rb, line 19 def format_as_table table = Table.new(runner: ' ', format_keys: false) object.each.with_index do |element, index| table.row(light_black("[#{index}]:"), element) end yield_lines do |y| y << '[' format(table).lines.each { |line| y << line } y << ']' end end
format_key(key, **options)
click to toggle source
# File lib/helium/console/registry/hash.rb, line 72 def format_key(key, **options) return light_blue(key.to_s) if all_symbol? format_nested(key, **options) end
format_pair(key, value, **options) { |[ runner, text_or_blank(rjust(format_key(key), key_width), blank: index > 0), text_or_blank(after_key, blank: index > 0), chomp| ... }
click to toggle source
# File lib/helium/console/registry/table.rb, line 19 def format_pair(key, value, **options) formatted_value = format_nested(value, max_width: max_value_width, **options) formatted_value.lines.each.with_index.map do |line, index| yield [ object.runner, text_or_blank(rjust(format_key(key), key_width), blank: index > 0), text_or_blank(object.after_key, blank: index > 0), line.chomp ].join end end
format_string(string, ellipses: '...', **options)
click to toggle source
# 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
formatted_elements(**options)
click to toggle source
# File lib/helium/console/registry/array.rb, line 61 def formatted_elements(**options) object.each.lazy.map { |element| format_nested(element, **options) } end
formatted_inline_elements()
click to toggle source
# File lib/helium/console/registry/hash.rb, line 57 def formatted_inline_elements object.each.lazy.map do |key, value| formatted_key = format_key(key, max_lines: 1, nesting: 1, max_with: 15) formatted_value = format_nested(value, max_lines: 1, nesting: 1, max_width: 15) [formatted_key, after_key, formatted_value].join end end
formatted_instance_variables(**options)
click to toggle source
# File lib/helium/console/registry/object.rb, line 49 def formatted_instance_variables(**options) object.instance_variables.sort.each.lazy.map do |var_name| value = object.instance_variable_get(var_name) "#{magenta(var_name.to_s)} = #{format_nested(value, **options)}" end end
inline_with_truncation()
click to toggle source
# File lib/helium/console/registry/array.rb, line 32 def inline_with_truncation formatted = formatted_elements.with_index.inject([]) do |joined, (element, index)| new_joined = [*joined[0..-2], element, trunc_message(object_size - index - 1, all_truncated: index.zero?)] break joined if too_long?(new_joined, max_width: max_width - 4) new_joined end "[ #{formatted.compact.join(' | ')} ]" end
inline_without_truncation()
click to toggle source
# File lib/helium/console/registry/array.rb, line 43 def inline_without_truncation return unless object.all? { |element| Helium::Console.simple? element } formatted = formatted_elements.inject([]) do |joined, element| joined = [*joined, element] break if too_long?(joined, max_width: max_width - 4) joined end "[ #{formatted.join(' | ')} ]" unless formatted.nil? end
key_width()
click to toggle source
# File lib/helium/console/registry/table.rb, line 38 def key_width @key_width ||= rows .map(&:first) .map(&method(:format_key)) .map(&method(:length_of)) .max end
max_lines()
click to toggle source
# File lib/helium/console/registry/string.rb, line 20 def max_lines case level when 1 then nil when 2 then 3 else 1 end end
max_value_width()
click to toggle source
# File lib/helium/console/registry/table.rb, line 46 def max_value_width max_width - length_of(object.runner) - key_width - length_of(object.after_key) end
object_size()
click to toggle source
# File lib/helium/console/registry/array.rb, line 71 def object_size @object_size ||= object.size end
register(klass, &handler)
click to toggle source
# File lib/helium/console.rb, line 59 def register(klass, &handler) registry.add(klass, &handler) end
rjust(string, width)
click to toggle source
# File lib/helium/console/registry/table.rb, line 54 def rjust(string, width) ' ' * (width - length_of(string)) + string end
rows()
click to toggle source
# File lib/helium/console/registry/table.rb, line 58 def rows @rows ||= case level when 1 then object.rows when 2 then rows_limited_by(10) else rows_limited_by(3) end end
rows_limited_by(number)
click to toggle source
# File lib/helium/console/registry/table.rb, line 66 def rows_limited_by(number) object.rows.count <= number ? object.rows : object.rows.first(number - 1) end
simple?(object)
click to toggle source
# 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
text_or_blank(text, blank:)
click to toggle source
# File lib/helium/console/registry/table.rb, line 32 def text_or_blank(text, blank:) return text unless blank ' ' * length_of(text) end
too_long?(object, max_width:)
click to toggle source
# File lib/helium/console/registry/array.rb, line 56 def too_long?(object, max_width:) string = object.respond_to?(:join) ? object.join(' | ') : object length_of(string) > max_width - 4 end
total_elements()
click to toggle source
# File lib/helium/console/registry/hash.rb, line 78 def total_elements @total_elements ||= object.length end
trunc_message(count, all_truncated: false)
click to toggle source
# File lib/helium/console/registry/array.rb, line 65 def trunc_message(count, all_truncated: false) return if count < 1 "(#{count} #{all_truncated ? 'elements' : 'more'})" end
trunc_text(count)
click to toggle source
# File lib/helium/console/registry/hash.rb, line 82 def trunc_text(count) truncated_elements = total_elements - count light_black("(#{truncated_elements} #{count.zero? ? 'elements' : 'more'})") end
truncation()
click to toggle source
# File lib/helium/console/registry/table.rb, line 70 def truncation return unless object.rows.length > rows.length [ object.runner, light_black("(#{object.rows.length - rows.length} more)"), "\n" ].join end