Is it possible to color the console output in just plain ANSI C? Without an external library? Can this be done in Windows, Linux, or Mac OS X?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
It is true that ISO C knows nothing about the console being capable of displaying colors, however there is an ANSI norm for console capabilities management, based on escape character controls. This works transparently in Linux and Mac OS X, however it fails in Windows, in which you need to use the primitives of the Win32 API.
You can find below a very simple library that allows to clear the screen, show colors and locate the cursor in a specific coordinate, in a multiplatform way (Win32 & Unix-like systems).
It comes with plain C source files (.c and .h), doxygen documentation in Spanish (doc/), and a simple demo (main.c)
http://github.com/Baltasarq/cscrutil/
Yes, in Linux/ Mac it is possible using ANSI C89. You can either manipulate the font and the color of the text. using the following command:
Notice that the left part of the ";" (where the numbers 0, 1 are) manipulates the text font, the right part of ";" manipulates the colors. You can experiment on your own and find out new colors.
This code compiles using
"-ansi -pedantic"
command with no warnings nor errors.***** Edit ***** In Windows based systems you can achieve colorful console text/background of text using the following example:
Good luck!
in Linux this can be done, if you you know the shell-specific control codes / Escape sequences.
Linux/OSX/Unix
On posix systems you can use the ANSI escape sequences.
Windows
On windows it is a bit more complicated, there are multiple solutions:
Win32 API
Using the Win32 API to set the output color before printing to the console using SetConsoleTextAttribute and friends. This is a lot more cumbersome than simply embedding ANSI escape sequences in your strings, and requires you to handle Windows as a special case.
Windows ANSI.SYS and Replacement
Older version of windows contained ANSI.SYS, but this has been removed in later versions. ANSICON is a replacement for this that you can install to get ANSI color code support in the windows command prompt: https://github.com/adoxa/ansicon
Embeddable no external dependencies solution
Here is a project that can be easily integrated into any existing project without relying on ANSI.SYS or ANSICON being installed.
It takes a string containing ANSI escape sequences and translates them to the relevant Win32 equivalent API functions: https://github.com/mattn/ansicolor-w32.c
No. The C standard doesn't assume the stdout is a console or has color.
Yes. See How can I print to the console in color on Mac OS X in a cross-platform manner? for Linux and Mac OS X.
For Windows, you may need to directly access the Console Functions if you want to avoid external libraries.