public static OutputStream wrapOutputStream(final OutputStream stream, boolean ttyDetection) {

        String os = System.getProperty("os.name");
        String vendor = System.getProperty("java.vendor");
        if (os.startsWith("Windows")) {

            try {
                return new AnsiOutputStream(stream, new AnsiOutputStream.ZeroWidthSupplier(), AnsiMode.Strip, new WindowsAnsiProcessor(stream), AnsiType.Emulation, AnsiColors.Colors256, Charset.defaultCharset(), () -> {
                }, () -> {
                }, true);
            } 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, new AnsiOutputStream.ZeroWidthSupplier(), AnsiMode.Strip, new AnsiProcessor(stream), AnsiType.Emulation, AnsiColors.Colors256, Charset.defaultCharset(), () -> {
            }, () -> {
            }, true);
        }
        if (vendor.toLowerCase().contains("android")) {
            //todo is this ok?
            return new FilterOutputStream(stream) {
                @Override
                public void close() throws IOException {
                    write(RESET_CODE);
                    flush();
                    super.close();
                }
            };
        }
        if (!vendor.toLowerCase().contains("android") && ttyDetection) {
            // We must be on some unix variant..
            try {
                // If we can detect that stdout is not a tty.. then setup
                // to strip the ANSI sequences..
                int rc = isatty(STDOUT_FILENO);
                if (rc == 0) {
                    return new AnsiOutputStream(stream, new AnsiOutputStream.ZeroWidthSupplier(), AnsiMode.Strip, new AnsiProcessor(stream), AnsiType.Emulation, AnsiColors.Colors256, Charset.defaultCharset(), () -> {
                    }, () -> {
                    }, true);
                }

                // These erros happen if the JNI lib is not available for your platform.
            } catch (NoClassDefFoundError | UnsatisfiedLinkError ignore) {
            }
        }

        // By default we assume your Unix tty can handle ANSI codes.
        // Just wrap it up so that when we get closed, we reset the
        // attributes.
        return new FilterOutputStream(stream) {
            @Override
            public void close() throws IOException {
                write(RESET_CODE);
                flush();
                super.close();
            }
        };
    }
This is actually a modified copy of AnsiConsole from the jansi project.
We had to copy it, because we want to be able to disable the tty detection.
jgiven-core/src/main/java/com/tngtech/jgiven/impl/util/AnsiUtil.java