Kotlin DSL ANSI Output Colored Console
Just copy only one file com/github/mm/coloredconsole/ColoredConsole.kt into your project and use it.
-
styles - bold, italic, strike, underline, hidden, faint, reverse, blink
-
colors - black, white, red, blue, green, yellow, cyan, purple
-
background colors support
-
bright colors support
-
works with normal OS - Linux, Mac OS (not Windows)
println { "Hello World".cyan.bold }
// or
colored {
println("Hello World".cyan.bold)
}
println { "Hello World".cyan.bg }
// or
colored {
// use Cyan as background color
println("Hello World".cyan.bg)
}
// coloring/styling can be called on any object not just String
val pi = 22f/7
println { pi.blue.italic.underline }
// or
colored {
println(pi.blue.italic.underline)
}
// custom style: characters + or . can be used to group styles
val header = style { green + underline + bold }
println { "Hello World"(header) }
// or
println { "Hello World".style(header) }
colored {
// custom style: characters + or . can be used to group styles
val header = green + underline + bold
println("Hello World"(header))
// or
println("Hello World".style(header))
}
// prints all even numbers in Cyan color
println { listOf(1, 2, 3, 4, 5).joinToString { it.cyan { it.rem(2) == 0 } } }
// prints all even numbers in Cyan color
colored {
println(listOf(1, 2, 3, 4, 5).joinToString { it.cyan { it.rem(2) == 0 } })
}
// condition on style
colored {
val chapter = cyan + underline + bold
val chapterNumber = 12
println("$chapterNumber. Goodbye World"(chapter) { chapterNumber >= 10 })
// or
println("$chapterNumber. Goodbye World".style(chapter) { chapterNumber >= 10 })
}
println { ("bold " + ("italic " + ("color " + "Yellow".yellow.bold + " normal").faint + " italic").italic + " bold").bold }
colored {
println(("bold " + ("italic " + ("color " + "Yellow".yellow.bold + " normal").faint + " italic").italic + " bold").bold)
}
println(colored = true) { "Orange".yellow.bold + " Is the New " + "Black".bold.reverse }
or
colored(enabled = true) {
println("Orange".yellow.bold + " Is the New " + "Black".bold.reverse)
}
println(colored = true) { "Orange".yellow.bold + " Is the New " + "Black".bold.reverse }
or
colored(enabled = false) {
println("Orange".yellow.bold + " Is the New " + "Black".bold.reverse)
}
Your class can implement ColoredConsole and in that case you do not need to repeat colored { ... } block in each method but just use color/style directly.
class Weather(val degrees: Int) : ColoredConsole {
fun display() = println("Degrees:".blue.bold + " $degrees".italic.bold)
}
val style1 = style { blue.bright + bold }
println { "bright blue".style(style1) }
// or
println { "bright blue".blue.bright.bold }
colored {
val style1 = blue.bright + bold
println("bright blue".style(style1))
// or
println("bright blue".blue.bright.bold)
}
println { "cyan background".cyan.bg }
colored {
println("cyan background".cyan.bg)
}