vahidzolf
vahidzolf

Reputation: 107

Colorize special words in a string

I have a linked list of nodes which described below:

class ColorGr
{
    string word;
    string color;
    ColorGr *next;
}

I have a string and I want to search for "word"s in it and colorize them with "color".

I tried ncurses to do that but the problem is with using windows. I don't want the screen being refreshed.

I want to print the string in output just like a cout function. My code is in c++ language and I work with gcc in linux. what is the best way to do this?

Upvotes: 0

Views: 209

Answers (2)

tozka
tozka

Reputation: 3451

On Windows, you can use console APIs, and manipulate colors:

  DWORD dummy = 0;
  const WORD color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; // gray
  HANDLE console = ::GetStdHandle (STD_OUTPUT_HANDLE);
  SetConsoleTextAttribute (console, color);
  WriteConsoleA (console, msg.data (), msg.length (), &dummy, NULL);

more colors here (link)

Or another way, for Linux, you can use ANSI color codes (not all terminals support, most (except for windows) should.)

e.g.

  fprintf (stdout, "\e[0;36m" "cyan colored text" "\e[0m");

Upvotes: 2

Khanal
Khanal

Reputation: 788

As far as the windows issue is concerned, I don't know if you haved looked at PDACurses, so here is a SO link just in case Ncurses workaround for windows.

Upvotes: 0

Related Questions