"Fossies" - the Fresh Open Source Software Archive

Member "vagrant-2.2.14/lib/vagrant/util/ansi_escape_code_remover.rb" (20 Nov 2020, 1301 Bytes) of package /linux/misc/vagrant-2.2.14.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "ansi_escape_code_remover.rb": 2.2.9_vs_2.2.10.

    1 module Vagrant
    2   module Util
    3     module ANSIEscapeCodeRemover
    4       # Removes ANSI escape code sequences from the text and returns
    5       # it.
    6       #
    7       # This removes all the ANSI escape codes listed here along with
    8       # the escape codes for VT100 terminals:
    9       #
   10       # http://ascii-table.com/ansi-escape-sequences.php
   11       def remove_ansi_escape_codes(text)
   12         # An array of regular expressions which match various kinds
   13         # of escape sequences. I can't think of a better single regular
   14         # expression or any faster way to do this.
   15         matchers = [/\e\[\d*[ABCD]/,       # Matches things like \e[4D
   16                     /\e\[(\d*;)?\d*[HF]/,  # Matches \e[1;2H or \e[H
   17                     /\e\[(s|u|2J|K)/,      # Matches \e[s, \e[2J, etc.
   18                     /\e\[=\d*[hl]/,        # Matches \e[=24h
   19                     /\e\[\?[1-9][hl]/,     # Matches \e[?2h
   20                     /\e\[20[hl]/,          # Matches \e[20l]
   21                     /\e[DME78H]/,          # Matches \eD, \eH, etc.
   22                     /\e\[[0-3]?[JK]/,      # Matches \e[0J, \e[K, etc.
   23                     ]
   24 
   25         # Take each matcher and replace it with emptiness.
   26         matchers.each do |matcher|
   27           text.gsub!(matcher, "")
   28         end
   29 
   30         text
   31       end
   32     end
   33   end
   34 end