Skip to content

AnsiConsole should always obey the terminal (fixes #1160) - #1161

Merged
gnodet merged 4 commits into
jline:masterfrom
cstamas:pr-1160
Jan 24, 2025
Merged

AnsiConsole should always obey the terminal (fixes #1160)#1161
gnodet merged 4 commits into
jline:masterfrom
cstamas:pr-1160

Conversation

@cstamas

@cstamas cstamas commented Jan 23, 2025

Copy link
Copy Markdown
Contributor

Currently, if terminal is set, but is not "tty" (ie. is dumb), AnsiConsole will grab the process stdout/stderr instead to obey the set terminal output.

Fixes #1160

Currently, if terminal is set, but is not "tty" (ie. is dumb),
AnsiConsole will grab the process stdout/stderr instead to obey
the set terminal output.

Fixes jline#1160
@@ -247,7 +247,10 @@ private static AnsiPrintStream ansiStream(boolean stdout) throws IOException {
width = terminal::getWidth;
type = terminal instanceof DumbTerminal ? AnsiType.Unsupported : AnsiType.Native;

@gnodet gnodet Jan 23, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That line is also problematic I think, as DumbTerminalProvider always return false for isSystemStream.
So I'm tempted to rather somehow change the isatty computation, or maybe just remove it.

What about replacing:

        final TerminalProvider provider = ((TerminalExt) terminal).getProvider();
        final boolean isatty =
                provider != null && provider.isSystemStream(stdout ? SystemStream.Output : SystemStream.Error);
        if (isatty) {
            out = terminal.output();
            width = terminal::getWidth;
            type = terminal instanceof DumbTerminal ? AnsiType.Unsupported : AnsiType.Native;
        } else {
            out = new FastBufferedOutputStream(new FileOutputStream(stdout ? FileDescriptor.out : FileDescriptor.err));
            width = new AnsiOutputStream.ZeroWidthSupplier();
            type = ((TerminalExt) terminal).getSystemStream() != null ? AnsiType.Redirected : AnsiType.Unsupported;
        }

with

        out = terminal.output();
        width = terminal::getWidth;
        type = terminal instanceof DumbTerminal
                ? AnsiType.Unsupported
                : ((TerminalExt) terminal).getSystemStream() != null ? AnsiType.Redirected : AnsiType.Native;

as terminal cannot be null.

new FileInputStream(FileDescriptor.in),
new FileOutputStream(systemStream == SystemStream.Error ? FileDescriptor.err : FileDescriptor.out),
System.in,
systemStream == SystemStream.Error ? System.err : System.out,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about those lines.
The provider is required to create a system terminal, which is supposed to be backed by the real terminal emulator where the JVM is running. If someone changes System.out or System.err, there's no guarantee about the behavior. If the goal is to be able to run tests, you need to initialize a terminal and set it on the AnsiConsole before calling systemInstall.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, undone this

@gnodet gnodet changed the title bug: Obey terminal if set AnsiConsole should always obey the terminal (fixes #1160) Jan 24, 2025
@gnodet
gnodet merged commit f869217 into jline:master Jan 24, 2025
@cstamas
cstamas deleted the pr-1160 branch January 26, 2025 13:52
@gnodet gnodet added the bug label Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dumb Terminal combined with AnsiConsole neglects possible stdout/stderr redirects

2 participants