1

Lately I've been experimenting with coding basic programs in C using Xcode, and I've found myself looking for ways to italicize text or make it bold or coloured. Despite the numerous similar posts on SO, there hasn't been one to prove helpful to my situation, however I've found many examples for this in C++ (if that means anything). Perhaps it's not possible to format text in C using Xcode?

Particularly, I've read about using ANSI escape coding, but when I use this code: printf("\033[32;1mTest"), I end up with this as output: [32;1mTest. I believe this is because ANSI escape coding is not intended to be used on MacOS (just Linux).

Specifically, I'm looking for a way to output formatted text to the console using printf or some other method that prints text to the console, on a Mac. (Is this even possible?...)

Feel free to ask for any additional information as needed.

Nik
  • 1,664
  • 2
  • 14
  • 27
  • Try using `\e[32;1m` instead of `\033[32;1m`, it's more universal. Also you don't need the space between the 'm' and the 'Test'. – MD XF Nov 22 '16 at 22:18
  • @MDXF I just tried that and it had no effect. As for the extra space, that was just an error when writing code into the question, but thanks for reminding me – Nik Nov 22 '16 at 23:09
  • No problem, I'm not particularly experienced with Mac OS, just thought it might help. – MD XF Nov 22 '16 at 23:10
  • While you're at it you might as well try `\x1b` instead of `\033` and `\e`, I know this is a problem in other programming languages but it just might be applicable. – MD XF Nov 22 '16 at 23:13
  • @MDXF Unfortunately none of those produce a different result. If you need any more information, don't hesitate to ask – Nik Nov 22 '16 at 23:15
  • Is the code you're using simply the `printf` statement inside a hello-world type application? Or do you have more code? – MD XF Nov 22 '16 at 23:21
  • @MDXF Its currently in a blackjack game, but even when I try it in a simple program with only the `printf` the same result occurs – Nik Nov 22 '16 at 23:34
  • 1
    Are you running it in XCode and displaying in the internal console, or are you running it from the command line? The internal console displays the escape codes as text (i.e. what you're seeing there), rather than interpreting them as commands to alter the output. – Anya Shenanigans Nov 22 '16 at 23:44
  • @Petesh I am running it from the internal console. Should I not be? I just press **CMD-R** to run the program – Nik Nov 22 '16 at 23:46
  • 1
    The internal console doesn't interpret escape codes, so you'll not see the display of colors, bold - i.e. you'll not see the behaviour you expect. The escape codes would work just fine from `terminal`. You can use [edit scheme](http://stackoverflow.com/questions/19554822/xcode-command-line-tool-how-to-run-in-terminal) to get it to run in a terminal from xcode – Anya Shenanigans Nov 22 '16 at 23:53
  • @Petesh Thanks! Make an answer explaining this and I'll accept it! – Nik Nov 22 '16 at 23:56
  • @Petesh Follow up question: is it possible at all to colourize/bold/italicize text in the Xcode console? – Nik Nov 23 '16 at 00:01
  • Not that I'm aware of. Because the standard mechanism for changing colors, etc is to use escape codes, if they're being ignored on output, then you would not be able to do it. – Anya Shenanigans Nov 23 '16 at 00:08

1 Answers1

4

The default internal console for XCode is not a terminal, so it doesn't interpret escape codes. As a result you don't get to see the effects of bold/color changes.

On XCode 8, there is an option to edit the scheme to get the program to run in a terminal. Click on the pop-down to the right of the stop button, and select 'Edit Scheme…'

Edit Scheme Dropdown Target

Once you pop up the 'edit scheme' screen, choose options, then select run in terminal.

enter image description here

If you're just trying to get it to run from a terminal; you can launch terminal separately, then drag and drop the binary from the 'Products' section of the project navigator into the terminal (it will print out the full path to the binary in the terminal, and you can run it from there).

Colored emojis do display properly on the console; it's just that it doesn't respect color escape codes.

Community
  • 1
  • 1
Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122