S
Slash
I often do a lot of text-mode programming in Linux and wanted to use
colors in text mode, and found the ncurses library needlessly complex
for small applications. So I wrote my own little header to handle
colors in C++. (The code follows.)
This allows me to write code like
cout << gotoxy(10, 11)
<< color(lightcyan, blue)
<< "some text whatever";
I'd like to know how I could implement this more efficiently or in a
better way. Here are some questions I have:
(1) What exactly is a manipulator and how do I write one? How could a
manipulator help here?
(2) All that color and gotoxy do is simply output the corresponding
ANSI escape sequence string. How could I best make a color pair map
into its corresponding ANSI escape string?
(3) Could I selectively output the characters for gotoxy and color
(they simply output the corresponding ANSI escape sequences) only when
it is used with cout and not an ofstream object?
(4) How could I make this more easy to use?
Thanks in advance
-----------------------------------------------------
Here's the code:
//---------------------------------------------
// color.h
//---------------------------------------------
#ifndef COLOR_H
#define COLOR_H
#include <iostream>
using namespace std;
enum ColorName
{
black,
red,
green,
brown,
blue,
magenta,
cyan,
lightgray,
darkgray,
lightred,
lightgreen,
yellow,
lightblue,
lightmagenta,
lightcyan,
white
};
struct color
{
color(ColorName f = white, ColorName b = black)
:
fore(f),
back(b)
{}
ColorName fore;
ColorName back;
};
inline ostream& operator<<(ostream& o, const color& c)
{
if(c.fore > lightgray) // bold color
o << "\033[1;3" << c.fore - lightgray - 1 << "m";
else
o << "\033[0;3" << c.fore << "m";
return o << "\033[4" << c.back << "m";
}
struct gotoxy
{
gotoxy(int x_, int y_)
:
x(x_),
y(y_)
{}
int x;
int y;
};
inline ostream& operator<<(ostream& o, const gotoxy& g)
{
return o << "\033[" << g.y << ";" << g.x << "f";
}
#endif
colors in text mode, and found the ncurses library needlessly complex
for small applications. So I wrote my own little header to handle
colors in C++. (The code follows.)
This allows me to write code like
cout << gotoxy(10, 11)
<< color(lightcyan, blue)
<< "some text whatever";
I'd like to know how I could implement this more efficiently or in a
better way. Here are some questions I have:
(1) What exactly is a manipulator and how do I write one? How could a
manipulator help here?
(2) All that color and gotoxy do is simply output the corresponding
ANSI escape sequence string. How could I best make a color pair map
into its corresponding ANSI escape string?
(3) Could I selectively output the characters for gotoxy and color
(they simply output the corresponding ANSI escape sequences) only when
it is used with cout and not an ofstream object?
(4) How could I make this more easy to use?
Thanks in advance
-----------------------------------------------------
Here's the code:
//---------------------------------------------
// color.h
//---------------------------------------------
#ifndef COLOR_H
#define COLOR_H
#include <iostream>
using namespace std;
enum ColorName
{
black,
red,
green,
brown,
blue,
magenta,
cyan,
lightgray,
darkgray,
lightred,
lightgreen,
yellow,
lightblue,
lightmagenta,
lightcyan,
white
};
struct color
{
color(ColorName f = white, ColorName b = black)
:
fore(f),
back(b)
{}
ColorName fore;
ColorName back;
};
inline ostream& operator<<(ostream& o, const color& c)
{
if(c.fore > lightgray) // bold color
o << "\033[1;3" << c.fore - lightgray - 1 << "m";
else
o << "\033[0;3" << c.fore << "m";
return o << "\033[4" << c.back << "m";
}
struct gotoxy
{
gotoxy(int x_, int y_)
:
x(x_),
y(y_)
{}
int x;
int y;
};
inline ostream& operator<<(ostream& o, const gotoxy& g)
{
return o << "\033[" << g.y << ";" << g.x << "f";
}
#endif