Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Color enum #5

Merged
merged 1 commit into from
Jul 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
126 changes: 32 additions & 94 deletions src/main/kotlin/com/andreapivetta/kolor/Color.kt
Original file line number Diff line number Diff line change
@@ -1,100 +1,38 @@
package com.andreapivetta.kolor

import com.andreapivetta.kolor.Kolor.ESCAPE

/**
* Colors
* @author Andrea Pivetta
* The amount of codes required in order to jump from a foreground code to a background code. Equal to 10. For example,
* the foreground code for blue is "[33m", its respective background code is "[43m"
*/
enum class Color {
BLACK {
override fun ANSI() = "\u001B[30m"

override fun ANSIBackground() = "\u001B[40m"
},
RED {
override fun ANSI() = "\u001B[31m"

override fun ANSIBackground() = "\u001B[41m"
},
GREEN {
override fun ANSI() = "\u001B[32m"

override fun ANSIBackground() = "\u001B[42m"
},
YELLOW {
override fun ANSI() = "\u001B[33m"

override fun ANSIBackground() = "\u001B[43m"
},
BLUE {
override fun ANSI() = "\u001B[34m"

override fun ANSIBackground() = "\u001B[44m"
},
MAGENTA {
override fun ANSI() = "\u001B[35m"

override fun ANSIBackground() = "\u001B[45m"
},
CYAN {
override fun ANSI() = "\u001B[36m"

override fun ANSIBackground() = "\u001B[46m"
},
LIGHT_GRAY {
override fun ANSI() = "\u001B[37m"

override fun ANSIBackground() = "\u001B[47m"
},
DARK_GRAY {
override fun ANSI() = "\u001B[90m"

override fun ANSIBackground() = "\u001B[100m"
},
LIGHT_RED {
override fun ANSI() = "\u001B[91m"
private const val BG_JUMP = 10

override fun ANSIBackground() = "\u001B[101m"
},
LIGHT_GREEN {
override fun ANSI() = "\u001B[92m"

override fun ANSIBackground() = "\u001B[102m"
},
LIGHT_YELLOW {
override fun ANSI() = "\u001B[93m"

override fun ANSIBackground() = "\u001B[103m"
},
LIGHT_BLUE {
override fun ANSI() = "\u001B[94m"

override fun ANSIBackground() = "\u001B[104m"
},
LIGHT_MAGENTA {
override fun ANSI() = "\u001B[95m"

override fun ANSIBackground() = "\u001B[105m"
},
LIGHT_CYAN {
override fun ANSI() = "\u001B[96m"

override fun ANSIBackground() = "\u001B[106m"
},
WHITE {
override fun ANSI() = "\u001B[97m"

override fun ANSIBackground() = "\u001B[107m"
};

/**
* Get the ANSI foreground code
* @return The ANSI foreground code
*/
abstract fun ANSI(): String

/**
* Get the ANSI background code
* @return The ANSI foreground code
*/
abstract fun ANSIBackground(): String
/**
* An enumeration of colors supported by most terminals. Can be applied to both foreground and background.
*/
enum class Color(baseCode: Int) {
BLACK(30),
RED(31),
GREEN(32),
YELLOW(33),
BLUE(34),
MAGENTA(35),
CYAN(36),
LIGHT_GRAY(37),

DARK_GRAY(90),
LIGHT_RED(91),
LIGHT_GREEN(92),
LIGHT_YELLOW(93),
LIGHT_BLUE(94),
LIGHT_MAGENTA(95),
LIGHT_CYAN(96),
WHITE(97);

/** ANSI modifier string to apply the color to the text itself */
val foreground: String = "$ESCAPE[${baseCode}m"

/** ANSI modifier string to apply the color the text's background */
val background: String = "$ESCAPE[${baseCode + BG_JUMP}m"
}
7 changes: 4 additions & 3 deletions src/main/kotlin/com/andreapivetta/kolor/Kolor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@ package com.andreapivetta.kolor
* @author Andrea Pivetta
*/
object Kolor {
private const val RESET = "\u001B[0m"
internal const val ESCAPE = '\u001B'
internal const val RESET = "$ESCAPE[0m"

/**
* Create a string that will be printed with the specified color as foreground
* @param string The string to color
* @param color The color to use
* @return The colored string
*/
fun foreground(string: String, color: Color) = Kolor.color(string, color.ANSI())
fun foreground(string: String, color: Color) = Kolor.color(string, color.foreground)

/**
* Create a string that will be printed with the specified color as background
* @param string The string to color
* @param color The color to use
* @return The colored string
*/
fun background(string: String, color: Color) = Kolor.color(string, color.ANSIBackground())
fun background(string: String, color: Color) = Kolor.color(string, color.background)

private fun color(string: String, ansiString: String) = "$ansiString$string$RESET"
}
6 changes: 4 additions & 2 deletions src/test/kotlin/com/andreapivetta/kolor/KolorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.describe
import org.jetbrains.spek.api.dsl.it

import com.andreapivetta.kolor.Kolor.RESET

object KolorTest : Spek({
describe("foreground/background") {
it("should create a string that starts with an ANSI code and ends with the reset code") {
for (color in Color.values()) {
Kolor.foreground("foo", color).should.equal("${color.ANSI()}foo\u001B[0m")
Kolor.background("foo", color).should.equal("${color.ANSIBackground()}foo\u001B[0m")
Kolor.foreground("foo", color).should.equal("${color.foreground}foo$RESET")
Kolor.background("foo", color).should.equal("${color.background}foo$RESET")
}
}
}
Expand Down