Stream: beginners

Topic: terminal unicode / colors


view this post on Zulip Ghislain (Oct 18 2022 at 15:41):

Hi,

Is there an equivalent of this C line in Roc? I would like to use colors and cursor motion.

printf("\033[%dm %3d\033[m", n, n);

view this post on Zulip Brendan Hansknecht (Oct 18 2022 at 15:45):

Don't have a full answer, but probably not really tested yet.

The first %d is pretty straight forward:

xStr = Num.toStr x
"\(xStr)"

view this post on Zulip Brendan Hansknecht (Oct 18 2022 at 15:46):

We don't currently have something like %3d to my knowledge. You would have to make it programmatically.

view this post on Zulip Ghislain (Oct 18 2022 at 15:46):

oh, I meant the color thing, not the params

view this post on Zulip Brendan Hansknecht (Oct 18 2022 at 15:46):

The rest, not sure what is/isn't supported currently

view this post on Zulip Brendan Hansknecht (Oct 18 2022 at 15:47):

It might depend on the platform since we would be passing a Str to the platform

view this post on Zulip Ghislain (Oct 18 2022 at 15:48):

I took it from here https://en.wikipedia.org/wiki/ANSI_escape_code#In_C

view this post on Zulip Ghislain (Oct 18 2022 at 16:00):

I also tried this (which works in Rust):

println!("\x1b[93mHello\x1b[0m");

But compiler complains that the escape sequence is not recognized

view this post on Zulip Brendan Hansknecht (Oct 18 2022 at 16:17):

Ah. Ok. Yeah i think we currently have limited escape sequences. Not sure the plan here.

view this post on Zulip Brendan Hansknecht (Oct 18 2022 at 16:18):

@Richard Feldman do we have a way to support arbitrary Unicode? For you have to convert from int to str or should it be escapable?

view this post on Zulip Richard Feldman (Oct 18 2022 at 16:20):

we already do, although it may not be documented

view this post on Zulip Richard Feldman (Oct 18 2022 at 16:21):

I'm on my phone and haven't used it in over a year, but if I remember right it's "\u(1234)"

view this post on Zulip Richard Feldman (Oct 18 2022 at 16:23):

yep, that's it!

https://github.com/roc-lang/roc/blob/d743c6954a4de20cafd0fcfd84cf3be33cae6628/crates/compiler/parse/tests/test_parse.rs#L436

view this post on Zulip Brendan Hansknecht (Oct 18 2022 at 16:26):

Awesome!


Last updated: Jul 06 2025 at 12:14 UTC