libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

AnsiColor.kt (2341B)


      1 /*
      2  * This file is part of LibEuFin.
      3  * Copyright (C) 2024 Taler Systems S.A.
      4  *
      5  * LibEuFin is free software; you can redistribute it and/or modify
      6  * it under the terms of the GNU Affero General Public License as
      7  * published by the Free Software Foundation; either version 3, or
      8  * (at your option) any later version.
      9  *
     10  * LibEuFin is distributed in the hope that it will be useful, but
     11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
     13  * Public License for more details.
     14  *
     15  * You should have received a copy of the GNU Affero General Public
     16  * License along with LibEuFin; see the file COPYING.  If not, see
     17  * <http://www.gnu.org/licenses/>
     18  */
     19 
     20 package tech.libeufin.common
     21 
     22 object ANSI {
     23     private val ANSI_PATTERN = Regex("\\u001B\\[[;\\d]*m")
     24 
     25     enum class Color(val code: Int) {
     26         BLACK(0),
     27         RED(1),
     28         GREEN(2),
     29         YELLOW(3),
     30         BLUE(4),
     31         MAGENTA(5),
     32         CYAN(6),
     33         WHITE(7)
     34     }
     35 
     36     /** Compute [msg] length without ANSI escape sequences */
     37     fun CharSequence.displayLength(): Int =
     38         splitToSequence(ANSI_PATTERN).sumOf { it.length }
     39 
     40     /** Format a [msg] using optionals [fg] and [bg] colors and optionally make the text [bold] */
     41     fun fmt(msg: String, fg: Color? = null, bg: Color? = null, bold: Boolean = false): String {
     42         if (fg == null && bg == null && !bold) return msg
     43         return buildString {
     44             fun next() {
     45                 if (last() != '[') {
     46                     append(';')
     47                 }
     48             }
     49 
     50             append("\u001b[")
     51             if (bold) {
     52                 append('1')
     53             }
     54             if (fg != null) {
     55                 next()
     56                 append('3')
     57                 append(fg.code.toString())
     58             }
     59             if (bg != null) {
     60                 next()
     61                 append('4')
     62                 append(bg.code.toString())
     63             }
     64             append('m')
     65             append(msg)
     66             append("\u001b[0m")
     67         }
     68     }
     69 
     70     fun red(msg: String) = fmt(msg, Color.RED)
     71     fun green(msg: String) = fmt(msg, Color.GREEN)
     72     fun yellow(msg: String) = fmt(msg, Color.YELLOW)
     73     fun magenta(msg: String) = fmt(msg, Color.MAGENTA)
     74     fun bold(msg: String) = fmt(msg, bold = true)
     75 }