Class: CLAide::ANSI::StringEscaper

Inherits:
String
  • Object
show all
Defined in:
lib/claide/ansi/string_escaper.rb

Overview

Provides support to wrap strings in ANSI sequences according to the ANSI.disabled setting.

Instance Method Summary collapse

Methods inherited from String

#ansi

Constructor Details

#initialize(string) ⇒ StringEscaper

Returns a new instance of StringEscaper.

Parameters:

  • string (String)

    The string to wrap.



9
10
11
# File 'lib/claide/ansi/string_escaper.rb', line 9

def initialize(string)
  super
end

Instance Method Details

#apply(*keys) ⇒ StringEscaper

Parameters:

  • keys (Array<Symbol>)

    One or more keys corresponding to ANSI codes to apply to the string.

Returns:



33
34
35
36
37
38
# File 'lib/claide/ansi/string_escaper.rb', line 33

def apply(*keys)
  keys.flatten.each do |key|
    send(key)
  end
  self
end

#wrap_in_ansi_sequence(open, close) ⇒ StringEscaper

Returns Wraps a string in the given ANSI sequences, taking care of handling existing sequences for the same family of attributes (i.e. attributes terminated by the same sequence).

Returns:

  • (StringEscaper)

    Wraps a string in the given ANSI sequences, taking care of handling existing sequences for the same family of attributes (i.e. attributes terminated by the same sequence).



18
19
20
21
22
23
24
25
# File 'lib/claide/ansi/string_escaper.rb', line 18

def wrap_in_ansi_sequence(open, close)
  if ANSI.disabled
    self
  else
    gsub!(close, open)
    insert(0, open).insert(-1, close)
  end
end