Module: Trith::Shell::Inspect

Defined in:
lib/trith/shell/inspect.rb

Defined Under Namespace

Modules: Color

Class Method Summary (collapse)

Class Method Details

+ (String) colorize(text, color)

Parameters:

  • (String, #to_s) text
  • (Symbol, #to_s) color

Returns:

  • (String)


36
37
38
# File 'lib/trith/shell/inspect.rb', line 36

def self.colorize(text, color)
  !color ? text.to_s : [(Color.const_get(color.to_s.upcase) rescue nil), text.to_s, Color::NOTHING].join('')
end

+ (String) inspect(value, options = {})

Parameters:

  • (Object) value
  • (Hash{Symbol => Object}) options (defaults to: {})

Returns:

  • (String)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/trith/shell/inspect.rb', line 44

def self.inspect(value, options = {})
  case value
    when NilClass   then colorize(:nil, options[:color] ? :green : nil)
    when FalseClass then colorize(false, options[:color] ? :green : nil)
    when TrueClass  then colorize(true, options[:color] ? :green : nil)
    when Symbol     then colorize(value.to_s, options[:color] ? :yellow : nil)
    when Numeric    then colorize(value.inspect, options[:color] ? :cyan : nil)
    when String     then inspect_string(value, options)
    when Array      then inspect_array(value, options)
    when RDF::URI   then inspect_reference(value, options)
    when Machine    then inspect_machine(value, options)
    else value.inspect
  end
end

+ (String) inspect_array(array, options = {})

Parameters:

  • (Array, #to_ary) array
  • (Hash{Symbol => Object}) options (defaults to: {})

Returns:

  • (String)


74
75
76
77
78
# File 'lib/trith/shell/inspect.rb', line 74

def self.inspect_array(array, options = {})
  colorize('[', options[:color] ? :green : nil) <<
  array.to_ary.map { |element| inspect(element, options) }.join(' ') <<
  colorize(']', options[:color] ? :green : nil)
end

+ (String) inspect_machine(machine, options = {})

Parameters:

  • (Machine) machine
  • (Hash{Symbol => Object}) options (defaults to: {})

Returns:

  • (String)


63
64
65
66
67
68
# File 'lib/trith/shell/inspect.rb', line 63

def self.inspect_machine(machine, options = {})
  (options[:prefix].to_s || '') +
    (options[:align] ? ' ' * [20 - inspect(machine.stack, :color => false).size, 0].max : '') +
    inspect(machine.stack, options) + ' : ' +
    inspect(machine.queue, options)
end

+ (String) inspect_reference(uri, options = {})

Parameters:

  • (RDF::URI, #to_uri) uri
  • (Hash{Symbol => Object}) options (defaults to: {})

Returns:

  • (String)


94
95
96
97
98
# File 'lib/trith/shell/inspect.rb', line 94

def self.inspect_reference(uri, options = {})
  colorize('<', options[:color] ? :red : nil) <<
  colorize(uri.to_uri.to_s, options[:color] ? :cyan : nil) <<
  colorize('>', options[:color] ? :red : nil)
end

+ (String) inspect_string(string, options = {})

Parameters:

  • (String, #to_str) string
  • (Hash{Symbol => Object}) options (defaults to: {})

Returns:

  • (String)


84
85
86
87
88
# File 'lib/trith/shell/inspect.rb', line 84

def self.inspect_string(string, options = {})
  colorize('"', options[:color] ? :red : nil) <<
  colorize(string.to_str.inspect[1...-1], options[:color] ? :cyan : nil) <<
  colorize('"', options[:color] ? :red : nil)
end