Browse Source

Implemented priority colors

Vadik Sirekanyan 1 week ago
parent
commit
7420e69d01

+ 7 - 3
src/commonMain/kotlin/org/sirekanyan/todo/ext/List.kt

@@ -4,9 +4,13 @@ fun <T> List<T>.print() {
     forEach(::println)
 }
 
-fun <T> List<T>.printNumbered(start: Int = 1) {
-    val padding = (lastIndex + start).toString().length
-    forEachIndexed { index, entry ->
+fun List<String>.printNumbered() {
+    printNumbered(map(String::colorize), start = 1)
+}
+
+private fun <T> printNumbered(list: List<T>, start: Int) {
+    val padding = (list.lastIndex + start).toString().length
+    list.forEachIndexed { index, entry ->
         println((index + start).toString().padStart(padding) + ' ' + entry)
     }
 }

+ 26 - 0
src/commonMain/kotlin/org/sirekanyan/todo/ext/String.kt

@@ -0,0 +1,26 @@
+package org.sirekanyan.todo.ext
+
+private val priorityRegex = Regex("\\([A-Za-z]\\)")
+
+fun String.colorize(): String {
+    val color = calculateColor(this) ?: return this
+    return "\u001b[${color.code};1m${this}\u001b[0m"
+}
+
+private fun calculateColor(entry: String): Color? {
+    val priority = priorityRegex.findAll(entry)
+        .map(MatchResult::value)
+        .map(String::uppercase)
+        .minOrNull()
+    return when (priority) {
+        null -> null
+        "(A)" -> Color.Red
+        "(B)" -> Color.Yellow
+        "(C)" -> Color.Green
+        else -> Color.Blue
+    }
+}
+
+private enum class Color(val code: Int) {
+    Red(31), Yellow(33), Green(32), Blue(34);
+}