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

Support detecting the mintty terminal application on Cygwin / MSYS2 #127

Merged
merged 1 commit into from
Sep 20, 2023

Conversation

sschuberth
Copy link
Contributor

@sschuberth sschuberth commented Sep 18, 2023

See http://mintty.github.io/. Note that when running mintty as a MingGW
application (as done by Git Bash on Windows), the check for an
interactive terminal currently fails as mintty is redirecting output,
see 1 and 2. This needs to be compensated with by a pipe-based
check as done by Git for Windows itself 3.

Comment on lines 17 to 23

// https://github.com/mintty/mintty/issues/482
val mintty = isMinttyTerminal()

val inputInteractive = interactive ?: (ij || mintty || stdinInteractive())
val outputInteractive = interactive ?: (ij || mintty || stdoutInteractive())

Copy link
Owner

@ajalt ajalt Sep 18, 2023

Choose a reason for hiding this comment

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

This will cause ANSI codes to be output even if stdout is redirected to a file, which we want to avoid. So let's remove this part of the PR.

The linked issue mentions some native calls that can be used as a workaround, and we could add them to Win32MppImpls in a separate PR. I don't think that's necessary, though, since the issue also mentions that proper PTY support was added in 2019. It looks like Mordant can detect interactivity correctly on modern versions of mintty, so I would say we could omit all the workarounds and let folks on very old versions of mintty live without color.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, my initial code didn't include this isMinttyTerminal() check. But I had to add it as otherwise the if (!interactive) return NONE in ansiLevel() is taken and the when (getTermProgram()) { isn't even reached...

So...

It looks like Mordant can detect interactivity correctly on modern versions of mintty

no, that did't work for me (using mintty 3.6.4).

This will cause ANSI codes to be output even if stdout is redirected to a file, which we want to avoid.

True, but that's also already the case for ij == true, or?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Owner

Choose a reason for hiding this comment

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

when (getTermProgram()) { isn't even reached...

Yes, that's the point; if stdout is redirected to a file, we don't want to output ansi codes.

that's also already the case for ij == true

The intellij run console is a special case since it's not a normal terminal: you can't redirect it's output to a file, so we know it's always interactive.

Also see https://github.com/mintty/mintty/wiki/Tips#inputoutput-interaction-with-alien-programs.

That seems to support the idea that PTY support should work on newer versions of mintty. Did you try setting and of the config/evvnars they mention?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's the point; if stdout is redirected to a file, we don't want to output ansi codes.

Right. But my point is that when (getTermProgram()) { isn't reached even if output is not redirected to a file.

The intellij run console is a special case

Ok, understood.

That seems to support the idea that PTY support should work on newer versions of mintty.

You have to read closely: That's only the case when running mintty on Cygwin / MSYS2, i.e. if it uses the cygwin1.dll emulation layer. It's not the case when running mintty as an MinGW program, like it is done in the Git Bash that comes with Git for Windows. That's why Git for Windows itself has this detection mechanism; we'd need something similar in Mordant.

Did you try setting and of the config/evvnars they mention?

No. I'd like Mordant to work out of the box with a fresh installation of Git for Windows / mintty without any need to explicitly tell it that it's a color console; such an approach would not scale across users.

Copy link
Owner

Choose a reason for hiding this comment

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

when (getTermProgram()) { isn't reached even if output is not redirected to a file.

That's correct. Whenever we're in a situation where it's not possible to detect whether output is interactive or not (which is the case for Git for Windows), we should assume it is not, in order to avoid printing ANSI codes to a file.

Git for Windows itself has this detection mechanism

Yeah, that was the workaround I mentioned that we could add to Win32MppImpls in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So how should we move forward? IMO the preferred order would be to first have the means in Win32MppImpls to be able to implement that pipe-based detection, and then I'd rewrite this PR of mine to implement isMinttyTerminal() using those functions instead of just looking at the environment variable.

Do you agree?

Copy link
Owner

Choose a reason for hiding this comment

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

We might as well merge the envvar part now, since it's still valid for systems where the interactivity is detected correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok. I've rewritten my commit to not include the changes to the interactive logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that was the workaround I mentioned that we could add to Win32MppImpls in a separate PR.

@ajalt are you going to do that?

@@ -161,6 +170,14 @@ internal object TerminalDetection {
return ver != null && ver >= 3
}

private fun minttyVersionSupportsHyperlinks(): Boolean {
Copy link
Owner

Choose a reason for hiding this comment

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

👍

See http://mintty.github.io/. Note that when running mintty as a MingGW
application (as done by Git Bash on Windows), the check for an
interactive terminal currently fails as mintty is redirecting output,
see [1] and [2]. This needs to be compensated with by a pipe-based
check as done by Git for Windows itself [3].

[1]: mintty/mintty#482
[2]: https://github.com/mintty/mintty/wiki/Tips#inputoutput-interaction-with-alien-programs
[3]: https://github.com/git/git/blob/d4a83d07b8cc66d4afac2f33a8af729f2ba93bba/compat/winansi.c#L579-L585
@sschuberth sschuberth changed the title Support detecting the mintty terminal application Support detecting the mintty terminal application on Cygwin / MSYS2 Sep 20, 2023
@sschuberth sschuberth requested a review from ajalt September 20, 2023 06:45
@ajalt ajalt merged commit 51f5d31 into ajalt:master Sep 20, 2023
@sschuberth sschuberth deleted the mintty-support branch September 20, 2023 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants