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

fix: use environment variables to check for terminal #2330

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions maestro-cli/src/main/java/maestro/cli/DisableAnsiMixin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,32 @@ 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 }

val stdoutIsTTY = CLibrary.isatty(CLibrary.STDOUT_FILENO) != 0
// Instead of using CLibrary.isatty, use environment variables to detect terminal
val forceDisable = System.getenv("MAESTRO_DISABLE_ANSI")?.toBoolean() ?: false
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
?: stdoutIsTTY // Otherwise fall back to checking if output is a tty
?: isTTY // Otherwise fall back to checking environment

Ansi.setEnabled(ansiEnabled)

Expand Down
Loading