From 3a5f0cefb30380680cb0e461d2eea193f3fe4d8f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 23 Jan 2012 19:29:07 -0800 Subject: [PATCH] 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. --- lib/vagrant/communication/ssh.rb | 6 ++++-- lib/vagrant/util/ansi_escape_code_remover.rb | 17 +++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/vagrant/communication/ssh.rb b/lib/vagrant/communication/ssh.rb index f972ec0d2..e7d9fa7aa 100644 --- a/lib/vagrant/communication/ssh.rb +++ b/lib/vagrant/communication/ssh.rb @@ -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 diff --git a/lib/vagrant/util/ansi_escape_code_remover.rb b/lib/vagrant/util/ansi_escape_code_remover.rb index c714b98b5..2fadb6088 100644 --- a/lib/vagrant/util/ansi_escape_code_remover.rb +++ b/lib/vagrant/util/ansi_escape_code_remover.rb @@ -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, "")