Module: Aruba::Api::Text

Included in:
Aruba::Api
Defined in:
lib/aruba/api/text.rb

Overview

Text manipulation

Instance Method Summary collapse

Instance Method Details

#extract_text(text) ⇒ Object

Remove ansi characters from text

Parameters:

  • text (#to_s)

    Input



31
32
33
34
35
36
# File 'lib/aruba/api/text.rb', line 31

def extract_text(text)
  text
    .gsub(/(?:\e|\033)\[\d+(?>(;\d+)*)m/, "")
    .gsub(/\\\[|\\\]/, "")
    .gsub(/\007|\016|\017/, "")
end

#sanitize_text(text) ⇒ Object

Unescape special characters and remove ANSI characters

Parameters:

  • text (#to_s)

    The text to sanitize



42
43
44
45
46
47
# File 'lib/aruba/api/text.rb', line 42

def sanitize_text(text)
  text = unescape_text(text)
  text = extract_text(text) if aruba.config.remove_ansi_escape_sequences

  text.chomp
end

#unescape_text(text) ⇒ Object

Unescape text

‘n’ => “n” ‘e’ => “e” ‘033’ => “e” ‘"’ => ‘“’

Parameters:

  • text (#to_s)

    Input



16
17
18
19
20
21
22
23
24
25
# File 'lib/aruba/api/text.rb', line 16

def unescape_text(text)
  text
    .gsub('\n', "\n")
    .gsub('\"', '"')
    .gsub('\e', "\e")
    .gsub('\033', "\e")
    .gsub('\016', "\016")
    .gsub('\017', "\017")
    .gsub('\t', "\t")
end