from Java 11 on windows10 how best to detect in System.out is ansi enabled

  ansi, java, logback-classic, windows

I am trying to use Logback with colour output if the System.out is Ansi enabled, using Groovy v3, java 11, on windows 10.

I want to make my logback.groovy config switch the appender format string based on detecting whether the std out for my console is ansi enabled or not.

I’ve tried to find an easy answer online and not managed it. however by looking at some code in fusesource jansi libraries i can see things like this (along with relevant static imports etc )

final int VIRTUAL_TERMINAL_PROCESSING = 0x0004
**long console = GetStdHandle(STD_OUTPUT_HANDLE)
int[] mode = new int[1]
def ansiEnabled = Kernel32.GetConsoleMode(console, mode)**
println "appended is ansi enabled $ansiEnabled"

when i run this on my machine, this returns a value of 0

my coloured logging does work for me – so implicitly my System.out is ansi enabled

so query does the 0 correctly indicate i’m enabled?

I Found additional code, like this, in the jansi library, which shows setting the VTmode which suggests that if mode == 0 then its false (i.e. not ansi enabled). I have not attempted to try and force set the
console yet in my example code.

private static boolean setVTMode() {
    long console = GetStdHandle(STD_OUTPUT_HANDLE);
    int[] mode = new int[1];
    if (Kernel32.GetConsoleMode(console, mode) == 0) {
        // No need to go further, not supported.
        return false;
    }
    if (Kernel32.SetConsoleMode(console, mode[0] | VIRTUAL_TERMINAL_PROCESSING) == 0) {
        // No need to go further, not supported.
        return false;
    }

    return true;
}

public static boolean isVTSupported() {
    return setVTMode();
}

if there is an easier way to detect from within Java generally whether the System.out is ansi enabled . I’d like to use that if i could.

Failing which if i stick with the jansi library for now – how can i detect correctly whether the System.out is ansi enabled or not?

Source: Windows Questions

LEAVE A COMMENT