Skip to content

Commit

Permalink
Merge pull request #30 from KyoriPowered/fix/jansi
Browse files Browse the repository at this point in the history
Use JAnsi for color level support if it exists
  • Loading branch information
zml2008 authored Aug 2, 2023
2 parents c5fbd91 + 80a2958 commit 0ade0da
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 2 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies {
testImplementation libs.junit.api
testRuntimeOnly libs.junit.engine
checkstyle libs.stylecheck
compileOnly libs.jansi
}

spotless {
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ junit-bom = { module = "org.junit:junit-bom", version.ref = "junit" }
junit-api = { module = "org.junit.jupiter:junit-jupiter-api" }
junit-engine = { module = "org.junit.jupiter:junit-jupiter-engine" }
stylecheck = { module = "ca.stellardrift:stylecheck", version = "0.2.1" }
jansi = { module = "org.fusesource.jansi:jansi", version = "2.4.0" }

# unused, for renovate
zCheckstyle = { module = "com.puppycrawl.tools:checkstyle", version.ref = "checkstyle" }
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/net/kyori/ansi/ColorLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public enum ColorLevel {

private static final String COLORTERM = System.getenv("COLORTERM");
private static final String TERM = System.getenv("TERM");
private static final String WT_SESSION = System.getenv("WT_SESSION");
private static int[] indexed256ColorTable = null;

/**
Expand Down Expand Up @@ -200,7 +201,13 @@ public enum ColorLevel {
} else if (TERM != null && TERM.contains("256color")) {
return ColorLevel.INDEXED_256;
} else if (TERM == null) {
return ColorLevel.NONE; // This isn't a terminal at all
if (WT_SESSION != null) {
return ColorLevel.TRUE_COLOR; // Windows Terminal
} else if (System.console() != null && JAnsiColorLevel.isAvailable()) {
return JAnsiColorLevel.computeFromJAnsi();
} else {
return ColorLevel.NONE; // This isn't a terminal at all
}
}

// Fallback, unknown type of terminal
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/net/kyori/ansi/JAnsiColorLevel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* This file is part of ansi, licensed under the MIT License.
*
* Copyright (c) 2023 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.kyori.ansi;

import org.fusesource.jansi.AnsiColors;
import org.fusesource.jansi.AnsiConsole;

final class JAnsiColorLevel {
private static final Throwable UNAVAILABILITY_CAUSE;

static {
Throwable cause = null;
try {
Class.forName("org.fusesource.jansi.AnsiConsole");
} catch (final ClassNotFoundException classNotFoundException) {
cause = classNotFoundException;
}
UNAVAILABILITY_CAUSE = cause;
}

private JAnsiColorLevel() {
}

static boolean isAvailable() {
return UNAVAILABILITY_CAUSE == null;
}

static ColorLevel computeFromJAnsi() {
final AnsiColors colors = AnsiConsole.out().getColors();
if (colors == null) return ColorLevel.NONE;
switch (colors) {
case Colors16:
return ColorLevel.INDEXED_16;
case Colors256:
return ColorLevel.INDEXED_256;
case TrueColor:
return ColorLevel.TRUE_COLOR;
default:
return ColorLevel.NONE;
}
}
}
3 changes: 2 additions & 1 deletion src/main/java9/module-info.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of ansi, licensed under the MIT License.
*
* Copyright (c) 2021-2022 KyoriPowered
* Copyright (c) 2021-2023 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -29,5 +29,6 @@
*/
module net.kyori.ansi {
exports net.kyori.ansi;
requires static org.fusesource.jansi;
requires static transitive org.jetbrains.annotations;
}
6 changes: 6 additions & 0 deletions src/test/java/net/kyori/ansi/ANSIComponentRendererTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

class ANSIComponentRendererTest {
@Test
Expand Down Expand Up @@ -156,6 +157,11 @@ void testSystemProperty() {
assertEquals(ColorLevel.compute(), ColorLevel.TRUE_COLOR);
}

@Test
void testComputeActuallyRuns() {
assertNotEquals(ColorLevel.compute(), null);
}

private String render(final Consumer<ANSIComponentRenderer<TestStyle>> action) {
return this.render(action, ColorLevel.TRUE_COLOR);
}
Expand Down

0 comments on commit 0ade0da

Please sign in to comment.