Skip to content
Full-featured text styling for Kotlin command-line applications
Branch: master
Clone or download
Latest commit bdb4cee Mar 18, 2019

README.md

Mordant

Dead simple text styling for command-line applications

/mɔː(ɹ)dənt/ A substance used to set (i.e. bind) colored dyes on fabrics 1

Mordant has:

  • An easy, configuration-free, API
  • Support for nesting styles and colors
  • Automatic detection of terminal color support
  • Support for 256 and 24-bit colors, with automatic downsampling
  • Support for specifying colors in every color space supported by colormath

Usage

val t = TermColors()
println(t.red("This text will be red on terminals that support color"))

Multiple styles

with(TermColors()) {
    println("${red("red")} ${white("white")} and ${blue("blue")}")
}

Foreground and background colors

with(TermColors()) {
    println((yellow on brightGreen)("this is easy to read, right?"))
}

Background color alone

with(TermColors()) {
    println("The foreground ${brightBlue.bg("color will stay the")} same")
}

Combine styles and colors

with(TermColors()) {
    val style = (bold + white + underline)
    println(style("You can save styles"))
    println(style("to reuse"))
}

Nest styles and colors

with(TermColors()) {
    println(white("You ${(blue on yellow)("can ${(black + strikethrough)("nest")} styles")} arbitrarily"))
}

True color and other color spaces

with(TermColors()) {
    println(rgb("#b4eeb4")("This will get downsampled on terminals that don't support truecolor"))
}

with(TermColors()) {
    for (v in 0..100 step 4) {
        for (h in 0..360 step 4) {
            print(hsv(h, 100, 100 - v).bg(" "))
        }
        println()
    }
}

Terminal color support detection

By default, TermColors() will try to detect ANSI support in the current stdout stream. If you'd like to override the detection, you can pass a specific value to the TermColors constructor.

For example, to always output ANSI RGB color codes, even if stdout is currently directed to a file, you can do this:

TermColors(TermColors.Level.TRUECOLOR)

API Documentation

API docs are hosted on JitPack.

Installation

Mordant is distributed through Maven Central, Jcenter and Jitpack.

dependencies {
   implementation 'com.github.ajalt:mordant:1.2.1'
}

License

Copyright 2018-2019 AJ Alt

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
You can’t perform that action at this time.