
Parses CSI/SGR ANSI escape sequences into styled text slices with color, intensity and style metadata; reconstructs plain text, iterates lines, and supports all 16 ANSI colors.
Categorise ANSI — Fast ANSI escape sequence parser and categoriser for Kotlin Multiplatform.
A faithful 1:1 transliteration of Kurt Lawrence's cansi Rust crate.
Terminal applications, build tools, test runners, and command-line interfaces emit ANSI escape sequences to format text with colors, font weights, and text styling. When consuming this output—such as rendering into terminal user interfaces (TUIs), logging to disk, or displaying in web interfaces—you often need to:
CategorisedSlice and v3 optional v3.CategorisedSlice).cansi-kotlin provides fast, pure-KMP parsing of ANSI CSI (Control Sequence Introducer) sequences, focusing on SGR (Select Graphic Rendition) parameters, built on io.github.kotlinmania:rust-lang-kotlin:0.3.0.
kotlin {
sourceSets {
commonMain.dependencies {
implementation("io.github.kotlinmania:cansi-kotlin:0.1.7")
}
}
}<dependency>
<groupId>io.github.kotlinmania</groupId>
<artifactId>cansi-kotlin</artifactId>
<version>0.1.7</version>
</dependency>import io.github.kotlinmania.cansi.categoriseTextV3
fun main() {
val input = "\u001b[31;1mHello\u001b[0m, \u001b[32mWorld!\u001b[0m"
val slices = categoriseTextV3(input)
for (slice in slices) {
println(
"Text: \"${slice.text}\" | " +
"Range: [${slice.start}, ${slice.end}) | " +
"FG: ${slice.fg} | " +
"Intensity: ${slice.intensity}",
)
}
}import io.github.kotlinmania.cansi.categoriseText
import io.github.kotlinmania.cansi.constructTextNoCodes
fun main() {
val styled = "\u001b[33mWarning:\u001b[0m Something \u001b[1mnotable\u001b[0m happened."
val slices = categoriseText(styled)
val plain = constructTextNoCodes(slices)
println(plain)
// Output: "Warning: Something notable happened."
}import io.github.kotlinmania.cansi.categoriseTextV3
import io.github.kotlinmania.cansi.v3.lineIter
fun main() {
val logOutput = "\u001b[34m[INFO] Task started.\nProcessing step 1...\nProcessing step 2...\u001b[0m"
for ((index, line) in lineIter(categoriseTextV3(logOutput)).withIndex()) {
println("--- Line $index ---")
for (slice in line) {
println(" \"${slice.text}\" (Color: ${slice.fg})")
}
}
}cansi-kotlin builds for all 17 retained KotlinMania multiplatform targets:
| Platform | Target Identifiers |
|---|---|
| JVM |
jvm (Java 21 toolchain) |
| Android |
android (AAR, API 24+), androidNativeArm64, androidNativeX64
|
| Apple |
macosArm64, iosArm64, iosSimulatorArm64, tvosArm64, tvosSimulatorArm64, watchosDeviceArm64, watchosSimulatorArm64
|
| Linux |
linuxX64, linuxArm64
|
| Windows | mingwX64 |
| Web |
js (Browser & Node.js), wasmJs (Browser & Node.js), wasmWasi (WASI) |
cansi-kotlin is a direct transliteration of the cansi Rust crate by Kurt Lawrence and contributors, maintained as part of KotlinMania.
Licensed under the MIT License.
Categorise ANSI — Fast ANSI escape sequence parser and categoriser for Kotlin Multiplatform.
A faithful 1:1 transliteration of Kurt Lawrence's cansi Rust crate.
Terminal applications, build tools, test runners, and command-line interfaces emit ANSI escape sequences to format text with colors, font weights, and text styling. When consuming this output—such as rendering into terminal user interfaces (TUIs), logging to disk, or displaying in web interfaces—you often need to:
CategorisedSlice and v3 optional v3.CategorisedSlice).cansi-kotlin provides fast, pure-KMP parsing of ANSI CSI (Control Sequence Introducer) sequences, focusing on SGR (Select Graphic Rendition) parameters, built on io.github.kotlinmania:rust-lang-kotlin:0.3.0.
kotlin {
sourceSets {
commonMain.dependencies {
implementation("io.github.kotlinmania:cansi-kotlin:0.1.7")
}
}
}<dependency>
<groupId>io.github.kotlinmania</groupId>
<artifactId>cansi-kotlin</artifactId>
<version>0.1.7</version>
</dependency>import io.github.kotlinmania.cansi.categoriseTextV3
fun main() {
val input = "\u001b[31;1mHello\u001b[0m, \u001b[32mWorld!\u001b[0m"
val slices = categoriseTextV3(input)
for (slice in slices) {
println(
"Text: \"${slice.text}\" | " +
"Range: [${slice.start}, ${slice.end}) | " +
"FG: ${slice.fg} | " +
"Intensity: ${slice.intensity}",
)
}
}import io.github.kotlinmania.cansi.categoriseText
import io.github.kotlinmania.cansi.constructTextNoCodes
fun main() {
val styled = "\u001b[33mWarning:\u001b[0m Something \u001b[1mnotable\u001b[0m happened."
val slices = categoriseText(styled)
val plain = constructTextNoCodes(slices)
println(plain)
// Output: "Warning: Something notable happened."
}import io.github.kotlinmania.cansi.categoriseTextV3
import io.github.kotlinmania.cansi.v3.lineIter
fun main() {
val logOutput = "\u001b[34m[INFO] Task started.\nProcessing step 1...\nProcessing step 2...\u001b[0m"
for ((index, line) in lineIter(categoriseTextV3(logOutput)).withIndex()) {
println("--- Line $index ---")
for (slice in line) {
println(" \"${slice.text}\" (Color: ${slice.fg})")
}
}
}cansi-kotlin builds for all 17 retained KotlinMania multiplatform targets:
| Platform | Target Identifiers |
|---|---|
| JVM |
jvm (Java 21 toolchain) |
| Android |
android (AAR, API 24+), androidNativeArm64, androidNativeX64
|
| Apple |
macosArm64, iosArm64, iosSimulatorArm64, tvosArm64, tvosSimulatorArm64, watchosDeviceArm64, watchosSimulatorArm64
|
| Linux |
linuxX64, linuxArm64
|
| Windows | mingwX64 |
| Web |
js (Browser & Node.js), wasmJs (Browser & Node.js), wasmWasi (WASI) |
cansi-kotlin is a direct transliteration of the cansi Rust crate by Kurt Lawrence and contributors, maintained as part of KotlinMania.
Licensed under the MIT License.