In this blog post, we will look into using ANSI escape sequences to give colors for the terminal outputs in Gren Lang.
Standard escape codes are prefixed with \033
(Octal), \x1B
(Hex) or \u001B
(Unicode) followed by the command.
Gren Lang doesn’t support \0
or \x
as part of the string. So the only option is to use \u
(Unicode).
In order to get some basic text in red color, we will be using output string like below.
"\u{001B}[32mHello \u{001B}[0m"
Below is the sample program to write the text with ANSI escape sequence to stdout.
init :
Environment
-> Init.Task
{ model : Model
, command : Cmd Msg
}
init env =
Init.await Terminal.initialize
<| (\termConfig ->
Node.startProgram
{ model =
{ stdout = env.stdout
, stderr = env.stderr
}
, command =
Stream.sendLine env.stdout
<| "\u{001B}[32mHello \u{001B}[0;4;31mworld!\n\u{001B}[0m"
}
)
Here is output look like
You can look into the sample app on GitHub.
Versions of Language/packages used in this post.
Library/Language | Version |
---|---|
gren-lang | 0.3.0 |
gren-lang/node | 3.1.0 |
I hope it helped.
Thank You.