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

Disable colors on Windows OS #1701

Merged
merged 5 commits into from
Jun 19, 2019
Merged

Disable colors on Windows OS #1701

merged 5 commits into from
Jun 19, 2019

Conversation

remal
Copy link
Contributor

@remal remal commented Jun 16, 2019

This is simple and a bit dirty fix for #1694 - remove color chars in console output for Windows OS.

IMHO this PR is less preferable than #1702

Signed-off-by: Semyon Levin <levin.semen@gmail.com>
@codecov-io
Copy link

codecov-io commented Jun 16, 2019

Codecov Report

Merging #1701 into master will decrease coverage by 3.22%.
The diff coverage is 57.14%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #1701      +/-   ##
============================================
- Coverage     79.14%   75.91%   -3.23%     
- Complexity     1760     1856      +96     
============================================
  Files           285      328      +43     
  Lines          4943     5527     +584     
  Branches        908     1010     +102     
============================================
+ Hits           3912     4196     +284     
- Misses          541      791     +250     
- Partials        490      540      +50
Impacted Files Coverage Δ Complexity Δ
.../gitlab/arturbosch/detekt/cli/console/Colorizer.kt 76.92% <57.14%> (-23.08%) 0 <0> (ø)
.../gitlab/arturbosch/detekt/rules/empty/EmptyRule.kt 86.66% <0%> (-4.25%) 10% <0%> (+2%)
...ab/arturbosch/detekt/formatting/KtLintMultiRule.kt 88.88% <0%> (-3.97%) 9% <0%> (+3%)
...kotlin/io/gitlab/arturbosch/detekt/api/Findings.kt 0% <0%> (ø) 0% <0%> (?)
...tlin/io/gitlab/arturbosch/detekt/api/YamlConfig.kt 70.83% <0%> (ø) 7% <0%> (?)
...in/io/gitlab/arturbosch/detekt/api/SplitPattern.kt 75% <0%> (ø) 10% <0%> (?)
...ekt/sample/extensions/rules/TooManyFunctionsTwo.kt 0% <0%> (ø) 0% <0%> (?)
...io/gitlab/arturbosch/detekt/api/CompositeConfig.kt 60% <0%> (ø) 4% <0%> (?)
...n/kotlin/io/gitlab/arturbosch/detekt/api/Entity.kt 50% <0%> (ø) 1% <0%> (?)
...n/io/gitlab/arturbosch/detekt/api/ProjectMetric.kt 0% <0%> (ø) 0% <0%> (?)
... and 37 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0dd2eba...c119511. Read the comment docs.

Copy link
Member

@schalkms schalkms left a comment

Choose a reason for hiding this comment

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

Thanks for your time valuable feedback and your two proposals for this issue!!
I favor this approach, because it's simpler and doesn't require a 3rd party library as a dependency.
I have some questions.

What happens if this is executed in the Cygwin shell on Windows? The shell supports ANSI coloring.
What happens if this is executed in the Windows PowerShell?

CC @marschwar

@@ -12,7 +12,12 @@ private data class Color(private val value: Byte) {
get() = "$ESC[${value}m"
}

private fun CharSequence.colorized(color: Color) = "${color.escapeSequence}$this${RESET.escapeSequence}"
private val isColoredOutputSupported: Boolean = !System.getProperty("os.name", "").contains("win", true)
Copy link
Member

Choose a reason for hiding this comment

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

What happens if the OS is Darwin? Then this variable is true.
The check should be written with startsWith to be safer.

!System.getProperty("os.name", "").startsWith("Windows")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@remal
Copy link
Contributor Author

remal commented Jun 17, 2019

@schalkms The Idea behind checking if the property contains win substring is that it's better to turn off colorizing than add colors on a shell without colors support.

Actually I don't see any major drawbacks with adding Jansi dependency to detekt-cli module. I suppose those who don't need this library, probably, won't use detekt-cli.

Also, as I understand, all checks you mentioned are supported by Jansi.

BTW IDEA correctly displays colorized output by Jansi.

@schalkms
Copy link
Member

schalkms commented Jun 17, 2019

