-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve AnsiColor and support non-colored styles
- The RESET will no longer be added if we have no guarantees that ANSI is supported. - Add non-colored styles (bold, italic, blinking, etc.). - Printing an AnsiColor instance like AnsiColor.RED will now use regular style.
- Loading branch information
Showing
1 changed file
with
28 additions
and
10 deletions.
There are no files selected for viewing
38 changes: 28 additions & 10 deletions
38
lib-publishing-helpers/src/main/kotlin/lib_publisher_tools/cli/AnsiColor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,37 @@ | ||
package lib_publisher_tools.cli | ||
|
||
enum class AnsiColor(private val colorNumber: Byte) { | ||
BLACK(0), RED(1), GREEN(2), YELLOW(3), BLUE(4), MAGENTA(5), CYAN(6), WHITE(7); | ||
BLACK(0), | ||
RED(1), | ||
GREEN(2), | ||
YELLOW(3), | ||
BLUE(4), | ||
MAGENTA(5), | ||
CYAN(6), | ||
WHITE(7); | ||
|
||
companion object { | ||
private const val prefix = "\u001B" | ||
const val RESET = "$prefix[0m" | ||
private val isCompatible = "win" !in System.getProperty("os.name").lowercase() | ||
private val isCompatible = "win" !in System.getProperty("os.name").lowercase() //TODO: Support PowerShell? | ||
private inline fun ifCompatible(block: () -> String): String = if (isCompatible) block() else "" | ||
|
||
val RESET = ifCompatible { "$prefix[0m" } | ||
|
||
val bold get() = ifCompatible { "$prefix[1m" } | ||
val dim get() = ifCompatible { "$prefix[2m" } | ||
val italic get() = ifCompatible { "$prefix[3m" } | ||
val underline get() = ifCompatible { "$prefix[4m" } | ||
val blinking get() = ifCompatible { "$prefix[5m" } | ||
val inverse get() = ifCompatible { "$prefix[7m" } | ||
val invisible get() = ifCompatible { "$prefix[8m" } | ||
} | ||
|
||
val regular get() = if (isCompatible) "$prefix[0;3${colorNumber}m" else "" | ||
val bold get() = if (isCompatible) "$prefix[1;3${colorNumber}m" else "" | ||
val underline get() = if (isCompatible) "$prefix[4;3${colorNumber}m" else "" | ||
val background get() = if (isCompatible) "$prefix[4${colorNumber}m" else "" | ||
val highIntensity get() = if (isCompatible) "$prefix[0;9${colorNumber}m" else "" | ||
val boldHighIntensity get() = if (isCompatible) "$prefix[1;9${colorNumber}m" else "" | ||
val backgroundHighIntensity get() = if (isCompatible) "$prefix[0;10${colorNumber}m" else "" | ||
override fun toString() = regular | ||
val regular get() = ifCompatible { "$prefix[0;3${colorNumber}m" } | ||
val bold get() = ifCompatible { "$prefix[1;3${colorNumber}m" } | ||
val underline get() = ifCompatible { "$prefix[4;3${colorNumber}m" } | ||
val background get() = ifCompatible { "$prefix[4${colorNumber}m" } | ||
val highIntensity get() = ifCompatible { "$prefix[0;9${colorNumber}m" } | ||
val boldHighIntensity get() = ifCompatible { "$prefix[1;9${colorNumber}m" } | ||
val backgroundHighIntensity get() = ifCompatible { "$prefix[0;10${colorNumber}m" } | ||
} |