0

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?

Peter Wood
  • 23,859
  • 5
  • 60
  • 99
vahidzolf
  • 109
  • 1
  • 1
  • 13
  • Cout itself wont be able to help you, it depends on where you are printing to. You should try to use ncurses (lookup cygwin) or try to find something that works with windows cmd – Karthik T Jan 22 '13 at 07:29
  • @KarthikT That's only partially true. He can add formatting state to `std::cout` using its `iword` and `pword` members, and define custom manipulators. Or he can interpose a custom `std::streambuf` subclass to colorize the words as they're going to the screen. But the DOS text buffer support alone is probably enough for this one question. – Potatoswatter Jan 22 '13 at 07:52

2 Answers2

2

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");
Ajay
  • 18,086
  • 12
  • 59
  • 105
tozka
  • 3,211
  • 19
  • 23
  • thanks but is it possible to pass parameters to printf function ? for example string s="36m" and pass it the function ? – vahidzolf Jan 22 '13 at 10:43
  • Ofcourse, it is certainly possible : `fprintf (stdout, "%s" "cyan colored text" "\e[0m", "\e[0;36m");` – tozka Jan 22 '13 at 12:11
0

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.

Community
  • 1
  • 1
Khanal
  • 788
  • 6
  • 14
  • I code in linux and I think there's a misunderstanding here . I meant window concept in ncurses not Windows operating system in my question anyway I appreciate your answer. – vahidzolf Jan 22 '13 at 10:54