From 2989162d2b22fb7c5dd256cda3c274edb78d3276 Mon Sep 17 00:00:00 2001 From: Dmitry Trofimov Date: Sat, 4 Jan 2014 23:13:51 +0100 Subject: [PATCH] Cut out escapes from vagrant output in message dialogs. --- .../execution/process/AnsiEscapeDecoder.java | 2 +- ...pturingAnsiEscapesAwareProcessHandler.java | 66 +++++++++++++++++++ .../process/CapturingProcessAdapter.java | 8 ++- .../process/CapturingProcessHandler.java | 10 ++- 4 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 platform/platform-api/src/com/intellij/execution/process/CapturingAnsiEscapesAwareProcessHandler.java diff --git a/platform/platform-api/src/com/intellij/execution/process/AnsiEscapeDecoder.java b/platform/platform-api/src/com/intellij/execution/process/AnsiEscapeDecoder.java index 3a45c8d6ee48..0ecf7c8a6dd0 100644 --- a/platform/platform-api/src/com/intellij/execution/process/AnsiEscapeDecoder.java +++ b/platform/platform-api/src/com/intellij/execution/process/AnsiEscapeDecoder.java @@ -84,7 +84,7 @@ public class AnsiEscapeDecoder { return endMacroPos; } - private Key getCurrentOutputAttributes(final Key outputType) { + protected Key getCurrentOutputAttributes(final Key outputType) { if (outputType == ProcessOutputTypes.STDERR || outputType == ProcessOutputTypes.SYSTEM) { return outputType; } diff --git a/platform/platform-api/src/com/intellij/execution/process/CapturingAnsiEscapesAwareProcessHandler.java b/platform/platform-api/src/com/intellij/execution/process/CapturingAnsiEscapesAwareProcessHandler.java new file mode 100644 index 000000000000..c76e79a0bfd6 --- /dev/null +++ b/platform/platform-api/src/com/intellij/execution/process/CapturingAnsiEscapesAwareProcessHandler.java @@ -0,0 +1,66 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.execution.process; + +import com.intellij.execution.ExecutionException; +import com.intellij.execution.configurations.GeneralCommandLine; +import com.intellij.openapi.util.Key; +import org.jetbrains.annotations.NotNull; + +/** + * Capturing process handler that cuts off ansi escapes + * + * @author traff + */ +public class CapturingAnsiEscapesAwareProcessHandler extends CapturingProcessHandler { + public CapturingAnsiEscapesAwareProcessHandler(@NotNull GeneralCommandLine commandLine) + throws ExecutionException { + super(commandLine); + } + + public CapturingAnsiEscapesAwareProcessHandler(Process process) { + super(process); + } + + @Override + protected CapturingProcessAdapter createProcessAdapter(ProcessOutput processOutput) { + return new AnsiEscapesAwareAdapter(processOutput); + } + + + private static class AnsiEscapesAwareAdapter extends CapturingProcessAdapter implements AnsiEscapeDecoder.ColoredTextAcceptor { + private final AnsiEscapeDecoder myAnsiEscapeDecoder = new AnsiEscapeDecoder() { + @Override + protected Key getCurrentOutputAttributes(Key outputType) { + return outputType; //we don't need color information - only stdout and stderr keys are added to output in CapturingProcessAdapter + } + }; + + public AnsiEscapesAwareAdapter(ProcessOutput output) { + super(output); + } + + @Override + public void onTextAvailable(ProcessEvent event, Key outputType) { + myAnsiEscapeDecoder.escapeText(event.getText(), outputType, this); + } + + @Override + public void coloredTextAvailable(String text, Key attributes) { + addToOutput(text, attributes); + } + } +} diff --git a/platform/platform-api/src/com/intellij/execution/process/CapturingProcessAdapter.java b/platform/platform-api/src/com/intellij/execution/process/CapturingProcessAdapter.java index 5bb36f99ed01..b3d4c16679ea 100644 --- a/platform/platform-api/src/com/intellij/execution/process/CapturingProcessAdapter.java +++ b/platform/platform-api/src/com/intellij/execution/process/CapturingProcessAdapter.java @@ -34,11 +34,15 @@ public class CapturingProcessAdapter extends ProcessAdapter { @Override public void onTextAvailable(ProcessEvent event, Key outputType) { + addToOutput(event.getText(), outputType); + } + + protected void addToOutput(String text, Key outputType) { if (outputType == ProcessOutputTypes.STDOUT) { - myOutput.appendStdout(event.getText()); + myOutput.appendStdout(text); } if (outputType == ProcessOutputTypes.STDERR) { - myOutput.appendStderr(event.getText()); + myOutput.appendStderr(text); } } diff --git a/platform/platform-api/src/com/intellij/execution/process/CapturingProcessHandler.java b/platform/platform-api/src/com/intellij/execution/process/CapturingProcessHandler.java index d76817b98f97..2452db7cdd83 100644 --- a/platform/platform-api/src/com/intellij/execution/process/CapturingProcessHandler.java +++ b/platform/platform-api/src/com/intellij/execution/process/CapturingProcessHandler.java @@ -30,10 +30,10 @@ import java.nio.charset.Charset; public class CapturingProcessHandler extends OSProcessHandler { private static final Logger LOG = Logger.getInstance(CapturingProcessHandler.class); private final ProcessOutput myOutput = new ProcessOutput(); - + public CapturingProcessHandler(@NotNull GeneralCommandLine commandLine) throws ExecutionException { super(commandLine); - addProcessListener(new CapturingProcessAdapter(myOutput)); + addProcessListener(createProcessAdapter(myOutput)); } public CapturingProcessHandler(final Process process) { @@ -46,9 +46,13 @@ public class CapturingProcessHandler extends OSProcessHandler { public CapturingProcessHandler(final Process process, final Charset charset, final String commandLine) { super(process, commandLine, charset); - addProcessListener(new CapturingProcessAdapter(myOutput)); + addProcessListener(createProcessAdapter(myOutput)); } + protected CapturingProcessAdapter createProcessAdapter(ProcessOutput processOutput) { + return new CapturingProcessAdapter(processOutput); + } + public ProcessOutput runProcess() { startNotify(); if (waitFor()) {