Skip to content

marcelmatula/colored-console

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Colored Console

Kotlin DSL ANSI Output Colored Console

How to use it

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)

Simple Examples

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 Styles

// 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))
}

Conditional coloring

// 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 })
}

Nested coloring

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)
}

Enabling/Disable coloring

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)
}

Coloring for all class methods

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)
}

Bright Coloring

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)
}

Background Coloring

println { "cyan background".cyan.bg }
colored {
    println("cyan background".cyan.bg)
}

Releases

No releases published

Packages

No packages published

Languages