Skip to content

Commit

Permalink
Add conditional checking for Windows terminals
Browse files Browse the repository at this point in the history
  • Loading branch information
mchisolm0 committed Feb 24, 2025
1 parent 1f1fc8c commit d13b489
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion maestro-cli/src/main/java/maestro/cli/DisableAnsiMixin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,29 @@ class DisableAnsiMixin {
return null
}

private fun isWindowsTerminalColorCapable(): Boolean {
// Check for modern Windows terminals that support ANSI
return System.getenv("PSVersionTable") != null || // PowerShell
System.getenv("WT_SESSION") != null || // Windows Terminal
System.getenv("TERM_PROGRAM") == "vscode" || // VS Code terminal
}

private fun applyCLIMixin(parseResult: CommandLine.ParseResult) {
// Find the first mixin for which of the enable-ansi parameter was specified
val parserWithANSIOption = findFirstParserWithMatchedParamLabel(parseResult, "<enableANSIOutput>")
val mixin = parserWithANSIOption?.commandSpec()?.mixins()?.values?.firstNotNullOfOrNull { it.userObject() as? DisableAnsiMixin }

// Instead of using CLibrary.isatty, use environment variables to detect terminal
val forceDisable = System.getenv("MAESTRO_DISABLE_ANSI")?.toBoolean() ?: false
val isTTY = !forceDisable && System.getenv("TERM") != null
val forceEnable = System.getenv("MAESTRO_FORCE_ANSI")?.toBoolean() ?: false

val isWindows = System.getProperty("os.name").lowercase().contains("windows")
val isTTY = when {
forceEnable -> true
forceDisable -> false
isWindows -> isWindowsTerminalColorCapable()
else -> System.getenv("TERM") != null
}

ansiEnabled = mixin?.enableANSIOutput // Use the param value if it was specified
?: isTTY // Otherwise fall back to checking environment
Expand Down

0 comments on commit d13b489

Please sign in to comment.