Module: Vagrant::Util::ANSIEscapeCodeRemover

Defined in:
lib/vagrant/util/ansi_escape_code_remover.rb

Instance Method Summary collapse

Instance Method Details

#remove_ansi_escape_codes(text) ⇒ Object

Removes ANSI escape code sequences from the text and returns it.

This removes all the ANSI escape codes listed here along with the escape codes for VT100 terminals:

http://ascii-table.com/ansi-escape-sequences.php



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vagrant/util/ansi_escape_code_remover.rb', line 14

def remove_ansi_escape_codes(text)
  # An array of regular expressions which match various kinds
  # of escape sequences. I can't think of a better single regular
  # expression or any faster way to do this.
  matchers = [/\e\[\d*[ABCD]/,       # Matches things like \e[4D
              /\e\[(\d*;)?\d*[HF]/,  # Matches \e[1;2H or \e[H
              /\e\[(s|u|2J|K)/,      # Matches \e[s, \e[2J, etc.
              /\e\[=\d*[hl]/,        # Matches \e[=24h
              /\e\[\?[1-9][hl]/,     # Matches \e[?2h
              /\e\[20[hl]/,          # Matches \e[20l]
              /\e[DME78H]/,          # Matches \eD, \eH, etc.
              /\e\[[0-3]?[JK]/,      # Matches \e[0J, \e[K, etc.
              ]

  # Take each matcher and replace it with emptiness.
  matchers.each do |matcher|
    text.gsub!(matcher, "")
  end

  text
end