Class: EscapeCode::Code

Inherits:
Object
  • Object
show all
Defined in:
lib/escape_code/code.rb

Constant Summary collapse

REGEX =
/\e\[([0-9;]*)([a-zA-Z])/
SGR =
'm'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, args) ⇒ Code

Returns a new instance of Code.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/escape_code/code.rb', line 10

def initialize(type, args)
  @type = type
  @args = args

  if sgr?
    @sgr_commands ||= begin
      if args == []
        # SGR without an argument is equvalent to reset
        [EscapeCode::SgrCommand.new(SgrCommand::RESET)]
      else
        args.map do |arg|
          EscapeCode::SgrCommand.new(arg)
        end
      end
    end
  end
end

Instance Attribute Details

#argsObject (readonly)

TODO: Support other types of escape codes, like private sequences and non-alphanumeric mode characters



8
9
10
# File 'lib/escape_code/code.rb', line 8

def args
  @args
end

#sgr_commandsObject (readonly)

TODO: Support other types of escape codes, like private sequences and non-alphanumeric mode characters



8
9
10
# File 'lib/escape_code/code.rb', line 8

def sgr_commands
  @sgr_commands
end

#typeObject (readonly)

TODO: Support other types of escape codes, like private sequences and non-alphanumeric mode characters



8
9
10
# File 'lib/escape_code/code.rb', line 8

def type
  @type
end

Class Method Details

.parse(code) ⇒ Object



28
29
30
31
# File 'lib/escape_code/code.rb', line 28

def self.parse(code)
  raise 'not a valid escape sequence' unless code =~ REGEX
  new($~[2], $~[1].split(';'))
end

Instance Method Details

#sgr?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/escape_code/code.rb', line 33

def sgr?
  type == SGR
end