The Idea behind checking if the property contains win substring is that it's better to turn off colorizing than add colors on a shell without colors support.

I fully agree. However, .contains("win") could match operating systems, which are not Windows. Thus, we need to change it to .startsWith("Windows") to be safer here.
Apache System Utils also does it this way.

Actually I don't see any major drawbacks with adding Jansi dependency to detekt-cli module

Jansi uses JNI on Windows to support coloring terminal output.
I'd say for detekt's simple usage it's not necessary to import this library.

Also, as I understand, all checks you mentioned are supported by Jansi.

I tried it out on Windows with the following shells.

  • Cygwin - supports it natively without any hacks
  • Cmder supports cmd and unix shells - works out-of-the-box
  • Cmd + Powershell - do not support coloring the terminal output without any hacks (as expected).

BTW IDEA correctly displays colorized output by Jansi.

True, because IntelliJ only emulates the default cmd on Windows, if not configured otherwise.

@marschwar
Copy link
Contributor

I agree with @schalkms. I don't really see the need for adding an additional library just for this. It's far from being a core feature of detekt.

I could live with contains or startsWith, but in my opinion we should check for the string windows as it seems to be common practice.

See:
https://github.com/sonatype/plexus-interpolation/blob/16f8c521c60021fbac3295f0ed10ba5bc6cfdc9a/src/main/java/org/codehaus/plexus/interpolation/os/Os.java#L308
https://github.com/apache/commons-lang/blob/dc2953e5e41f0d5bf069349bbd7c6fe26f3362e1/src/main/java/org/apache/commons/lang3/SystemUtils.java#L1355

"${color.escapeSequence}$this${RESET.escapeSequence}"
} else {
this.toString()
}
Copy link
Contributor

@marschwar marschwar Jun 17, 2019

Choose a reason for hiding this comment

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

Minor nitpick: In my opinion CharSequence.colorized(color: Color) should itself return a CharSequence if possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@arturbosch
Copy link
Member

Thanks @remal @schalkms @marschwar for further investigations. So lets go with this pr then.

@arturbosch arturbosch added this to the 1.0.0 milestone Jun 18, 2019
Signed-off-by: Semyon Levin <levin.semen@gmail.com>
Signed-off-by: Semyon Levin <levin.semen@gmail.com>
@remal
Copy link
Contributor Author

remal commented Jun 18, 2019

Guys, please take a look.

Signed-off-by: Semyon Levin <levin.semen@gmail.com>
fun CharSequence.decolorized() = this.replace(escapeSequenceRegex, "")
fun String.red(): String = colorized(RED).toString()
fun String.yellow(): String = colorized(YELLOW).toString()
fun String.decolorized(): String = this.replace(escapeSequenceRegex, "")
Copy link
Member

Choose a reason for hiding this comment

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

Why do we have the same extension method String and CharSequence? Did I miss something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because @marschwar asked to make CharSequence.colorized(color: Color) to return CharSequence. But there are some places where String is expected, so it would be more convenient to have a method that returns String. I suggest leaving only one variant that returns String. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry to cause such a confusion. I had not actually made the change myself so I did not realize there were usages where a String is expected to be returned. My point was that the extension function should return the same type if possible.
Please keep fun String.colorized(color: Color): String only. AFAIK we don't need the extension function to be on CharSequence.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

Signed-off-by: Semyon Levin <levin.semen@gmail.com>
@arturbosch arturbosch merged commit a6f4335 into detekt:master Jun 19, 2019
arturbosch pushed a commit that referenced this pull request Jun 22, 2019
* Disable colors on Windows OS

Signed-off-by: Semyon Levin <levin.semen@gmail.com>

* CharSequence.colorized returns CharSequence

Signed-off-by: Semyon Levin <levin.semen@gmail.com>

* Is Windows check is changed

Signed-off-by: Semyon Levin <levin.semen@gmail.com>

* code formatting

Signed-off-by: Semyon Levin <levin.semen@gmail.com>

* simplification

Signed-off-by: Semyon Levin <levin.semen@gmail.com>
@remal remal deleted the disable-colors-on-windows branch June 28, 2019 20:41
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.

5 participants