Class: Sai::ANSI::ColorParser Private

Inherits:
Object
  • Object
show all
Defined in:
lib/sai/ansi/color_parser.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Handles parsing of ANSI color codes

Author:

Since:

  • 0.3.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(segment) ⇒ ColorParser

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a new instance of ColorParser

Parameters:

  • segment (Hash) —

    the segment to update

Author:

Since:

  • 0.3.1



33
34
35
# File 'lib/sai/ansi/color_parser.rb', line 33

def initialize(segment)
  @segment = segment
end

Instance Attribute Details

#segment ⇒ Hash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The current segment being processed

Returns:

  • (Hash) —

    the current segment being processed

Author:

Since:

  • 0.3.1



20
21
22
# File 'lib/sai/ansi/color_parser.rb', line 20

def segment
  @segment
end

Instance Method Details

#parse256(codes, index) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse a 256-color code

Parameters:

  • codes (Array<Integer>) —

    array of color codes

  • index (Integer) —

    current position in array

Returns:

  • (Integer) —

    new position in array

Author:

Since:

  • 0.3.1



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sai/ansi/color_parser.rb', line 49

def parse256(codes, index)
  base_code = codes[index]
  color_number = codes[index + 2]

  if base_code == 38
    segment[:foreground] = "38;5;#{color_number}"
  else
    segment[:background] = "48;5;#{color_number}"
  end

  index + 3
end

#parse_24bit(codes, index) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse a 24-bit color code

Parameters:

  • codes (Array<Integer>) —

    array of color codes

  • index (Integer) —

    current position in array

Returns:

  • (Integer) —

    new position in array

Author:

Since:

  • 0.3.1



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/sai/ansi/color_parser.rb', line 74

def parse_24bit(codes, index)
  base_code = codes[index]
  r = codes[index + 2]
  g = codes[index + 3]
  b = codes[index + 4]

  if base_code == 38
    segment[:foreground] = "38;2;#{r};#{g};#{b}"
  else
    segment[:background] = "48;2;#{r};#{g};#{b}"
  end

  index + 5
end

#parse_basic(code) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parse a basic color code

Parameters:

  • code (Integer) —

    the color code

Author:

Since:

  • 0.3.1



100
101
102
103
104
105
106
# File 'lib/sai/ansi/color_parser.rb', line 100

def parse_basic(code)
  if (30..37).cover?(code)
    segment[:foreground] = code.to_s
  else # 40..47
    segment[:background] = code.to_s
  end
end