An open API service indexing awesome lists of open source software.

https://github.com/andreacasalino/coloredstream

c++ stringstream handling colors
https://github.com/andreacasalino/coloredstream

color colors colour colours console cout cpp ostream print stream terminal text

Last synced: 4 months ago
JSON representation

c++ stringstream handling colors

Awesome Lists containing this project

README

          

This small package is intended to allow printing **colored** text into **C++** console applications.

Before doing anything else, leave a **star** to this project ;).

What you would see from the console using this package:

![temp](pictures/Sample.png)

It is cross-platform and all the functionalities are contained in [**ColoredStream.h**](./src/ColoredStream/ColoredStream.h).

The ability to show a colored text is made possible by building and printing a **ColoredText** object.
**ColoredText** extends **std::stringstream** adding the possibility to show colored text.
Such ability is enabled when passing a **ColoredText** instance to a **std::cout** or a **std::cerr** for printing something into the console. On the opposite, when passing a **ColoredText** instance to another kind of output stream, like for instance a **std::ofstream**, the object behaves like a normal **std::stringstream**, printing normal text.

Three possible color prescriptions can be specified:
- specify classical colors like **red** or **yellow**
- specify a [8-bit color](https://en.wikipedia.org/wiki/ANSI_escape_code) code
- specify a [R,G,B](https://en.wikipedia.org/wiki/ANSI_escape_code) triplet

It is also possible to apply a specific background color:
![temp](pictures/part04.png)

The kind of color used for specifying the text (R,G,B triplet, 8-bit code, etc...) can be different from the one used for the background.

## EXAMPLES

Still haven't left a **star**? Do it now!! ;).

Using this package is straightforward: you just need to create a **ColoredText** object and then pass it to **std::cout** (or **std::cerr**) for printing it.
Suppose for example you want to display a red colored 'hello world', all you need to do would be this:

```cpp
#include
using namespace colored_stream;

std::cout << ColoredText{ClassicColor::RED, "Hello world"} << std::endl;
```

![temp](pictures/part01.png)

and that's it!

You may also want to create an empty **ColoredText** instance, whose content is created step by step, before printing it:

```cpp
ColoredText colored_content(ClassicColor::RED);
colored_content << "Hello ";
colored_content << "World ";
colored_content << " :-)";

std::cout << colored_content << std::endl;
```

![temp](pictures/part02.png)

... or, you can pass a variadic number of inputs in order to build the object in place:

```cpp
std::cout << ColoredText{ClassicColor::BLUE, "Hello ", "World ", " :-)"}
<< std::endl;
```

![temp](pictures/part03.png)

You can also prescribe the background color and not only the text one:

```cpp
ColoredText stream{
Settings{}.text(ClassicColor::RED).background(Uint8Color{11}),
"Hello World"};
std::cout << stream << std::endl;
```

![temp](pictures/part04.png)

You can also decide to bind a certain color to a certain concrete **std::ostream** so that everything that will be passed to that stream will be rendered with a certain color. Indeed, you can use **ColoredStream** for this:
```cpp
ColoredStream stream{ClassicColor::MAGENTA, std::cerr};
stream << "All this line was "
<< "passed to the same "
<< " ColoredStream";

stream << " ... also this from another line of code" << std::endl;
```

![temp](pictures/part05.png)

The created stream will be a wrapper of the concrete passed one. Every time something is passed to the wrapper, the prescription about the text and the background color is added and then the content is actually propagated to the concrete stream.

Check the [samples folder](./samples) for other examples.

## CMAKE SUPPORT

You can fetch this package and link to the **ColoredStream** library, which will expose the position of [**ColoredStream.h**](./src/ColoredStream/ColoredStream.h):

```cmake
include(FetchContent)
FetchContent_Declare(
colored_stream
GIT_REPOSITORY https://github.com/andreacasalino/ColoredStream.git
GIT_TAG main
)
FetchContent_MakeAvailable(colored_stream)
```
and then link to the **ColoredStream** library:

```cmake
target_link_libraries(${TARGET_NAME}
ColoredStream
)
```