3

Using ncurses in C++, how would I colour my text in a console/terminal without breaking MinGW support? Are there any other libraries that can do this? I know that you can do it with ASCII characters, but this isn't fully supported. I don't want to use the Windows API.

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
Will03uk
  • 3,346
  • 8
  • 34
  • 40

3 Answers3

2

The discussion in the other answer is regarding Windows, so for the benefit of Win readers, I'm leaving this answer even though it is not cross-platform.

The Old New Thing blog covers some of the Win32 API that can be used to change text color in the console. See FillConsoleOutputAttribute and Console Screen Buffers.

sean e
  • 11,792
  • 3
  • 44
  • 56
  • Although I would like it to work on windows as ubuntu is my OS I want to use something ncurses, but I can't find a good example – Will03uk Apr 20 '11 at 18:54
1

Your reference to mingw implies that you expect this to work in a Windows Cmd window. Unfortunately the Cmd window doesn't support color, except globally for the whole window.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • That's what I thought, would it break the program with ncurses or just show up as black text? – Will03uk Apr 20 '11 at 17:50
  • @Will03uk I've never tried ncurses on Windows so I don't know, sorry. – Mark Ransom Apr 20 '11 at 17:54
  • @Will03uk, my answer might not be correct. It appears that there's a direct interface to the console which does allow color coding, and I'm guessing ncurses for Windows would take advantage of it: http://en.wikipedia.org/wiki/ANSI_escape_code#Windows_and_DOS – Mark Ransom Apr 20 '11 at 18:00
  • Maybe this is of interest: http://stackoverflow.com/questions/899341/print-coloured-text-to-console-in-c (*shameless plug*) – Skurmedel Apr 20 '11 at 18:57
  • 1
    @Skurmedel, your solution looks good for the Windows case but the original question is for a cross-platform solution. – Mark Ransom Apr 20 '11 at 19:54
  • @Mark Ransom: I am blind as a bat :) – Skurmedel Apr 20 '11 at 20:41
  • @Skurmedel that's a windows way of using colour. I want it to work and windows MAC and Linux – Will03uk Apr 20 '11 at 21:30
0

I can't speak for NCurses, but I have used PDCurses to do color under winXP/MSVC 6, WIN7/MSVC 2010 in a console application.

EvilTeach
  • 28,120
  • 21
  • 85
  • 141