Publi

Escribir con colores en la terminal al estilo C++

Hace mucho, publicaba en esta web stermp, es una versión libre de la biblioteca conio para C. Esa biblioteca muy popular en los 90 escribía en colores por la consola, pero propietaria y sólo para entornos DOS y Windows.

Aunque cuando utilizamos C++, es normal querer utilizar un método más acorde con lo que estamos haciendo, es decir, utilizar streams.

Para ello, podemos utilizar el siguiente código:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class ccolor
{
 public:
  ccolor(unsigned short c)
    {
      setColor(c);
      background=-1;
    }

  ccolor(unsigned short fc, unsigned short bc)
    {
      setColor(fc);
      background=(bc<BRILLO_MIN)?ansicolors[bc & 15]+10:-1;
    }

  friend std::ostream& operator <<(std::ostream& out, ccolor c)
    {
      if (c.background>-1)
    out<<"\033["<<c.background<<"m";

      out<<"\033["<<c.attributes<<";"<<c.foreground<<"m";
      return out;
    }

  static const unsigned short  BLACK          = 0;
  static const unsigned short  BLUE           = 1;
  static const unsigned short  GREEN          = 2;
  static const unsigned short  CYAN           = 3;
  static const unsigned short  RED            = 4;
  static const unsigned short  MAGENTA        = 5;
  static const unsigned short  BROWN          = 6;
  static const unsigned short  LIGHTGRAY      = 7;
  static const unsigned short  DARKGRAY       = 8;
  static const unsigned short  LIGHTBLUE      = 9;
  static const unsigned short  LIGHTGREEN     = 10;
  static const unsigned short  LIGHTCYAN      = 11;
  static const unsigned short  LIGHTRED       = 12;
  static const unsigned short  LIGHTMAGENTA   = 13;
  static const unsigned short  YELLOW         = 14;
  static const unsigned short  WHITE          = 15;
  /* Special attributes */
  static const unsigned short  UNDERLINE      = 64;
  static const unsigned short  BLINK          = 128;

 private:
  inline void setColor(unsigned short c)
  {
    attributes=0;
    if (c & UNDERLINE) 
      attributes=UNDERLINE_ATTR;
    else if (c & BLINK)
      attributes=BLINK_ATTR;
    else if (c>=BRILLO_MIN)
      attributes=BRILLO_ATTR;

    foreground=ansicolors[c & 15];      /* Eliminamos los atributos */
  }
  /* Color codes */
  static const short ansicolors[16];

  /* Special attributes for letters */
  static const unsigned short  UNDERLINE_ATTR = 4;
  static const unsigned short  BLINK_ATTR     = 5;
  static const unsigned short  BRILLO_ATTR    = 1;

  /* Bright colors start on 9th color */
  static const unsigned short  BRILLO_MIN     = 9;

  unsigned short foreground;
  short background;
  unsigned short attributes;
};

inline std::ostream& endColor(std::ostream &s)
{
  return s<<"\033[00m";
}

const short ccolor::ansicolors[16]= {30, 34, 32, 36, 31, 35, 33, 37, 0, 34, 32, 36, 31, 35, 33, 37};

Hacemos lo mismo que en C, utilizar los códigos ANSI para representar colores en la terminal, aunque esta vez lo pasamos vía streams, por eso tenemos el operator::CODECOLORER_BLOCK_2::

Con esto sacaremos el texto «Hola mundo!» en amarillo, utilizaremos endColor cuando queramos volver a escribir en el mismo color inicial de la terminal (no seguir escribiendo en nuestros colores) y si queremos algo más complicado podemos hacer:

1
2
3
4
5
int main(int argc, char *argv[])
{
  cout << ccolor(ccolor::YELLOW | ccolor::UNDERLINE, ccolor::BLUE)<< "Hola mundo!"<<endColor<<endl;
  return 0;
}

donde asignamos el atributo UNDERLINE (subrayado) al texto que saldrá por la terminal, además, le pondremos fondo azul. Si miráis la clase ccolor, cuando la construimos con dos parámetros, podremos definir el color de primer plano y el de fondo.

También podría interesarte....

Only 1 comment left Ir a comentario

  1. Pingback: Bitacoras.com /

Leave a Reply