Printing terminal colors with writeln

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Printing terminal colors with writeln

Ryan Joseph-2
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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Printing terminal colors with writeln

geneb
On Thu, 26 Sep 2019, Ryan Joseph wrote:

> 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_!
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Printing terminal colors with writeln

Jonas Maebe-3
In reply to this post by Ryan Joseph-2
On 26/09/2019 22:41, Ryan Joseph wrote:
> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Printing terminal colors with writeln

Tomas Hajny-2
In reply to this post by Ryan Joseph-2
On 2019-09-26 22:41, Ryan Joseph wrote:

> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Printing terminal colors with writeln

Ryan Joseph-2
In reply to this post by Jonas Maebe-3


> On Sep 26, 2019, at 4:46 PM, Jonas Maebe <[hidden email]> wrote:
>
> 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');

that’s why! thanks.

Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal