53 lines
1.5 KiB
Nim
53 lines
1.5 KiB
Nim
|
import tables
|
||
|
import strutils
|
||
|
import math
|
||
|
|
||
|
proc colorEsc(color: string): string {.gcsafe.} =
|
||
|
# Stored in proc to be gcsafe
|
||
|
let colors = {
|
||
|
"black": 30,
|
||
|
"red": 31,
|
||
|
"green": 32,
|
||
|
"orange": 33,
|
||
|
"blue": 34,
|
||
|
"purple": 35,
|
||
|
"cyan": 36,
|
||
|
"reset": 0
|
||
|
}.toTable()
|
||
|
|
||
|
return "\e[" & intToStr(colors[color]) & "m"
|
||
|
|
||
|
proc colorize(text: string, color: string): string {.gcsafe.} =
|
||
|
return colorEsc(color) & text & colorEsc("reset")
|
||
|
|
||
|
proc strCenter(text: string, filler: string = "-", length: int = 10): string {.gcsafe.} =
|
||
|
# Simply truncate and return if it fills the entire length
|
||
|
if text.len() > length:
|
||
|
return text[0..length-1]
|
||
|
|
||
|
# Find remaining characters and how much to pad each side
|
||
|
var remainHalf: float = (length - text.len()) / 2
|
||
|
var remainLeft: int = remainHalf.floor().int
|
||
|
var remainRight: int = remainHalf.ceil().int
|
||
|
|
||
|
return filler.repeat(remainLeft) & text & filler.repeat(remainRight)
|
||
|
|
||
|
proc logFormat(text: string, level: string = "undef"): string {.gcsafe.} =
|
||
|
# Stored in proc to be gcsafe
|
||
|
let colorMapping = {
|
||
|
"Main": "green",
|
||
|
"Error": "red",
|
||
|
"Info": "purple",
|
||
|
"Warning": "orange",
|
||
|
"undef": "cyan"
|
||
|
}.toTable()
|
||
|
|
||
|
var prefix = "[" & colorize(text=strCenter(level), color=colorMapping[level]) & "]"
|
||
|
return prefix & " " & text
|
||
|
|
||
|
proc logPrint(text: string = "Undefined error message", level: string = "undef") {.gcsafe.} =
|
||
|
# In separate function in case I need to for example also write it to a file in the future.
|
||
|
echo logFormat(text=text, level=level)
|
||
|
|
||
|
export logPrint
|