CLI: add color to compiler output via jansi

Disabled on Windows temporarily, because for some reason colors there work only
in REPL
This commit is contained in:
Alexander Udalov
2015-06-13 14:56:05 +03:00
parent 00566bcfb0
commit 5ec4576844
6 changed files with 71 additions and 10 deletions
+11
View File
@@ -0,0 +1,11 @@
<component name="libraryTable">
<library name="jansi">
<CLASSES>
<root url="jar://$PROJECT_DIR$/dependencies/jansi.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$PROJECT_DIR$/dependencies/jansi-sources.jar!/" />
</SOURCES>
</library>
</component>
+1 -1
View File
@@ -61,8 +61,8 @@
<pathelement location="${protobuf.jar}"/>
<fileset dir="${basedir}/lib" includes="**/*.jar"/>
<fileset dir="${dependencies.dir}" includes="jline.jar"/>
<fileset dir="${dependencies.dir}" includes="jansi.jar"/>
<fileset dir="${dependencies.dir}" includes="jline.jar"/>
<fileset dir="${dependencies.dir}" includes="cli-parser-1.1.1.jar"/>
<fileset dir="${basedir}/ideaSDK/jps" includes="jps-model.jar"/>
</path>
+1
View File
@@ -13,6 +13,7 @@
<orderEntry type="module" module-name="light-classes" />
<orderEntry type="library" name="intellij-core" level="project" />
<orderEntry type="module" module-name="js.translator" />
<orderEntry type="library" name="jansi" level="project" />
<orderEntry type="library" name="jline" level="project" />
<orderEntry type="module" module-name="cli-common" exported="" />
<orderEntry type="module" module-name="backend-common" />
@@ -21,6 +21,7 @@ import com.google.common.collect.Lists;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.util.Disposer;
import com.sampullara.cli.Args;
import org.fusesource.jansi.AnsiConsole;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments;
@@ -120,15 +121,16 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
return OK;
}
errStream.print(messageRenderer.renderPreamble());
MessageCollector collector = new PrintingMessageCollector(errStream, messageRenderer, arguments.verbose);
try {
AnsiConsole.systemInstall();
errStream.print(messageRenderer.renderPreamble());
return exec(collector, services, arguments);
}
finally {
errStream.print(messageRenderer.renderConclusion());
AnsiConsole.systemUninstall();
}
}
@@ -16,10 +16,13 @@
package org.jetbrains.kotlin.cli.common.messages;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.LineSeparator;
import kotlin.KotlinPackage;
import kotlin.io.IoPackage;
import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.internal.CLibrary;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -80,6 +83,12 @@ public interface MessageRenderer {
};
abstract class PlainText implements MessageRenderer {
// AnsiConsole doesn't check isatty() for stderr (see https://github.com/fusesource/jansi/pull/35).
// TODO: investigate why ANSI escape codes on Windows only work in REPL for some reason
private static final boolean COLOR_ENABLED =
!SystemInfo.isWindows &&
CLibrary.isatty(CLibrary.STDERR_FILENO) != 0;
private static final String LINE_SEPARATOR = LineSeparator.getSystemLineSeparator().getSeparatorString();
@Override
@@ -110,10 +119,30 @@ public interface MessageRenderer {
result.append(" ");
}
result.append(severity.name().toLowerCase());
result.append(": ");
if (COLOR_ENABLED) {
Ansi ansi = Ansi.ansi()
.bold()
.fg(severityColor(severity))
.a(severity.name().toLowerCase())
.a(": ")
.reset()
.bold();
result.append(decapitalizeIfNeeded(message));
// Only make the first line of the message bold. Otherwise long overload ambiguity errors or exceptions are hard to read
String decapitalized = decapitalizeIfNeeded(message);
int firstNewline = decapitalized.indexOf(LINE_SEPARATOR);
if (firstNewline < 0) {
result.append(ansi.a(decapitalized).reset());
}
else {
result.append(ansi.a(decapitalized.substring(0, firstNewline)).reset().a(decapitalized.substring(firstNewline)));
}
}
else {
result.append(severity.name().toLowerCase());
result.append(": ");
result.append(decapitalizeIfNeeded(message));
}
if (lineContent != null && 1 <= column && column <= lineContent.length() + 1) {
result.append(LINE_SEPARATOR);
@@ -142,6 +171,26 @@ public interface MessageRenderer {
return KotlinPackage.decapitalize(message);
}
@NotNull
private static Ansi.Color severityColor(@NotNull CompilerMessageSeverity severity) {
switch (severity) {
case EXCEPTION:
return Ansi.Color.RED;
case ERROR:
return Ansi.Color.RED;
case WARNING:
return Ansi.Color.YELLOW;
case INFO:
return Ansi.Color.BLUE;
case LOGGING:
return Ansi.Color.BLUE;
case OUTPUT:
return Ansi.Color.BLUE;
default:
throw new UnsupportedOperationException("Unknown severity: " + severity);
}
}
@Nullable
protected abstract String getPath(@NotNull CompilerMessageLocation location);
+2 -4
View File
@@ -173,12 +173,10 @@
dest="dependencies/jflex/idea-flex.skeleton" usetimestamp="true"/>
<!-- jline -->
<get-maven-library prefix="jline" lib="jline" version="2.9" target.jar.name.base="jline"/>
<get-maven-library prefix="jline" lib="jline" version="2.12.1" target.jar.name.base="jline"/>
<!-- jansi -->
<!--
<get-maven-library prefix="org/fusesource/jansi" lib="jansi" version="1.9"/>
-->
<get-maven-library prefix="org/fusesource/jansi" lib="jansi" version="1.11" target.jar.name.base="jansi"/>
<!-- Guava 17 sources-->
<get-maven-library prefix="com/google/guava" lib="guava" version="17.0" bin="false"/>