GithubHelp home page GithubHelp logo

andreacasalino / coloredstream Goto Github PK

View Code? Open in Web Editor NEW
3.0 4.0 0.0 117 KB

c++ stringstream handling colors

CMake 6.93% C++ 93.07%
colors text colours cout print terminal console color colour stream cpp ostream

coloredstream's Introduction

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

It is cross-platform and all the functionalities are contained in 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 code
  • specify a R,G,B triplet

It is also possible to apply a specific background color: temp

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:

#include <ColoredStream/ColoredStream.h>
using namespace colored_stream;

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

temp

and that's it!

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

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

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

temp

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

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

temp

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

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

temp

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:

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

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 for other examples.

CMAKE SUPPORT

You can fetch this package and link to the ColoredStream library, which will expose the position of ColoredStream.h:

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:

target_link_libraries(${TARGET_NAME}
    ColoredStream
)

coloredstream's People

Contributors

andreacasalino avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

coloredstream's Issues

std::out Interoperable ability

### This is a Feature Request!

Ok so this isn't a real issue, because I ended up making something that serves my purpose, but I thought I would still put in the request here, as its the main reason I used this library, and I am sure for others they will want the same thing from this library.

What I am asking for is the ability for this to work exactly the same way that std::cout does, but with predefined colors. What I mean is that you can plug in various variables and such and just keep chaining if you want, without having to define the color on every text block.

/// something like this would be awesome

colored_stream::Print << "this prints yellow" << "this is yellow too"
<< someVar << " even shows up yellow" << std::endl;

I have already made something for myself FROM this library, and it took me awhile to get it working, so I guess I don't absolutely need it, but I still think that this will really attract more users to this AWESOME library you have made here.

Anyways if you don't want to add this feature, its ok, but I wanted to at least say thank you for making this library public!

Here is a picture of what I made using your library that works the way I am requesting :D

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.