472,378 Members | 1,234 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 software developers and data experts.

Design question: A little C++ header for colorizing text in Linux -- Comments/Ideas

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
Jul 19 '05 #1
2 4752
"Slash" <de******@hotmail.com> wrote...
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.
I think your implementation is good enough. What inefficiencies do
you see in it? What is not good about it, in your opinion?
Here are some questions I have:

(1) What exactly is a manipulator and how do I write one? How could a
manipulator help here?
You have successfully written two manipulators. Why are you asking
about how to write them?
(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?
What don't you like about the way you do it now? I can see only one
marginally acceptable improvement: a table of ANSI strings instead
of the calculation. Still, to prove that it's an improvement, one
would need to profile both versions.
(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?
No, for all intents and purposes 'std::cout' _could_ be a file. It
is impossible to tell without being platform-specific, AFAIK.

(4) How could I make this more easy to use?


It's easy as it is. How much easier do you expect it to get?

Keep in mind, though, that your code is NOT going to work on systems
that do not have ANSI terminals (and those are quite a few).

Victor
Jul 19 '05 #2
de******@hotmail.com (Slash) writes:
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:
You still need libncurses, or libslang, or terminfo. If you don't,
you can't cope with non-ANSI terminals or output redirection to
non-terminal destinations.

ncurses isn't /that/ difficult. Your existing API is quite good, and
should wrap ncurses with little difficulty. You could have a base
object representing a window, and do

window << color(blue, yellow) << message;

and have this wrap printw() et. al..
(1) What exactly is a manipulator and how do I write one? How could a
manipulator help here?
Do you mean in the sense of <iomanip>? These are used to control
formatting of numbers, field widths, alignment etc. I don't think you
need to use them in your header, but users of the header would use
them to format their output appropriately.

If you output to your own class (rather than a std::ostream) you could
use your own custom manipulators to set the colours, coordinates,
etc..
(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?
I'd use ncurses and a defined set of COLOR_PAIRs: use an enum for the
defined values (it looks like you are already doing this).
(4) How could I make this more easy to use?


Personally, I'd prefer to use an ncurses-based toolkit that provided a
higher-level abstraction of terminal I/O, providing widgets, input
focus, container-based widget alignment and sizing etc.. Essentially,
I mean an object-oriented toolkit after the likes of GTK+/Gtkmm or
Swing.
--
Roger Leigh

Printing on GNU/Linux? http://gimp-print.sourceforge.net/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Patchwork | last post by:
Hi Everyone, I have a design related question (in C++) that I am hoping someone can help me with. It is related to my previous post but since it was pointed out that I was more or less asking...
4
by: Roland Hall | last post by:
If I wanted to write my own blog application, what functionality should it contain? TIA... -- Roland Hall /* This information is distributed in the hope that it will be useful, but without...
29
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this...
4
by: Fried Egg | last post by:
Hi Pythonist(a/o)s I am interested if anyone can shed any light on a web application problem, both in the specific details (see below) but also in the theory of how to do ad hoc data processing...
5
by: A_M_IS | last post by:
Dear valuable experts, I truly hope than You can suggest for me Your ideas how to resolve design. I developing relative small Access VB tool, for single user use only. Access version 2003, but db...
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
10
by: dtmfcc | last post by:
My website is at www.simi-therapy.com My CSS is at http://www.simi-therapy.com/simitherapy-screen.css Question -- on the dark blue bar under the beach image, one change caused another...
25
by: Rehceb Rotkiv | last post by:
Hello everyone, I'm using Linux since quite a while now and I'm happy to notice that I'm beginning to "know my way round". I can write little bash, sed and awk scripts to help me with my...
18
by: Steve Phillips | last post by:
Hi All, I am just wondering what seems to be the most popular IDE. The reason I ask is I am currently at war with myself when it comes to IDE's. It seems like every one I find and try out has...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.