Search in sources :

Example 1 with WindowsAnsiPrintStream

use of org.fusesource.jansi.WindowsAnsiPrintStream in project gradle by gradle.

the class AnsiConsoleUtil method wrapOutputStream.

/**
 * @see <a href="https://github.com/fusesource/jansi/blob/eeda18cb05122abe48b284dca969e2c060a0c009/jansi/src/main/java/org/fusesource/jansi/AnsiConsole.java#L64-L119">Method copied over from AnsiConsole.wrapOutputStream</a>
 */
private static OutputStream wrapOutputStream(final OutputStream stream, int fileno) {
    // any of the ansi sequences.
    if (Boolean.getBoolean("jansi.passthrough")) {
        return stream;
    }
    // the ansi escapes.
    if (Boolean.getBoolean("jansi.strip")) {
        return new AnsiOutputStream(stream);
    }
    if (OperatingSystem.current().isWindows() && !IS_MINGW_XTERM) {
        final long stdOutputHandle = GetStdHandle(STD_OUTPUT_HANDLE);
        final int[] mode = new int[1];
        if (stdOutputHandle != INVALID_HANDLE_VALUE && 0 != GetConsoleMode(stdOutputHandle, mode) && 0 != SetConsoleMode(stdOutputHandle, mode[0] | ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN)) {
            return new ProxyOutputStream(stream) {

                @Override
                public void close() throws IOException {
                    write(AnsiOutputStream.RESET_CODE);
                    flush();
                    // Reset console mode
                    SetConsoleMode(stdOutputHandle, mode[0]);
                    super.close();
                }
            };
        }
        // On windows we know the console does not interpret ANSI codes..
        try {
            return new WindowsAnsiPrintStream(new PrintStream(stream));
        } catch (Throwable ignore) {
        // this happens when JNA is not in the path.. or
        // this happens when the stdout is being redirected to a file.
        }
        // Use the ANSIOutputStream to strip out the ANSI escape sequences.
        return new AnsiOutputStream(stream);
    }
    // We must be on some Unix variant, including MSYS(2) on Windows...
    try {
        // If the jansi.force property is set, then we force to output
        // the ansi escapes for piping it into ansi color aware commands (e.g. less -r)
        boolean forceColored = Boolean.getBoolean("jansi.force");
        // to strip the ANSI sequences..
        if (!IS_XTERM && !forceColored && isatty(fileno) == 0) {
            return new AnsiOutputStream(stream);
        }
    } catch (Throwable ignore) {
    // These errors happen if the JNI lib is not available for your platform.
    // But since we are on ANSI friendly platform, assume the user is on the console.
    }
    // attributes.
    return new ProxyOutputStream(stream) {

        @Override
        public void close() throws IOException {
            write(AnsiOutputStream.RESET_CODE);
            flush();
            super.close();
        }
    };
}
Also used : PrintStream(java.io.PrintStream) WindowsAnsiPrintStream(org.fusesource.jansi.WindowsAnsiPrintStream) AnsiOutputStream(org.fusesource.jansi.AnsiOutputStream) ProxyOutputStream(org.apache.commons.io.output.ProxyOutputStream) WindowsAnsiPrintStream(org.fusesource.jansi.WindowsAnsiPrintStream)

Aggregations

PrintStream (java.io.PrintStream)1 ProxyOutputStream (org.apache.commons.io.output.ProxyOutputStream)1 AnsiOutputStream (org.fusesource.jansi.AnsiOutputStream)1 WindowsAnsiPrintStream (org.fusesource.jansi.WindowsAnsiPrintStream)1