问题
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.
回答1:
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…'
Once you pop up the 'edit scheme' screen, choose options
, then select run in terminal.
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.
来源:https://stackoverflow.com/questions/40752107/output-bold-text-to-console-in-c-on-mac-xcode