corinthia-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gabriela Gibson <gabriela.gib...@gmail.com>
Subject Re: Colo(u)rs?
Date Tue, 26 May 2015 19:01:07 GMT
On Tue, May 26, 2015 at 12:47 PM, jan i <jani@apache.org> wrote:

> On 26 May 2015 at 00:13, Gabriela Gibson <gabriela.gibson@gmail.com>
> wrote:
>
> > On Mon, May 18, 2015 at 9:14 AM, jan i <jani@apache.org> wrote:
> >
> > > On 18 May 2015 at 01:48, Gabriela Gibson <gabriela.gibson@gmail.com>
> > > wrote:
> >
> > > >
> > > > 2) Is it portable?
> > > >
> > > I dont think so, for a number of reasons...I would not know how to do
> > color
> > > on a vt100 terminal in Linux or a dos prompt in windows.
> > >
> > > Hmm, but I guess it would work on a cygwin xterm in windows or on a
> mac?
> > As in, partially portable? ;-)
> >
> -1 for using cygwin, or even suggesting it.
>
> Uh oh, that bad, is it? ;-)  Shows how long ago I used DOS in earnest.


> You might have a look at the dos command prompt, in earlier days ANSI was
> supported, but
> it no longer is, however there is a utility that enables that. I have NO
> problem that we suggest
> developers to install that utility.
>
>
> http://stackoverflow.com/questions/16755142/how-to-make-win32-console-recognize-ansi-vt100-escape-sequences
>
>
Hmm, not sure how to set this all up (this is not my windows box) but in
the color.h file (see below) it can all be turned off, so it should not
affect windows if it can't be done.


> > > I am very much for the MACROS, and against a direct printf. Colors is
> > more
> > > a portability concern.
> > >
> >
> > I did some thinking today how to keep colour out of Corinthias' hair but
> > still be able to use it locally and had the idea that I should try and
> make
> > a dynamic library for the colors, so that I can easily use it everything
> I
> > do in C.  I'm sure someone already wrote a RollsRoyce doing just the same
> > thing, but, it was fun nevertheless and a good reason to learn about the
> > topic.  I am now proud owner of my very own /usr/local/lib/libcolor.so
> and
> > have started to use it -- profit! :>
> >
> I am -1 to add an extra library, this should be internal functions.
>
> I now figured out I can just make color.h and copy that to /usr/include
 and it works great. The simple printf macros probably have room for
improvement, but at least I finally gotten round to writing a macro in C ;-)

I chose 'normal' names for the colours because it's less to type and more
descriptive, and maybe I went a little over the top with the #define
SHOW_COLOR_HELP() , but I thought it may be useful.

Anyway, here is the 1st cut:

