/usr/include/tulip/ConsoleUtils.h is in libtulip-dev 4.6.0dfsg-2+b5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 | /*
*
* This file is part of Tulip (www.tulip-software.org)
*
* Authors: David Auber and the Tulip development Team
* from LaBRI, University of Bordeaux
*
* Tulip is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* Tulip is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
*/
///@cond DOXYGEN_HIDDEN
#ifndef CONSOLE_UTILS
#define CONSOLE_UTILS
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <map>
#ifdef WIN32
#include <windows.h>
#include <io.h>
#endif
#if defined(__unix__) || defined(__APPLE__)
#include <termios.h>
#include <sys/ioctl.h>
#include <unistd.h>
static struct termios stored_settings;
static void setConsoleSettingsForAnsiRequest(void) {
struct termios new_settings;
tcgetattr(0, &stored_settings);
new_settings = stored_settings;
new_settings.c_lflag &= ~ICANON;
new_settings.c_lflag &= ~ECHO;
tcsetattr(0, TCSANOW, &new_settings);
}
static void resetConsoleSettings(void) {
tcsetattr(0, TCSANOW, &stored_settings);
}
static bool processInForeground(void) {
pid_t fg = tcgetpgrp(STDIN_FILENO);
return fg == getpgrp();
}
static std::pair<int, int> getConsoleCursorPosition() {
int row=0, col=0;
if (isatty(fileno(stdin)) && processInForeground()) {
setConsoleSettingsForAnsiRequest();
fprintf(stdout, "\x1b[6n");
if (fscanf(stdin, "\x1b[%d;%dR", &row, &col)) {}
resetConsoleSettings();
}
return std::make_pair(row, col);
}
static std::pair<int, int> getConsoleSize() {
int rows=-1, cols=-1;
struct winsize ws;
if (ioctl(0,TIOCGWINSZ,&ws)==0) {
rows = ws.ws_row;
cols = ws.ws_col;
}
return std::make_pair(rows, cols);
}
#elif defined(WIN32)
inline std::pair<int, int> getConsoleCursorPosition() {
int row=0, col=0;
CONSOLE_SCREEN_BUFFER_INFO SBInfo;
if(GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &SBInfo)) {
row = SBInfo.dwCursorPosition.Y;
col = SBInfo.dwCursorPosition.X;
}
return std::make_pair(row, col);
}
inline std::pair<int, int> getConsoleSize() {
int rows=200, cols=200;
CONSOLE_SCREEN_BUFFER_INFO SBInfo;
if(GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &SBInfo)) {
rows = SBInfo.dwSize.Y;
cols = SBInfo.dwSize.X-1;
}
return std::make_pair(rows, cols);
}
#endif
enum TextEffect {NORMAL=1, BOLD=2, UNDERLINED=4, BLINK=8};
enum TextColor {BLACK, RED, GREEN, YELLOW, BLUE,
MAGENTA, CYAN, LIGHT_GRAY, DARK_GRAY,
LIGHT_RED, LIGHT_GREEN, LIGHT_YELLOW,
LIGHT_BLUE, LIGHT_MAGENTA, LIGHT_CYAN, WHITE
};
class TextFgColorSetup {
public :
TextFgColorSetup(TextColor color) :
color(color) {}
TextColor color;
};
class TextBgColorSetup {
public :
TextBgColorSetup(TextColor color) :
color(color) {}
TextColor color;
};
class TextEffectSetup {
public :
TextEffectSetup(int effect) :
effect(effect) {}
int effect;
};
static std::map<TextColor, std::string> initAnsiFgColors() {
const char *colorterm = getenv("COLORTERM");
bool rxvt = colorterm && std::string(colorterm).find("rxvt") != std::string::npos;
std::map<TextColor, std::string> ret;
ret[BLACK] = "30";
ret[RED] = "31";
ret[GREEN] = "32";
ret[YELLOW] = "33";
ret[BLUE] = "34";
ret[MAGENTA] = "35";
ret[CYAN] = "36";
ret[LIGHT_GRAY] = "37";
ret[DARK_GRAY] = rxvt ? ret[BLACK] : "90";
ret[LIGHT_RED] = rxvt ? ret[RED] : "91";
ret[LIGHT_GREEN] = rxvt ? ret[GREEN] : "92";
ret[LIGHT_YELLOW] = rxvt ? ret[YELLOW] : "93";
ret[LIGHT_BLUE] = rxvt ? ret[BLUE] : "94";
ret[LIGHT_MAGENTA] = rxvt ? ret[MAGENTA] : "95";
ret[LIGHT_CYAN] = rxvt ? ret[CYAN] : "96";
ret[WHITE] = rxvt ? ret[LIGHT_GRAY] : "97";
return ret;
}
static std::map<TextColor, std::string> initAnsiBgColors() {
const char *colorterm = getenv("COLORTERM");
bool rxvt = colorterm && std::string(colorterm).find("rxvt") != std::string::npos;
std::map<TextColor, std::string> ret;
ret[BLACK] = "40";
ret[RED] = "41";
ret[GREEN] = "42";
ret[YELLOW] = "43";
ret[BLUE] = "44";
ret[MAGENTA] = "45";
ret[CYAN] = "46";
ret[LIGHT_GRAY] = "47";
ret[DARK_GRAY] = rxvt ? ret[BLACK] : "100";
ret[LIGHT_RED] = rxvt ? ret[RED] : "101";
ret[LIGHT_GREEN] = rxvt ? ret[GREEN] : "102";
ret[LIGHT_YELLOW] = rxvt ? ret[YELLOW] : "103";
ret[LIGHT_BLUE] = rxvt ? ret[BLUE] : "104";
ret[LIGHT_MAGENTA] = rxvt ? ret[MAGENTA] : "105";
ret[LIGHT_CYAN] = rxvt ? ret[CYAN] : "106";
ret[WHITE] = rxvt ? ret[LIGHT_GRAY] : "107";
return ret;
}
static std::map<TextColor, std::string> ansiFgColors = initAnsiFgColors();
static std::map<TextColor, std::string> ansiBgColors = initAnsiBgColors();
#ifdef WIN32
static std::map<TextColor, int> initCrtFgColors() {
std::map<TextColor, int> ret;
ret[BLACK] = 0;
ret[RED] = FOREGROUND_RED;
ret[GREEN] = FOREGROUND_GREEN;
ret[YELLOW] = FOREGROUND_RED | FOREGROUND_GREEN;
ret[BLUE] = FOREGROUND_BLUE;
ret[MAGENTA] = FOREGROUND_BLUE | FOREGROUND_RED;
ret[CYAN] = FOREGROUND_GREEN | FOREGROUND_BLUE;
ret[LIGHT_GRAY]= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
ret[DARK_GRAY] = FOREGROUND_INTENSITY;
ret[LIGHT_RED] = FOREGROUND_RED | FOREGROUND_INTENSITY;
ret[LIGHT_GREEN] = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
ret[LIGHT_YELLOW] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
ret[LIGHT_BLUE] = FOREGROUND_BLUE | FOREGROUND_INTENSITY;
ret[LIGHT_MAGENTA] = FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY;
ret[LIGHT_CYAN] = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
ret[WHITE] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
return ret;
}
static std::map<TextColor, int> initCrtBgColors() {
std::map<TextColor, int> ret;
ret[BLACK] = 0;
ret[RED] = BACKGROUND_RED;
ret[GREEN] = BACKGROUND_GREEN;
ret[YELLOW] = BACKGROUND_RED | BACKGROUND_GREEN;
ret[BLUE] = BACKGROUND_BLUE;
ret[MAGENTA] = BACKGROUND_BLUE | BACKGROUND_RED;
ret[CYAN] = BACKGROUND_GREEN | BACKGROUND_BLUE;
ret[LIGHT_GRAY]= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
ret[DARK_GRAY] = BACKGROUND_INTENSITY;
ret[LIGHT_RED] = BACKGROUND_RED | BACKGROUND_INTENSITY;
ret[LIGHT_GREEN] = BACKGROUND_GREEN | BACKGROUND_INTENSITY;
ret[LIGHT_YELLOW] = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_INTENSITY;
ret[LIGHT_BLUE] = BACKGROUND_BLUE | BACKGROUND_INTENSITY;
ret[LIGHT_MAGENTA] = BACKGROUND_BLUE | BACKGROUND_RED | BACKGROUND_INTENSITY;
ret[LIGHT_CYAN] = BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY;
ret[WHITE] = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY;
return ret;
}
static WORD getCurrentBgColor() {
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(hStdout, &info);
return info.wAttributes & (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY);
}
static WORD getCurrentFgColor() {
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(hStdout, &info);
return info.wAttributes & (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
}
static std::map<TextColor, int> crtFgColors = initCrtFgColors();
static std::map<TextColor, int> crtBgColors = initCrtBgColors();
#endif
static void escapeAnsiCode(std::ostream &os, const std::string &ansiCode, const std::string &endEscape ="m") {
static const char *stdOutNoAnsiEscapes = getenv("STDOUT_NO_ANSI_ESCAPES");
static const char *stdErrNoAnsiEscapes = getenv("STDERR_NO_ANSI_ESCAPES");
#ifndef WIN32
if ((&os == &std::cout && !stdOutNoAnsiEscapes && isatty(fileno(stdout))) ||
(&os == &std::cerr && !stdErrNoAnsiEscapes && isatty(fileno(stderr))))
#else
if ((&os == &std::cout && !stdOutNoAnsiEscapes) ||
(&os == &std::cerr && !stdErrNoAnsiEscapes))
#endif
os << "\x1b[" << ansiCode << endEscape;
}
/*
This set of stream manipulation operators allow to produce
a colored console ouput. Except for windows console application (based on cmd.exe),
the colors are activated through ANSI escape sequences. The writing of these sequences
can be disabled by setting the STDOUT_N0_ANSI_ESCAPES and STDERR_N0_ANSI_ESCAPES
environment variables (for instance, when working with a terminal who does not understand
these sequences).
Below is an example on how to use it :
// Output a bold text colored in red on a white background
std::cout << setTextEffect(BOLD) << setTextColor(RED) << setTextBackgroundColor(WHITE) << "Hello World!" << std::endl;
// Reset the text colors to the default ones
std::cout << defaultTextColor;
// You can also use some predefined color schemes (only affects text color) : black, white, red, green,
// blue, yellow, white, cyan, magenta, darkGray, lightGray, lightRed, lightGreen, ...
std::cout << red << "Hello " << blue << World !!" << defaulTextColor << std::endl;
*/
inline std::ostream& defaultTextColor(std::ostream &s) {
if (&s == &std::cout || &s == &std::cerr) {
#ifndef WIN32
escapeAnsiCode(s, "0");
escapeAnsiCode(s, "39");
escapeAnsiCode(s, "49");
#else
// on windows, if the TERM environment variable is not defined
// it means that the shell used is cmd.exe, whose does not understand ANSI escape sequences
static const char *term = getenv("TERM");
if (term && std::string(term) != "cygwin") {
escapeAnsiCode(s, "0");
escapeAnsiCode(s, "39");
escapeAnsiCode(s, "49");
}
else {
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout, crtBgColors[BLACK] | crtFgColors[WHITE]);
}
#endif
}
return s;
}
inline std::ostream& operator<<(std::ostream &s, const TextFgColorSetup &tgfcs) {
if (&s == &std::cout || &s == &std::cerr) {
#ifndef WIN32
escapeAnsiCode(s, ansiFgColors[tgfcs.color]);
#else
// on windows, if the TERM environment variable is not defined
// it means that the shell used is cmd.exe, whose does not understand ANSI escape sequences
static const char *term = getenv("TERM");
if (term && std::string(term) != "cygwin") {
escapeAnsiCode(s, ansiFgColors[tgfcs.color]);
}
else {
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout, crtFgColors[tgfcs.color] | getCurrentBgColor());
}
#endif
}
return s;
}
inline std::ostream& operator<<(std::ostream &s, const TextBgColorSetup &tbgcs) {
if (&s == &std::cout || &s == &std::cerr) {
#ifndef WIN32
escapeAnsiCode(s, ansiBgColors[tbgcs.color]);
#else
// on windows, if the TERM environment variable is not defined
// it means that the shell used is cmd.exe, whose does not understand ANSI escape sequences
static const char *term = getenv("TERM");
if (term && std::string(term) != "cygwin") {
escapeAnsiCode(s, ansiBgColors[tbgcs.color]);
}
else {
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout, getCurrentFgColor() | crtBgColors[tbgcs.color]);
}
#endif
}
return s;
}
inline void setEffects(std::ostream &s, const int &effect) {
if (effect & NORMAL)
escapeAnsiCode(s, "0");
if (effect & BOLD)
escapeAnsiCode(s, "1");
if (effect & UNDERLINED)
escapeAnsiCode(s, "4");
if (effect & BLINK)
escapeAnsiCode(s, "5");
}
inline std::ostream& operator<<(std::ostream &s, const TextEffectSetup &tes) {
if (&s == &std::cout || &s == &std::cerr) {
#ifndef WIN32
setEffects(s, tes.effect);
#else
static const char *term = getenv("TERM");
if (term && std::string(term) != "cygwin") {
setEffects(s, tes.effect);
}
#endif
}
return s;
}
inline TextBgColorSetup setTextBackgroundColor(TextColor color) {
return TextBgColorSetup(color);
}
inline TextFgColorSetup setTextColor(TextColor color) {
return TextFgColorSetup(color);
}
inline TextEffectSetup setTextEffect(int effect) {
return TextEffectSetup(effect);
}
inline std::ostream& bold(std::ostream& s) {
return s << setTextEffect(BOLD);
}
inline std::ostream& underlined(std::ostream& s) {
return s << setTextEffect(UNDERLINED);
}
inline std::ostream& red(std::ostream& s) {
return s << setTextColor(RED);
}
inline std::ostream& green(std::ostream& s) {
return s << setTextColor(GREEN);
}
inline std::ostream& blue(std::ostream& s) {
return s << setTextColor(BLUE);
}
inline std::ostream& yellow(std::ostream& s) {
return s << setTextColor(YELLOW);
}
inline std::ostream& white(std::ostream& s) {
return s << setTextColor(WHITE);
}
inline std::ostream& black(std::ostream& s) {
return s << setTextColor(BLACK);
}
inline std::ostream& magenta(std::ostream& s) {
return s << setTextColor(MAGENTA);
}
inline std::ostream& cyan(std::ostream& s) {
return s << setTextColor(CYAN);
}
inline std::ostream& lightRed(std::ostream& s) {
return s << setTextColor(LIGHT_RED);
}
inline std::ostream& lightGreen(std::ostream& s) {
return s << setTextColor(LIGHT_GREEN);
}
inline std::ostream& lightBlue(std::ostream& s) {
return s << setTextColor(LIGHT_BLUE);
}
inline std::ostream& lightYellow(std::ostream& s) {
return s << setTextColor(LIGHT_YELLOW);
}
inline std::ostream& lightGray(std::ostream& s) {
return s << setTextColor(LIGHT_GRAY);
}
inline std::ostream& darkGray(std::ostream& s) {
return s << setTextColor(DARK_GRAY);
}
inline std::ostream& lightMagenta(std::ostream& s) {
return s << setTextColor(LIGHT_MAGENTA);
}
inline std::ostream& lightCyan(std::ostream& s) {
return s << setTextColor(LIGHT_CYAN);
}
// Fill the current line of the terminal with space characters from the
// current cursor position to the end of the line.
// The purpose is to have a constant background color on the line.
inline std::ostream& fillToEndOfLine(std::ostream& s) {
#ifndef WIN32
if (processInForeground() && ((&s == &std::cout && isatty(fileno(stdout))) || (&s == &std::cerr && isatty(fileno(stderr))))) {
#endif
std::pair<int, int> cursorPos = getConsoleCursorPosition();
std::pair<int, int> consoleSize = getConsoleSize();
if ((cursorPos.second + consoleSize.second) != 0) {
for (int i = cursorPos.second ; i <= consoleSize.second ; ++i) {
s << " ";
}
}
else {
s << std::endl;
}
#ifndef WIN32
}
#endif
if ((&s == &std::cout && !isatty(fileno(stdout))) || (&s == &std::cerr && !isatty(fileno(stderr)))) {
s << std::endl;
}
#ifndef WIN32
if (!processInForeground()) {
s << defaultTextColor << std::endl;
}
#endif
return s;
}
#endif
///@endcond
|