rlutil – a cross-platform library to change console text color in C/C++

Dissecting the rlutil library to colorize the text.
Last month ago, while I was searching for cross-platform ways to change console text color in C++, something in the fashion of vim text editor, then I found a very good open-source library, called rlutil, which can be used to change console text color in console-based applications (vocabulary builders, text editors like vim, etc.) and for easing the creation of roguelike games.
It is cross-platform library, so your code can run on both Windows and Linux, and the effect will be the same; that is, your text will be colorized!

rlutil is a single header library and hosted on Github, so you can easily clone the source codes in your hard drive. I found the API of rlutil very easy to use; you do not need to compile the library; it is a single header file library, so just include the rlutil.h in your directory of source file and call simple functions to change the color of your text!

In this post, I will show you how you can┬áchange console text color through rlutil to add fun to your boring console programs. Let’s get started!

rlutil – a cross-platform library to change console text color in C/C++

Create a new folder “rlutil-test” and within the folder, create a new file “main.cpp” (the rlutil.h should be in the same directory as your main.cpp file so copy the file if you haven’t yet)


Assuming the file is saved as main.cpp , you can easily compile and run the program using g++ as follows:

My linux console shows the following:

rlutil example

You can see how easy it is to change the color of console text using rlutil library!

I have tested the code on both Linux and Windows and the result was the same. Note that if you are compiling through C compiler, then the library will automatically remove the namespaces, so you won’t need to write rlutil:: when using the functions.

Apart from functions for changing the color of console text, the library also has many utility functions such as rlutil::cls() for clearing the console screen, rlutil::anykey() to check if a user presses a key, rlutil::hidecursor() and rlutil::showcursor() for hiding and showing the cursor respectively, rlutil::msleep() for sleeping the CPU in a cross-platform way, rlutil::getkey() for reading the pressed keys, etc.

What do you think of this library? Does it provide a better API to change the console text color for console-based programs than the libraries you use?