[[[

/////////////////////////////////////////////////////////////
//
// color.h -- a color utility
//
// To use this file globally via #include <color.h>
// copy it to /usr/include
//
// To see the color map and some help on this file,
// use SHOW_COLOR_HELP() to your code.
//
/////////////////////////////////////////////////////////////
#ifndef __COLOR_H
#define __COLOR_H
//
#ifndef COLOR_ON
#define COLOR_ON 1
#endif

#if COLOR_ON

#define RED        "\x1b[31m"
#define ORANGE     "\x1b[01;31m"
#define MOSS       "\x1b[32m"
#define GREEN      "\x1b[01;32m"
#define BROWN      "\x1b[33m"
#define YELLOW     "\x1b[01;33m"
#define NAVY       "\x1b[34m"
#define BLUE       "\x1b[01;34m"
#define PURPLE     "\x1b[35m"
#define PINK       "\x1b[01;35m"
#define TEAL       "\x1b[36m"
#define CYAN       "\x1b[01;36m"
#define BLACK      "\x1b[30m"
#define GREY       "\x1b[37m"
#define CHARCOAL   "\x1b[01;30m"
#define WHITE      "\x1b[01;37m"
#define RESET      "\x1b[0m"
#define COLOR_SIZEOF 10
//
#else
//
#define RED      ""
#define ORANGE   ""
#define MOSS     ""
#define GREEN    ""
#define YELLOW   ""
#define BROWN    ""
#define NAVY     ""
#define BLUE     ""
#define PURPLE   ""
#define PINK     ""
#define TEAL     ""
#define CYAN     ""
#define BLACK    ""
#define GREY     ""
#define CHARCOAL ""
#define WHITE    ""
#define RESET    ""
#define COLOR_SIZEOF 0
//
#endif // COLOR_ON
//

#define P_RED(X)      printf(RED "%s" RESET, X)
#define P_ORANGE(X)   printf(ORANGE "%s" RESET, X)
#define P_MOSS(X)     printf(MOSS "%s" RESET, X)
#define P_GREEN(X)    printf(GREEN "%s" RESET, X)
#define P_YELLOW(X)   printf(YELLOW "%s" RESET, X)
#define P_BROWN(X)    printf(BROWN "%s" RESET, X)
#define P_NAVY(X)     printf(NAVY "%s" RESET, X)
#define P_BLUE(X)     printf(BLUE "%s" RESET, X)
#define P_PURPLE(X)   printf(PURPLE "%s" RESET, X)
#define P_PINK(X)     printf(PINK "%s" RESET, X)
#define P_TEAL(X)     printf(TEAL"%s" RESET, X)
#define P_CYAN(X)     printf(CYAN "%s" RESET, X)
#define P_BLACK(X)    printf(BLACK "%s" RESET, X)
#define P_GREY(X)     printf(GREY "%s" RESET, X)
#define P_CHARCOAL(X) printf(CHARCOAL "%s" RESET, X)
#define P_WHITE(X)    printf(WHITE "%s" RESET, X)

#define SHOW_COLOR_HELP() {\
P_WHITE    ("Color map:\n");\
P_RED      ("RED, ");\
P_ORANGE   ("ORANGE, ");\
P_MOSS     ("MOSS, ");\
P_GREEN    ("GREEN, ");\
P_YELLOW   ("YELLOW, ");\
P_BROWN    ("BROWN, ");\
P_NAVY     ("NAVY, ");\
P_BLUE     ("BLUE, ");\
P_PURPLE   ("PURPLE, ");\
P_PINK     ("PINK,\n");\
P_TEAL     ("TEAL, ");\
P_CYAN     ("CYAN,");\
P_WHITE    ("--> ");\
P_BLACK    ("BLACK ");\
P_WHITE    ("<-- BLACK, ");\
P_GREY     ("GREY, ");\
P_CHARCOAL ("CHARCOAL, ");\
P_WHITE    ("WHITE, ");\
printf(RESET "RESET: Normal text\n\n");\
printf("Every color has a macro, add P_ to the name, eg.: P_PURPLE(\"Your
text\");\n");\
printf("To use a color in printf, do: printf(COLOR \"Text = %%d \" RESET,
variable);\n");\
printf("The #define COLOR_SIZEOF provides the string length of the
colors.\n");\
printf("To switch off colours, add #define COLOR_ON 0 before including
<color.h>.\n");\
printf("if you forget a RESET and your terminal is affected, type " PINK
"stty sane" RESET ".\n");\
}
#endif // __COLOR_H

]]]

Small test file for this opus:

[[[

#include <stdio.h>
// #define COLOR_ON 0    // uncomment to turn off colors


#include <color.h>

int main(int argc, char *argv[])
{
    SHOW_COLOR_HELP();
    return 0;
}

]]]


> >
> > This together with Jan's debug print macros idea makes me think that
> maybe
> > there is already such a library of a good debug system out there we could
> > use, or, if there isn't, perhaps separating this kind of service out
> would
> > not be the worst thing to do -- having a great debug mechanism that I
> know
> > and that just works everywhere for me would be brill.
> >
> +1 but integrated please.
>
> Would the trick above of a .h file  that can optionally be added to
/usr/include be ok?

G

Ps.: found on reddit:  'To get your owl to bloom, gently water it':
http://i.imgur.com/OrH8sDR.gif

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message