Skip to content

Commit

Permalink
Add char extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
importre committed May 23, 2017
1 parent 7bb1725 commit cb3d66a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
37 changes: 37 additions & 0 deletions src/main/kotlin/com/importre/crayon/Crayon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,40 @@ fun String.bgBrightMagenta() = "\u001b[45;1m${this}\u001b[0m"
fun String.bgBrightRed() = "\u001b[41;1m${this}\u001b[0m"
fun String.bgBrightWhite() = "\u001b[47;1m${this}\u001b[0m"
fun String.bgBrightYellow() = "\u001b[43;1m${this}\u001b[0m"

fun Char.bold() = "\u001b[1m${this}\u001b[0m"
fun Char.italic() = "\u001b[3m${this}\u001b[0m"
fun Char.underline() = "\u001b[4m${this}\u001b[0m"
fun Char.reversed() = "\u001b[7m${this}\u001b[0m"
fun Char.black() = "\u001b[30m${this}\u001b[0m"
fun Char.blue() = "\u001b[34m${this}\u001b[0m"
fun Char.cyan() = "\u001b[36m${this}\u001b[0m"
fun Char.green() = "\u001b[32m${this}\u001b[0m"
fun Char.magenta() = "\u001b[35m${this}\u001b[0m"
fun Char.red() = "\u001b[31m${this}\u001b[0m"
fun Char.white() = "\u001b[37m${this}\u001b[0m"
fun Char.yellow() = "\u001b[33m${this}\u001b[0m"
fun Char.brightBlack() = "\u001b[30;1m${this}\u001b[0m"
fun Char.brightBlue() = "\u001b[34;1m${this}\u001b[0m"
fun Char.brightCyan() = "\u001b[36;1m${this}\u001b[0m"
fun Char.brightGreen() = "\u001b[32;1m${this}\u001b[0m"
fun Char.brightMagenta() = "\u001b[35;1m${this}\u001b[0m"
fun Char.brightRed() = "\u001b[31;1m${this}\u001b[0m"
fun Char.brightWhite() = "\u001b[37;1m${this}\u001b[0m"
fun Char.brightYellow() = "\u001b[33;1m${this}\u001b[0m"
fun Char.bgBlack() = "\u001b[40m${this}\u001b[0m"
fun Char.bgBlue() = "\u001b[44m${this}\u001b[0m"
fun Char.bgCyan() = "\u001b[46m${this}\u001b[0m"
fun Char.bgGreen() = "\u001b[42m${this}\u001b[0m"
fun Char.bgMagenta() = "\u001b[45m${this}\u001b[0m"
fun Char.bgRed() = "\u001b[41m${this}\u001b[0m"
fun Char.bgWhite() = "\u001b[47m${this}\u001b[0m"
fun Char.bgYellow() = "\u001b[43m${this}\u001b[0m"
fun Char.bgBrightBlack() = "\u001b[40;1m${this}\u001b[0m"
fun Char.bgBrightBlue() = "\u001b[44;1m${this}\u001b[0m"
fun Char.bgBrightCyan() = "\u001b[46;1m${this}\u001b[0m"
fun Char.bgBrightGreen() = "\u001b[42;1m${this}\u001b[0m"
fun Char.bgBrightMagenta() = "\u001b[45;1m${this}\u001b[0m"
fun Char.bgBrightRed() = "\u001b[41;1m${this}\u001b[0m"
fun Char.bgBrightWhite() = "\u001b[47;1m${this}\u001b[0m"
fun Char.bgBrightYellow() = "\u001b[43;1m${this}\u001b[0m"
19 changes: 11 additions & 8 deletions utils/gen.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ fun default(background: Boolean, bright: Boolean) = colors
}
.sortedBy { it.first }

fun extension(clazz: String): List<String> = crayonMap
.filterNot { (k, _) -> k == "reset" }
.map { (name, code) ->
"""
fun $clazz.$name() = "$code${'$'}{this}${reset.first().second}"
""".trimIndent()
} + ""

val crayonMap = (reset + deco +
default(false, false) +
default(false, true) +
Expand All @@ -50,14 +58,9 @@ val ansi = listOf(
.map { (name, code) -> "const val $name = \"$code\"" } + ""

val crayon = listOf(
"@file:JvmName(\"Crayon\")", "",
"package com.importre.crayon", "") + crayonMap
.filterNot { (k, _) -> k == "reset" }
.map { (name, code) ->
"""
fun String.$name() = "$code${'$'}{this}${reset.first().second}"
""".trim()
} + ""
listOf("@file:JvmName(\"Crayon\")", "", "package com.importre.crayon", ""),
extension("String"),
extension("Char")).flatten()

File("../src/main/kotlin/com/importre/crayon/Ansi.kt")
.writeText(ansi.joinToString("\n"), Charsets.UTF_8)
Expand Down

0 comments on commit cb3d66a

Please sign in to comment.