Stream: ideas

Topic: Terminal Colors


view this post on Zulip Luke Boswell (Dec 07 2022 at 02:55):

Screen-Shot-2022-12-07-at-13.53.03.png

view this post on Zulip Luke Boswell (Dec 07 2022 at 02:55):

I thought I would share a simple example to colorise the terminal when printing test results. It's nothing fancy, but it might save you time looking for the codes and how to print them in Roc. I think they look cool!

pass = withColor "PASS: " Green
fail = withColor "FAIL: " Red

Stdout.line ("\(pass)\(name)")
interface TerminalColor
    exposes [Color, escape, withColor]
    imports []

Color : [
    Green,
    Red,
    Blue,
    Black,
]

escape : Color -> Str
escape = \color ->
    when color is
        Green -> "\u(001b)[32m"
        Red -> "\u(001b)[31m"
        Blue -> "\u(001b)[34m"
        Black -> "\u(001b)[30m"

# Resets to black after
withColor : Str, Color -> Str
withColor = \text, color ->
    code = escape color
    black = escape Black
    "\(code)\(text)\(black)"

Last updated: Jun 16 2026 at 16:19 UTC