-
Notifications
You must be signed in to change notification settings - Fork 280
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
Add a --color flag to the CLI and disable colors for Pkl test #571
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -17,6 +17,7 @@ package org.pkl.commons.cli.commands | |||||||||||||||||
|
||||||||||||||||||
import com.github.ajalt.clikt.parameters.groups.OptionGroup | ||||||||||||||||||
import com.github.ajalt.clikt.parameters.options.* | ||||||||||||||||||
import com.github.ajalt.clikt.parameters.types.choice | ||||||||||||||||||
import com.github.ajalt.clikt.parameters.types.int | ||||||||||||||||||
import com.github.ajalt.clikt.parameters.types.long | ||||||||||||||||||
import com.github.ajalt.clikt.parameters.types.path | ||||||||||||||||||
|
@@ -198,6 +199,11 @@ class BaseOptions : OptionGroup() { | |||||||||||||||||
.single() | ||||||||||||||||||
.split(",") | ||||||||||||||||||
|
||||||||||||||||||
val colors: String by | ||||||||||||||||||
option(names = arrayOf("--colors"), help = "Enable or disable colour output in the terminal") | ||||||||||||||||||
.choice("auto", "never", "always") | ||||||||||||||||||
.default("auto") | ||||||||||||||||||
Comment on lines
+202
to
+205
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's follow the convention set by unix-y tools like Also, with the above suggestion to enum-ify this (requires
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
// hidden option used by native tests | ||||||||||||||||||
private val testPort: Int by | ||||||||||||||||||
option(names = arrayOf("--test-port"), help = "Internal test option", hidden = true) | ||||||||||||||||||
|
@@ -208,7 +214,8 @@ class BaseOptions : OptionGroup() { | |||||||||||||||||
fun baseOptions( | ||||||||||||||||||
modules: List<URI>, | ||||||||||||||||||
projectOptions: ProjectOptions? = null, | ||||||||||||||||||
testMode: Boolean = false | ||||||||||||||||||
testMode: Boolean = false, | ||||||||||||||||||
disableColors: Boolean = false, | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
): CliBaseOptions { | ||||||||||||||||||
return CliBaseOptions( | ||||||||||||||||||
sourceModules = modules, | ||||||||||||||||||
|
@@ -230,7 +237,8 @@ class BaseOptions : OptionGroup() { | |||||||||||||||||
noProject = projectOptions?.noProject ?: false, | ||||||||||||||||||
caCertificates = caCertificates, | ||||||||||||||||||
httpProxy = proxy, | ||||||||||||||||||
httpNoProxy = noProxy ?: emptyList() | ||||||||||||||||||
httpNoProxy = noProxy ?: emptyList(), | ||||||||||||||||||
colors = if (disableColors) "never" else colors, | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This exists to provide a mechanism for the By providing this escape hatch, I can ensure that colour are disabled before there's any opportunity for the Jansi formatting object to init, because once it's created you can't disable the injection of ANSI codes, only strip them out later. That mixed with the fact that caught errors in I tried to figure out where the error truncation etc was happening, so I could modify it to handle ANSI/non-ANSI outputs in an equivalent way, but I haven't been able to track down where it happens. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if (when?) we switch to a text formatter, we can control this ourselves by passing a no-op text formatter when Anyways, I don't know if that's relevant to my suggestion here. val options = baseOptions(myModules).copy(color = Color.NEVER) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's only relevant due to how Jansi handles global state. It's caught me out a couple of times because Jansi doesn't handle changes to global state the way you might expect. The final result is what you expect, but the mechanism by which Jansi achieves that result depends on the exact order of operations (mutating global state before or after doing the initial Jansi setup). In most cases it doesn't make any practical difference, but for the specific case of Pkl test it does (which I learned the hard way). For this specific case I need to make sure the Agree we can just get rid of this hack by implementing a proper formatting API, and having direct control over how control codes are injected. It's also a good argument to do a proper formatting API sooner rather than later. The number of hacks and shortcuts I've had to introduce to get Jansi working as expected is getting pretty uncomfortable at this point. So I'll probably look towards doing that, rather than continuing further with this approach. |
||||||||||||||||||
) | ||||||||||||||||||
} | ||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -166,7 +166,8 @@ protected CliBaseOptions getCliBaseOptions() { | |
getTestPort().getOrElse(-1), | ||
Collections.emptyList(), | ||
getHttpProxy().getOrNull(), | ||
getHttpNoProxy().getOrElse(List.of())); | ||
getHttpNoProxy().getOrElse(List.of()), | ||
"auto"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm pretty sure this means that Gradle will not output any colors, because this is executed as a child process. Anyway, we should provide the same options in Gradle (auto, never, always). Take a look at how the other options are configured to see what to do here. Or, leave a TODO comment here so we can follow up in a future PR. |
||
} | ||
return cachedOptions; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make this an enum:
And change this type to said enum, and also add a doc comment. Also, nit:
s/colors/color