commonMain.com.bkahlert.kommons.ansi.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kommons-core-jvm Show documentation
Show all versions of kommons-core-jvm Show documentation
Kommons Core is a Kotlin Multiplatform Library that offers shared features for all Kommons modules.
package com.bkahlert.kommons
private val ansiPatterns = listOf(
@Suppress("RegExpRedundantEscape") // otherwise "lone quantifier brackets in JS"
"\\u001B\\]\\d*;[^\\u001B]*\\u001B\\\\".toRegex(), // OSC (operating system command) escape sequences
"\\u001B[@-Z\\-_]".toRegex(), // Fe escape sequences
"\\u001B[ -/][@-~]".toRegex(), // 2-byte sequences
"\\u001B\\[[0-?]*[ -/]*[@-~]".toRegex(), // CSI (control sequence intro) escape sequences
)
private val ansiPattern: Regex = ansiPatterns.joinToString("|") { it.pattern }.toRegex()
/** Whether this character sequence contains [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code).*/
public val CharSequence.ansiContained: Boolean
get() = ansiPattern.containsMatchIn(this)
/** This character sequence with all [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) removed. */
public val CharSequence.ansiRemoved: CharSequence
get() = if (ansiContained) ansiPattern.replace(this, String.EMPTY) else this
/** This character sequence with all [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) removed. */
public val String.ansiRemoved: String
get() = if (ansiContained) ansiPattern.replace(this, String.EMPTY) else this
© 2015 - 2025 Weber Informatics LLC | Privacy Policy