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


24
25
26
# File 'lib/aruba/api/text.rb', line 24

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

#replace_variables(text) ⇒ Object

Replace variables in command string (experimental)

Parameters:

  • text (#to_s)

    The text to parse


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

def replace_variables(text)
  text = text.gsub(/<pid-last-command-started>/, last_command_started.pid.to_s) if text.include? '<pid-last-command-started>'

  text
end

#sanitize_text(text) ⇒ Object

Unescape special characters and remove ANSI characters

Parameters:

  • text (#to_s)

    The text to sanitize


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

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
# 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