Coloured text in the Terminal from a Console app

How do I output coloured text to the Terminal from a console application?

I did a bit of looking on Stack Overflow and tried the following in the Run() method of an empty Console app on 10.11:

Print("\\033[1;31mbold red text\\033[0m\ ")

But all that’s output is Print("\033[1;31mbold red text\033[0m
") in white.

Am I missing something?

The above code was taken from this post.

StdOut.Write() or StdOut.WriteLine() instead of Print() doesn’t work either…

[quote]\a audible bell byte 0x07 in ASCII encoding
\b backspace byte 0x08 in ASCII encoding[/quote]

so Im guessing c = 9, d = 10, e = 11

Try

Print chrb(11) + “[31mRed”"

Did you read the content of that post and try to comprehend it or did you just copy and paste the example code?

Take a minute to read the answer and understand why it works. Then you will understand how to make it work in Xojo. All of the answers you need are in that post. It does indeed work.

It does work in OS X Terminal. It does not in a Xojo console application.

And the quoted post says:

So you should be able to do the same with

[code]const FG_RED = “31”
const FG_GREEN = “32”
const FG_BLUE = “34”
const FG_DEFAULT = “39”
const BG_RED = “41”
const BG_GREEN = “42”
const BG_BLUE = “44”
const BG_DEFAULT = “49”

Print chrb(27) + “[” + FG_RED +“m” + “Red Text”[/code]

I really wish you would have let the why settle in rather than give a code example.

?

Me?
I don’t understand.
Is this homework or something?

This seems to be good old ESC sequences.

http://ascii-table.com/ansi-escape-sequences.php

Thanks for the help guys.

I get the feeling @Tim Parnell thinks I was being a bit lazy in not reading the question fully. To make up for it, I put together a simple module (get it from Github) called ShellFormat that you can drop into a ConsoleApplication to simplify the process of adding formatted text.

There are two methods, one is just for setting colours (Colourise), the other also allows you to set the bold and underline status.

Example usage:

[code] using ShellFormat

Print "This is " + Colourise(“red text”, ShellColor.Red) + “.”
Print Colourise(“Here’s white on a blue background”, ShellColor.White, ShellColor.Blue) + “.”

Print "This is " + Formatted(“blue & underlined”, False, True, ShellColor.Blue, ShellColor.Default)
Print "This is " + Formatted(“green & bold”, True, False, ShellColor.Green, ShellColor.Default)[/code]

Hopefully someone will find it useful.

Thanks for the help guys.

I get the feeling @Tim Parnell thinks I was being a bit lazy in not reading the question fully. To make up for it, I put together a simple module (get it from Github) called ShellFormat that you can drop into a ConsoleApplication to simplify the process of adding formatted text.

There are two methods, one is just for setting colours (Colourise), the other also allows you to set the bold and underline status.

Example usage:

[code] using ShellFormat

Print "This is " + Colourise(“red text”, ShellColor.Red) + “.”
Print Colourise(“Here’s white on a blue background”, ShellColor.White, ShellColor.Blue) + “.”

Print "This is " + Formatted(“blue & underlined”, False, True, ShellColor.Blue, ShellColor.Default)
Print "This is " + Formatted(“green & bold”, True, False, ShellColor.Green, ShellColor.Default)[/code]

Hopefully someone will find it useful.

Thanks Jeff for the nudge in the right direction.

Hi Guys,

Sorry to bring up such an old thread, but…

Anyone got the source code, the github repo has gone AWOL!

Thanks.

Found this with a Google search

https://github.com/gkjpettet/dk2image/blob/master/ShellFormat.xojo_code

Thanks

You’re welcome.

@James Dooley : Here’s my refined version:

Thanks.