Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2019-09

Printing terminal colors with writeln

5 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ryan Joseph

In C I can do:

// https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-sequences
printf("\033[31;1;4mHello\033[0m");

and get red text in the terminal (MacOS). When I do the same thing with writeln and FPC it prints the literal characters with no colors. Why doesn’t this work the same in FPC I wonder?

Regards,
Ryan Joseph

Reply by email

geneb
to Ryan Joseph
Ryan Joseph wrote 9 lines
In C I can do: // https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-sequences printf("\033[31;1;4mHello\033[0m"); and get red text in the terminal (MacOS). When I do the same thing with writeln and FPC it prints the literal characters with no colors. Why doesn’t this work the same in FPC I wonder?

Because Pascal isn't C. :)

Try:
writeln(char(27) + '[31;1;4mHello' + char(27) + '[0m');

That might not work, but it's damn close to what would ;)

g.

Proud owner of F-15C 80-0007 http://www.f15sim.com - The only one of its kind. http://www.diy-cockpits.org/coll - Go Collimated or Go Home. Some people collect things for a hobby. Geeks collect hobbies. ScarletDME - The red hot Data Management Environment A Multi-Value database for the masses, not the classes. http://scarlet.deltasoft.com - Get it _today_!

Reply by email

Jonas Maebe
to Ryan Joseph: In C I can do: // https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-se…
Ryan Joseph wrote 7 lines
In C I can do: // https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-sequences printf("\033[31;1;4mHello\033[0m"); and get red text in the terminal (MacOS). When I do the same thing with writeln and FPC it prints the literal characters with no colors. Why doesn’t this work the same in FPC I wonder?

In C, \033 inside a string means "the ASCII character with octal value 033". In Pascal, \033 instead a string means "\033". Use this instead: writeln(#&033'[31;1;4mHello'#&033'[0m');

Jonas

Reply by email

Tomas Hajny
to Ryan Joseph: In C I can do: // https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-se…
Ryan Joseph wrote 10 lines
In C I can do: // https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-sequences printf("\033[31;1;4mHello\033[0m"); and get red text in the terminal (MacOS). When I do the same thing with writeln and FPC it prints the literal characters with no colors. Why doesn’t this work the same in FPC I wonder?

That depends on the state of the console / terminal window. Do you use unit Crt or unit Video?

Tomas

Reply by email