Module: RASN2::Helpers::Colorize

Included in:
Model, Types::Base
Defined in:
lib/rasn2/helpers/colorize.rb

Instance Method Summary collapse

Instance Method Details

#begin_colorizer(color) ⇒ Object



15
16
17
18
19
# File 'lib/rasn2/helpers/colorize.rb', line 15

def begin_colorizer(color)
  @colorizer ||= []
  @colorizer << Rainbow.new
  @colorizer.last.enabled = color
end

#brace_surround(input) ⇒ Object



52
53
54
# File 'lib/rasn2/helpers/colorize.rb', line 52

def brace_surround(input)
  "#{colorize('[').webgray} #{input} #{colorize(']').webgray}"
end

#colorize(msg) ⇒ Object



21
22
23
# File 'lib/rasn2/helpers/colorize.rb', line 21

def colorize(msg)
  colorizer.wrap(msg)
end

#colorize_attribute(attribute) ⇒ Object



86
87
88
# File 'lib/rasn2/helpers/colorize.rb', line 86

def colorize_attribute(attribute)
  colorize(attribute).bold.magenta
end

#colorize_bool(value, raw_value) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/rasn2/helpers/colorize.rb', line 107

def colorize_bool(value, raw_value)
  hex_raw_value = raw_value.is_a?(String) ? "0x#{raw_value.unpack1('H*').upcase}" : raw_value
  value = if value
            colorize('TRUE').bold.green
          else
            colorize('FALSE').bold.red
          end

  "#{value} #{parens_hex(hex_raw_value)}"
end

#colorize_class(name, tag_id = nil, *attributes) ⇒ Object



56
57
58
59
60
61
# File 'lib/rasn2/helpers/colorize.rb', line 56

def colorize_class(name, tag_id=nil, *attributes)
  parts = [colorize(name).bold.yellow]
  parts += attributes.map { |attr| colorize_attribute(attr) } if attributes.any?
  parts << parens_hex(tag_id) if tag_id
  parts.join(' ')
end

#colorize_default(value) ⇒ Object



90
91
92
# File 'lib/rasn2/helpers/colorize.rb', line 90

def colorize_default(value)
  " #{colorize_attribute('DEFAULT VALUE')} #{colorize(value).green.bold}"
end

#colorize_enum(name, raw_value) ⇒ Object



98
99
100
101
# File 'lib/rasn2/helpers/colorize.rb', line 98

def colorize_enum(name, raw_value)
  raw_value = "0x#{raw_value.unpack1('H*').upcase}" if raw_value.is_a?(String)
  "#{colorize_class(name)} #{parens_hex(raw_value)}"
end

#colorize_id(id, tag_class = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rasn2/helpers/colorize.rb', line 63

def colorize_id(id, tag_class=nil)
  id = id.strip if id.is_a? String
  tag_class = tag_class.strip if tag_class.is_a? String
  tag_class = '' if tag_class == 'UNIVERSAL'

  return '' if id.to_s == '' && tag_class.to_s == ''

  if tag_class && tag_class != ''
    brace_surround("#{colorize_class(tag_class)} #{colorize(id).bold.green}")
  else
    brace_surround(colorize(id).bold.green)
  end
end

#colorize_name(name) ⇒ Object



82
83
84
# File 'lib/rasn2/helpers/colorize.rb', line 82

def colorize_name(name)
  colorize(name).bold.white
end

#colorize_nil(name = 'NONE') ⇒ Object



94
95
96
# File 'lib/rasn2/helpers/colorize.rb', line 94

def colorize_nil(name='NONE')
  colorize(name).red.bold
end

#colorize_value(value) ⇒ Object



103
104
105
# File 'lib/rasn2/helpers/colorize.rb', line 103

def colorize_value(value)
  colorize(value).blue
end

#colorizerObject



6
7
8
# File 'lib/rasn2/helpers/colorize.rb', line 6

def colorizer
  @colorizer&.first || Rainbow.new
end

#colorizer=(colorizer) ⇒ Object



10
11
12
13
# File 'lib/rasn2/helpers/colorize.rb', line 10

def colorizer=(colorizer)
  @colorizer ||= []
  @colorizer << colorizer
end

#end_colorizerObject



25
26
27
# File 'lib/rasn2/helpers/colorize.rb', line 25

def end_colorizer
  @colorizer&.pop
end

#int_with_hex(input, raw_value = nil) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/rasn2/helpers/colorize.rb', line 44

def int_with_hex(input, raw_value=nil)
  if raw_value.nil?
    "#{colorize(input).blue} #{parens_hex(input)}"
  else
    "#{colorize(input).blue} #{parens_hex(raw_value)}"
  end
end

#length_specifier(data_length, encoded_length = nil) ⇒ Object



77
78
79
80
# File 'lib/rasn2/helpers/colorize.rb', line 77

def length_specifier(data_length, encoded_length=nil)
  encoded_length = data_length if encoded_length.nil?
  "#{colorize('len:').gray} #{colorize(data_length).blue} #{parens_hex(encoded_length)}"
end

#parens_hex(input, padding = 2) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rasn2/helpers/colorize.rb', line 29

def parens_hex(input, padding=2)
  value = if input.is_a?(Integer)
            if input >= 0
              value = "%0#{padding}X" % input
              value = value.rjust((value.length + 1) / 2 * 2, '0')
              "0x#{value}"
            else
              '0xFF'
            end
          else
            input
          end
  "#{colorize('(').webgray}#{colorize(value).blue}#{colorize(')').webgray}"
end