Skip to content

storing color in variable #103

@Lecrapouille

Description

@Lecrapouille

Hi ! this is not an issue. I get inspired by your library. I need to store style and fg color in a variable for a postponed usage. Here is the idea:

rang::color mycolor = std::pair(rang::red, rang::italic);
std::cout << mycolor << "hello" << std::endl;

So why not adding in your lib something like:

struct color
  {
    color(rang::style style,
          rang::fg fg,
          rang::bg bg = rang::bg::reset)
      : m_style(style), m_fg(fg), m_bg(bg)
    {
    }

    rang::style m_style;
    rang::fg m_fg;
    rang::bg m_bg;
  };

inline std::ostream& setColor(std::ostream &os, rang::color const c)
  {
    return os << "\033["
      << static_cast<int>(c.m_fg) << ';'
      << static_cast<int>(c.m_bg) << ';'
      << static_cast<int>(c.m_style) << "m";
  }

A simple test code could be:

#define ERROR_COLOR  rang::color(rang::style::bold, rang::fg::red)
rang::color c = ERROR_COLOR;
std::cout << c << "hello" << std::endl;

I dunno what could be the best: std::pair (ugly and long syntax but can be used with constexpr) or struct (drawback: make copy). personally I would prefer something like red | italic (but can achieve easily). Other solution ?

(Maybe your lib already can do this, in this case ignore my comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions