Remove ANSI escape codes from SSH output

Note that the ANSI escape code removal is not complete,
but is fairly comprehensive in terms of the codes that
really muck with the terminal layout.
This commit is contained in:
Mitchell Hashimoto 2012-01-23 19:29:07 -08:00
parent d1e78f791d
commit 3a5f0cefb3
2 changed files with 13 additions and 10 deletions

View File

@ -4,6 +4,7 @@ require 'log4r'
require 'net/ssh'
require 'net/scp'
require 'vagrant/util/ansi_escape_code_remover'
require 'vagrant/util/file_mode'
require 'vagrant/util/platform'
require 'vagrant/util/retryable'
@ -12,6 +13,7 @@ module Vagrant
module Communication
# Provides communication with the VM via SSH.
class SSH < Base
include Util::ANSIEscapeCodeRemover
include Util::Retryable
def initialize(vm)
@ -169,7 +171,7 @@ module Vagrant
ch2.on_data do |ch3, data|
if block_given?
# Filter out the clear screen command
data.gsub!("\e[H", "")
data = remove_ansi_escape_codes(data)
@logger.debug("stdout: #{data}")
yield :stdout, data
end
@ -178,7 +180,7 @@ module Vagrant
ch2.on_extended_data do |ch3, type, data|
if block_given?
# Filter out the clear screen command
data.gsub!("\e[H", "")
data = remove_ansi_escape_codes(data)
@logger.debug("stderr: #{data}")
yield :stderr, data
end

View File

@ -12,16 +12,17 @@ module Vagrant
# 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.
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*;){0,2}\d*m/, # Matches color escapes: \e[32m
/\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-2]?[JK]/, # Matches \e[0J, \e[K, 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-2]?[JK]/, # Matches \e[0J, \e[K, etc.
]
# Take each matcher and replace it with emptiness.
matchers.each do |matcher|
text.gsub!(matcher, "")