Skip to content

Commit

Permalink
pull helpers for Ansi Escape codes into separate utility class
Browse files Browse the repository at this point in the history
  • Loading branch information
gtritchie committed Dec 3, 2016
1 parent 95a9b33 commit c71620a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* AnsiEscapeCode.java
*
* Copyright (C) 2009-16 by RStudio, Inc.
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/

package org.rstudio.studio.client.workbench.views.terminal;

/**
* Helpers for generating Ansi Escape Code strings for output to a terminal.
*/
public class AnsiCode
{
// Ansi command constants
public static final String CSI = "\33[";
public static final String SGR = "m";

public static final String DEFAULTCOLORS = CSI + "0;0" + SGR;

public static class ForeColor
{
public static final String BLACK = CSI + "0;30" + SGR;
public static final String RED = CSI + "0;31" + SGR;
public static final String GREEN = CSI + "0;32" + SGR;
public static final String BROWN = CSI + "0;33" + SGR;
public static final String BLUE = CSI + "0;34" + SGR;
public static final String MAGENTA = CSI + "0;35" + SGR;
public static final String CYAN = CSI + "0;36" + SGR;
public static final String GRAY = CSI + "0;37" + SGR;
public static final String DARKGRAY = CSI + "1;30" + SGR;
public static final String LIGHTRED = CSI + "1;31" + SGR;
public static final String LIGHTGREEN = CSI + "1;32" + SGR;
public static final String YELLOW = CSI + "1;33" + SGR;
public static final String LIGHTBLUE = CSI + "1;34" + SGR;
public static final String LIGHTMAGENTA = CSI + "1;35" + SGR;
public static final String LIGHTCYAN = CSI + "1:36" + SGR;
public static final String WHITE = CSI + "1;37" + SGR;
}

public static class BackColor
{
public static final String BLACK = CSI + "0;40" + SGR;
public static final String RED = CSI + "0;41" + SGR;
public static final String GREEN = CSI + "0;42" + SGR;
public static final String BROWN = CSI + "0;43" + SGR;
public static final String BLUE = CSI + "0;44" + SGR;
public static final String MAGENTA = CSI + "0;45" + SGR;
public static final String CYAN = CSI + "0;46" + SGR;
public static final String GRAY = CSI + "0;47" + SGR;
public static final String DARKGRAY = CSI + "1;40" + SGR;
public static final String LIGHTRED = CSI + "1;41" + SGR;
public static final String LIGHTGREEN = CSI + "1;42" + SGR;
public static final String YELLOW = CSI + "1;43" + SGR;
public static final String LIGHTBLUE = CSI + "1;44" + SGR;
public static final String LIGHTMAGENTA = CSI + "1;45" + SGR;
public static final String LIGHTCYAN = CSI + "1:46" + SGR;
public static final String WHITE = CSI + "1;47" + SGR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ protected void unregisterHandlers()

protected void writeError(String msg)
{
write(AnsiColor.RED +"Fatal Error: " + msg + AnsiColor.DEFAULT);
write(AnsiCode.ForeColor.RED + "Error: " + msg + AnsiCode.DEFAULTCOLORS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.rstudio.core.client.ExternalJavaScriptLoader.Callback;
import org.rstudio.core.client.resources.StaticDataResource;
import org.rstudio.studio.client.common.SuperDevMode;
import org.rstudio.studio.client.workbench.views.terminal.AnsiCode;
import org.rstudio.studio.client.workbench.views.terminal.events.ResizeTerminalEvent;
import org.rstudio.studio.client.workbench.views.terminal.events.TerminalDataInputEvent;
import org.rstudio.studio.client.workbench.views.terminal.events.TerminalTitleEvent;
Expand All @@ -45,42 +46,10 @@
* Xterm-compatible terminal emulator
*/
public class XTermWidget extends Widget implements RequiresResize,
ResizeTerminalEvent.HasHandlers,
TerminalDataInputEvent.HasHandlers,
ResizeTerminalEvent.HasHandlers, TerminalDataInputEvent.HasHandlers,
TerminalTitleEvent.HasHandlers
{
public enum AnsiColor
{
DEFAULT ("0;0"),
BLACK ("0;30"),
BLUE ("0;34"),
GREEN ("0;32"),
CYAN ("0;36"),
RED ("0;31"),
PURPLE ("0;35"),
BROWN ("0;33"),
LIGHTGRAY ("0;37"),
DARKGRAY ("1;30"),
LIGHTBLUE ("1;34"),
LIGHTCYAN ("1;32"),
LIGHTRED ("1;31"),
LIGHTPURPLE ("1;35"),
YELLOW ("1;33"),
WHITE ("1;37");

private final String color;
AnsiColor(String color)
{
this.color = color;
}

public String toString()
{
return "\33[" + color + "m";
}
}

/**
/**
* Creates an XTermWidget.
*/
public XTermWidget()
Expand Down Expand Up @@ -122,15 +91,15 @@ private void showBanner()
{
if (newTerminal_)
{
writeln("Welcome to " + AnsiColor.LIGHTBLUE + "RStudio" +
AnsiColor.DEFAULT + " terminal.");
writeln("Welcome to " + AnsiCode.ForeColor.LIGHTBLUE + "RStudio" +
AnsiCode.DEFAULTCOLORS + " terminal.");
}
else
{
// TODO (gary) this is temporary until buffer save and restore is done
writeln("\33[30;43m" +
writeln(AnsiCode.ForeColor.BLACK + AnsiCode.BackColor.BROWN +
"Reconnected. Restoring buffer is not-yet-implemented.");
writeln("Hit <enter> for prompt." + AnsiColor.DEFAULT);
writeln("Hit <enter> for prompt." + AnsiCode.DEFAULTCOLORS);
}
}

Expand Down

0 comments on commit c71620a

Please sign in to comment.