Module: Rjq::Color

Defined in:
lib/rjq/color.rb

Constant Summary collapse

DEFAULT =
['1;30', '0;39', '0;39', '0;39', '0;32', '1;39', '1;39', '1;34'].freeze

Class Method Summary collapse

Class Method Details

.color_for(token, colors, object_key: false) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rjq/color.rb', line 20

def color_for(token, colors, object_key: false)
  case token
  when 'null' then colors[0]
  when 'false' then colors[1]
  when 'true' then colors[2]
  when /\A-?\d/ then colors[3]
  when /\A"/ then object_key ? colors[7] : colors[4]
  when '[', ']' then colors[5]
  when '{', '}' then colors[6]
  end
end

.colorize(json) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/rjq/color.rb', line 9

def colorize(json)
  colors = (ENV['RJQ_COLORS'] || ENV.fetch('JQ_COLORS', nil)).to_s.split(':')
  colors = DEFAULT unless valid_colors?(colors)
  json.gsub(/"(?:\\.|[^"\\])*"|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?|\b(?:null|true|false)\b|[\[\]{}]/) do |token|
    remainder = json[Regexp.last_match.end(0)..].to_s
    object_key = token.start_with?('"') && remainder.match?(/\A\s*:/)
    color = color_for(token, colors, object_key: object_key)
    color ? "\e[#{color}m#{token}\e[0m" : token
  end
end

.valid_colors?(colors) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/rjq/color.rb', line 32

def valid_colors?(colors)
  colors.length >= 8 && colors.first(8).all? { |color| color.match?(/\A\d{1,3}(?:;\d{1,3})*\z/) }
end