Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Jan 24, 2025

Conversation

cstamas
Copy link
Contributor

@cstamas cstamas commented Jan 23, 2025

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;
Copy link
Member

@gnodet gnodet Jan 23, 2025

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
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
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
5 checks passed
@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