Class: REPLUtils::ColorPrinter

Inherits:
PP show all
Defined in:
opal/stdlib/opal-replutils.rb

Overview

Slightly based on Pry's implementation

Constant Summary collapse

TOKEN_COLORS =

Taken from CodeRay

{
  debug: "\e[1;37;44m",

  annotation: "\e[34m",
  attribute_name: "\e[35m",
  attribute_value: "\e[31m",
  binary: {
    self: "\e[31m",
    char: "\e[1;31m",
    delimiter: "\e[1;31m",
  },
  char: {
    self: "\e[35m",
    delimiter: "\e[1;35m"
  },
  class: "\e[1;35;4m",
  class_variable: "\e[36m",
  color: "\e[32m",
  comment: {
    self: "\e[1;30m",
    char: "\e[37m",
    delimiter: "\e[37m",
  },
  constant: "\e[1;34;4m",
  decorator: "\e[35m",
  definition: "\e[1;33m",
  directive: "\e[33m",
  docstring: "\e[31m",
  doctype: "\e[1;34m",
  done: "\e[1;30;2m",
  entity: "\e[31m",
  error: "\e[1;37;41m",
  exception: "\e[1;31m",
  float: "\e[1;35m",
  function: "\e[1;34m",
  global_variable: "\e[1;32m",
  hex: "\e[1;36m",
  id: "\e[1;34m",
  include: "\e[31m",
  integer: "\e[1;34m",
  imaginary: "\e[1;34m",
  important: "\e[1;31m",
  key: {
    self: "\e[35m",
    char: "\e[1;35m",
    delimiter: "\e[1;35m",
  },
  keyword: "\e[32m",
  label: "\e[1;33m",
  local_variable: "\e[33m",
  namespace: "\e[1;35m",
  octal: "\e[1;34m",
  predefined: "\e[36m",
  predefined_constant: "\e[1;36m",
  predefined_type: "\e[1;32m",
  preprocessor: "\e[1;36m",
  pseudo_class: "\e[1;34m",
  regexp: {
    self: "\e[35m",
    delimiter: "\e[1;35m",
    modifier: "\e[35m",
    char: "\e[1;35m",
  },
  reserved: "\e[32m",
  shell: {
    self: "\e[33m",
    char: "\e[1;33m",
    delimiter: "\e[1;33m",
    escape: "\e[1;33m",
  },
  string: {
    self: "\e[31m",
    modifier: "\e[1;31m",
    char: "\e[1;35m",
    delimiter: "\e[1;31m",
    escape: "\e[1;31m",
  },
  symbol: {
    self: "\e[33m",
    delimiter: "\e[1;33m",
  },
  tag: "\e[32m",
  type: "\e[1;34m",
  value: "\e[36m",
  variable: "\e[34m",

  insert: {
    self: "\e[42m",
    insert: "\e[1;32;42m",
    eyecatcher: "\e[102m",
  },
  delete: {
    self: "\e[41m",
    delete: "\e[1;31;41m",
    eyecatcher: "\e[101m",
  },
  change: {
    self: "\e[44m",
    change: "\e[37;44m",
  },
  head: {
    self: "\e[45m",
    filename: "\e[37;45m",
  },

  reset: "\e[0m",
}
NUMBER =
'[+-]?(?:0x[0-9a-fA-F]+|[0-9.]+(?:e[+-][0-9]+|i)?)'
REGEXP =
'/.*?/[iesu]*'
STRING =
'".*?"'
TOKEN_REGEXP =
/(\s+|=>|[@$:]?[a-z]\w+|[A-Z]\w+|#{NUMBER}|#{REGEXP}|#{STRING}|#<.*?[> ]|.)/

Instance Attribute Summary

Attributes inherited from PrettyPrint

#genspace, #group_queue, #indent, #maxwidth, #newline, #output

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PP

mcall, singleline_pp

Methods included from PP::PPMethods

#check_inspect_key, #comma_breakable, #guard_inspect_key, #object_address_group, #object_group, #pop_inspect_key, #pp, #pp_hash, #pp_object, #push_inspect_key, #seplist

Methods inherited from PrettyPrint

#break_outmost_groups, #breakable, #current_group, #fill_breakable, #flush, format, #group, #group_sub, #initialize, #nest, singleline_format

Constructor Details

This class inherits a constructor from PrettyPrint

Class Method Details

.colorize(str) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'opal/stdlib/opal-replutils.rb', line 255

def self.colorize(str)
  tokens = tokenize(str)

  tokens.map do |tok|
    case tok
    when /^[0-9+-]/
      if /[.e]/ =~ tok
        token(tok, :float)
      else
        token(tok, :integer)
      end
    when /^"/
      token(tok, :string, :self)
    when /^:/
      token(tok, :symbol, :self)
    when /^[A-Z]/
      token(tok, :constant)
    when '<', '#', /^#</, '=', '>'
      token(tok, :keyword)
    when /^\/./
      token(tok, :regexp, :self)
    when 'true', 'false', 'nil'
      token(tok, :predefined_constant)
    else
      token(tok, :reset)
    end
  end.join
end

.default(obj, width = 79) ⇒ Object



225
226
227
228
229
# File 'opal/stdlib/opal-replutils.rb', line 225

def self.default(obj, width = 79)
  pager = StringIO.new
  pp(obj, pager, width)
  pager.string
end

.pp(obj, output = $DEFAULT_OUTPUT, max_width = 79) ⇒ Object



231
232
233
234
235
236
# File 'opal/stdlib/opal-replutils.rb', line 231

def self.pp(obj, output = $DEFAULT_OUTPUT, max_width = 79)
  queue = ColorPrinter.new(output, max_width, "\n")
  queue.guard_inspect_key { queue.pp(obj) }
  queue.flush
  output << "\n"
end

.token(string, *name) ⇒ Object



242
243
244
# File 'opal/stdlib/opal-replutils.rb', line 242

def self.token(string, *name)
  TOKEN_COLORS.dig(*name) + string + TOKEN_COLORS[:reset]
end

.tokenize(str) ⇒ Object



251
252
253
# File 'opal/stdlib/opal-replutils.rb', line 251

def self.tokenize(str)
  str.scan(TOKEN_REGEXP).map(&:first)
end

Instance Method Details

#text(str, max_width = str.length) ⇒ Object



238
239
240
# File 'opal/stdlib/opal-replutils.rb', line 238

def text(str, max_width = str.length)
  super(ColorPrinter.colorize(str), max_width)
end