Class: TTY::Command::Window::ANSI::SGRState

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/command/window/ansi.rb

Overview

Tracks the current SGR (color/attribute) state of the emulator pen and serializes it to a compact parameter string stored per cell.

Constant Summary collapse

RESET_CODES =
{
  22 => %i[bold dim], 23 => %i[italic], 24 => %i[underline],
  25 => %i[blink], 27 => %i[inverse], 28 => %i[hidden], 29 => %i[strike]
}.freeze
SET_CODES =
{
  1 => :bold, 2 => :dim, 3 => :italic, 4 => :underline,
  5 => :blink, 7 => :inverse, 8 => :hidden, 9 => :strike
}.freeze
FLAG_SGR =
{
  bold: "1", dim: "2", italic: "3", underline: "4",
  blink: "5", inverse: "7", hidden: "8", strike: "9"
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeSGRState

Returns a new instance of SGRState.



42
43
44
# File 'lib/tty/command/window/ansi.rb', line 42

def initialize
  reset
end

Instance Method Details

#apply(params) ⇒ Object

Apply a list of SGR parameters (integers, with extended color sub-parameters consumed in place).

Parameters:

  • params (Array<Integer>)


57
58
59
60
61
62
# File 'lib/tty/command/window/ansi.rb', line 57

def apply(params)
  params = [0] if params.empty?
  i = 0
  i += apply_one(params, i) while i < params.length
  @serialized = nil
end

#resetObject



46
47
48
49
50
51
# File 'lib/tty/command/window/ansi.rb', line 46

def reset
  @flags = {}
  @fg = nil
  @bg = nil
  @serialized = nil
end

#to_paramsString?

Returns compact SGR params ("1;38;5;196") or nil when default.

Returns:

  • (String, nil)

    compact SGR params ("1;38;5;196") or nil when default



65
66
67
68
69
70
71
72
73
# File 'lib/tty/command/window/ansi.rb', line 65

def to_params
  if @serialized.nil?
    parts = @flags.keys.map { |flag| FLAG_SGR[flag] }
    parts << @fg if @fg
    parts << @bg if @bg
    @serialized = parts.empty? ? false : parts.join(";").freeze
  end
  @serialized || nil
end