class RainbowHash::Display

Public Class Methods

new(string) click to toggle source
# File lib/rainbow_hash/display.rb, line 9
def initialize(string)
  @string = string
  @color_stack = 30
  @indentation_level = 0
end
print(string) click to toggle source

Public Instance Methods

output() click to toggle source
# File lib/rainbow_hash/display.rb, line 15
def output
  new_string = ""
  @string.each_char do |char|
    if beginning_delimeter?(char)
      increment_level
      new_string += "\n" + (" " * @indentation_level)
    end
    new_string += colorize(char)
    if ending_delimeter?(char)
      decrement_level
      new_string += "\n" + (" " * @indentation_level)
    end
  end
  new_string
end

Private Instance Methods

beginning_delimeter?(char) click to toggle source
# File lib/rainbow_hash/display.rb, line 33
def beginning_delimeter?(char)
  char == "[" || char == "{"
end
colorize(char) click to toggle source
# File lib/rainbow_hash/display.rb, line 41
def colorize(char)
  if beginning_delimeter?(char) || ending_delimeter?(char)
    "\e[#{@color_stack}m\e[1m#{char}\e[0m"
  else
    "\e[#{@color_stack}m#{char}\e[0m"
  end
end
decrement_level() click to toggle source
# File lib/rainbow_hash/display.rb, line 54
def decrement_level
  @color_stack -= 1
  @indentation_level -= 2
end
ending_delimeter?(char) click to toggle source
# File lib/rainbow_hash/display.rb, line 37
def ending_delimeter?(char)
  char == "]" || char == "}"
end
increment_level() click to toggle source
# File lib/rainbow_hash/display.rb, line 49
def increment_level
  @color_stack += 1
  @indentation_level += 2
end