Skip to content

Commit 111617d

Browse files
committedSep 18, 2023
Support detecting the mintty terminal application
This is the default terminal used by Git Bash on Windows. See http://mintty.github.io/.
1 parent e7d9ef1 commit 111617d

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed
 

‎mordant/src/commonMain/kotlin/com/github/ajalt/mordant/terminal/TerminalDetection.kt

+23-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,18 @@ internal object TerminalDetection {
1414
): TerminalInfo {
1515
// intellij console is interactive, even though isatty returns false
1616
val ij = isIntellijRunActionConsole()
17-
val inputInteractive = interactive ?: (ij || stdinInteractive())
18-
val outputInteractive = interactive ?: (ij || stdoutInteractive())
17+
18+
// https://github.com/mintty/mintty/issues/482
19+
val mintty = isMinttyTerminal()
20+
21+
val inputInteractive = interactive ?: (ij || mintty || stdinInteractive())
22+
val outputInteractive = interactive ?: (ij || mintty || stdoutInteractive())
23+
1924
val level = ansiLevel ?: ansiLevel(outputInteractive)
2025
val ansiHyperLinks = hyperlinks ?: (outputInteractive && level != NONE && ansiHyperLinks())
26+
2127
val (w, h) = detectInitialSize()
28+
2229
return TerminalInfo(
2330
width = width ?: w,
2431
height = height ?: h,
@@ -45,6 +52,7 @@ internal object TerminalDetection {
4552
return forcedColor() != NONE && (isWindowsTerminal() || when (getTermProgram()) {
4653
"hyper", "wezterm" -> true
4754
"iterm.app" -> iTermVersionSupportsTruecolor()
55+
"mintty" -> minttyVersionSupportsHyperlinks()
4856
else -> when (getTerm()) {
4957
"xterm-kitty", "alacritty" -> true
5058
else -> false
@@ -82,6 +90,7 @@ internal object TerminalDetection {
8290
"hyper" -> return TRUECOLOR
8391
"apple_terminal" -> return ANSI256
8492
"iterm.app" -> return if (iTermVersionSupportsTruecolor()) TRUECOLOR else ANSI256
93+
"mintty" -> return TRUECOLOR
8594
"wezterm" -> return TRUECOLOR
8695
}
8796

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

173+
private fun minttyVersionSupportsHyperlinks(): Boolean {
174+
val ver = getEnv("TERM_PROGRAM_VERSION")?.split(".")?.mapNotNull { it.toIntOrNull() }
175+
if (ver == null || ver.size != 3) return false
176+
177+
// https://github.com/mintty/mintty/issues/823#issuecomment-473096464
178+
return (ver[0] > 2) || (ver[0] == 2 && ver[1] > 9) || (ver[0] == 2 && ver[1] == 9 && ver[2] > 6)
179+
}
180+
164181
private fun isCI(): Boolean {
165182
return getEnv("CI") != null
166183
}
@@ -182,4 +199,8 @@ internal object TerminalDetection {
182199
// their terminal tab. In the latter case, the JediTerm envvar is set, in the former it's missing.
183200
return !isJediTerm() && runningInIdeaJavaAgent()
184201
}
202+
203+
private fun isMinttyTerminal(): Boolean {
204+
return getEnv("TERM_PROGRAM") == "mintty"
205+
}
185206
}

0 commit comments

Comments
 (0)