AnsiTerminal.h revision 296417
1254721Semaste//===---------------------AnsiTerminal.h ------------------------*- C++ -*-===// 2254721Semaste// 3254721Semaste// The LLVM Compiler Infrastructure 4254721Semaste// 5254721Semaste// This file is distributed under the University of Illinois Open Source 6254721Semaste// License. See LICENSE.TXT for details. 7254721Semaste// 8254721Semaste//===----------------------------------------------------------------------===// 9254721Semaste 10254721Semaste 11254721Semaste 12254721Semaste#define ANSI_FG_COLOR_BLACK 30 13254721Semaste#define ANSI_FG_COLOR_RED 31 14254721Semaste#define ANSI_FG_COLOR_GREEN 32 15254721Semaste#define ANSI_FG_COLOR_YELLOW 33 16254721Semaste#define ANSI_FG_COLOR_BLUE 34 17254721Semaste#define ANSI_FG_COLOR_PURPLE 35 18254721Semaste#define ANSI_FG_COLOR_CYAN 36 19254721Semaste#define ANSI_FG_COLOR_WHITE 37 20254721Semaste 21254721Semaste#define ANSI_BG_COLOR_BLACK 40 22254721Semaste#define ANSI_BG_COLOR_RED 41 23254721Semaste#define ANSI_BG_COLOR_GREEN 42 24254721Semaste#define ANSI_BG_COLOR_YELLOW 43 25254721Semaste#define ANSI_BG_COLOR_BLUE 44 26254721Semaste#define ANSI_BG_COLOR_PURPLE 45 27254721Semaste#define ANSI_BG_COLOR_CYAN 46 28254721Semaste#define ANSI_BG_COLOR_WHITE 47 29254721Semaste 30254721Semaste#define ANSI_SPECIAL_FRAMED 51 31254721Semaste#define ANSI_SPECIAL_ENCIRCLED 52 32254721Semaste 33254721Semaste#define ANSI_CTRL_NORMAL 0 34254721Semaste#define ANSI_CTRL_BOLD 1 35254721Semaste#define ANSI_CTRL_FAINT 2 36254721Semaste#define ANSI_CTRL_ITALIC 3 37254721Semaste#define ANSI_CTRL_UNDERLINE 4 38254721Semaste#define ANSI_CTRL_SLOW_BLINK 5 39254721Semaste#define ANSI_CTRL_FAST_BLINK 6 40254721Semaste#define ANSI_CTRL_IMAGE_NEGATIVE 7 41254721Semaste#define ANSI_CTRL_CONCEAL 8 42254721Semaste#define ANSI_CTRL_CROSSED_OUT 9 43254721Semaste 44296417Sdim#define ANSI_ESC_START "\033[" 45296417Sdim#define ANSI_ESC_END "m" 46254721Semaste 47296417Sdim#define ANSI_STR(s) #s 48296417Sdim#define ANSI_DEF_STR(s) ANSI_STR(s) 49296417Sdim 50296417Sdim#define ANSI_ESCAPE1(s) ANSI_ESC_START ANSI_DEF_STR(s) ANSI_ESC_END 51296417Sdim 52254721Semaste#define ANSI_1_CTRL(ctrl1) "\033["##ctrl1 ANSI_ESC_END 53254721Semaste#define ANSI_2_CTRL(ctrl1,ctrl2) "\033["##ctrl1";"##ctrl2 ANSI_ESC_END 54254721Semaste 55254721Semastenamespace lldb_utility { 56254721Semaste 57254721Semaste namespace ansi { 58254721Semaste 59254721Semaste inline std::string 60254721Semaste FormatAnsiTerminalCodes(const char *format, bool do_color = true) 61254721Semaste { 62254721Semaste // Convert "${ansi.XXX}" tokens to ansi values or clear them if do_color is false. 63254721Semaste static const struct 64254721Semaste { 65254721Semaste const char *name; 66254721Semaste const char *value; 67254721Semaste } g_color_tokens[] = 68254721Semaste { 69254721Semaste #define _TO_STR2(_val) #_val 70254721Semaste #define _TO_STR(_val) _TO_STR2(_val) 71254721Semaste { "fg.black}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_BLACK) ANSI_ESC_END }, 72254721Semaste { "fg.red}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_RED) ANSI_ESC_END }, 73254721Semaste { "fg.green}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_GREEN) ANSI_ESC_END }, 74254721Semaste { "fg.yellow}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_YELLOW) ANSI_ESC_END }, 75254721Semaste { "fg.blue}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_BLUE) ANSI_ESC_END }, 76254721Semaste { "fg.purple}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_PURPLE) ANSI_ESC_END }, 77254721Semaste { "fg.cyan}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_CYAN) ANSI_ESC_END }, 78254721Semaste { "fg.white}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_WHITE) ANSI_ESC_END }, 79254721Semaste { "bg.black}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_BLACK) ANSI_ESC_END }, 80254721Semaste { "bg.red}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_RED) ANSI_ESC_END }, 81254721Semaste { "bg.green}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_GREEN) ANSI_ESC_END }, 82254721Semaste { "bg.yellow}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_YELLOW) ANSI_ESC_END }, 83254721Semaste { "bg.blue}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_BLUE) ANSI_ESC_END }, 84254721Semaste { "bg.purple}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_PURPLE) ANSI_ESC_END }, 85254721Semaste { "bg.cyan}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_CYAN) ANSI_ESC_END }, 86254721Semaste { "bg.white}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_WHITE) ANSI_ESC_END }, 87254721Semaste { "normal}", ANSI_ESC_START _TO_STR(ANSI_CTRL_NORMAL) ANSI_ESC_END }, 88254721Semaste { "bold}", ANSI_ESC_START _TO_STR(ANSI_CTRL_BOLD) ANSI_ESC_END }, 89254721Semaste { "faint}", ANSI_ESC_START _TO_STR(ANSI_CTRL_FAINT) ANSI_ESC_END }, 90254721Semaste { "italic}", ANSI_ESC_START _TO_STR(ANSI_CTRL_ITALIC) ANSI_ESC_END }, 91254721Semaste { "underline}", ANSI_ESC_START _TO_STR(ANSI_CTRL_UNDERLINE) ANSI_ESC_END }, 92254721Semaste { "slow-blink}", ANSI_ESC_START _TO_STR(ANSI_CTRL_SLOW_BLINK) ANSI_ESC_END }, 93254721Semaste { "fast-blink}", ANSI_ESC_START _TO_STR(ANSI_CTRL_FAST_BLINK) ANSI_ESC_END }, 94254721Semaste { "negative}", ANSI_ESC_START _TO_STR(ANSI_CTRL_IMAGE_NEGATIVE) ANSI_ESC_END }, 95254721Semaste { "conceal}", ANSI_ESC_START _TO_STR(ANSI_CTRL_CONCEAL) ANSI_ESC_END }, 96254721Semaste { "crossed-out}", ANSI_ESC_START _TO_STR(ANSI_CTRL_CROSSED_OUT) ANSI_ESC_END }, 97254721Semaste #undef _TO_STR 98254721Semaste #undef _TO_STR2 99254721Semaste }; 100254721Semaste static const char tok_hdr[] = "${ansi."; 101254721Semaste 102254721Semaste std::string fmt; 103254721Semaste for (const char *p = format; *p; ++p) 104254721Semaste { 105254721Semaste const char *tok_start = strstr (p, tok_hdr); 106254721Semaste if (!tok_start) 107254721Semaste { 108254721Semaste fmt.append (p, strlen(p)); 109254721Semaste break; 110254721Semaste } 111254721Semaste 112254721Semaste fmt.append (p, tok_start - p); 113254721Semaste p = tok_start; 114254721Semaste 115254721Semaste const char *tok_str = tok_start + sizeof(tok_hdr) - 1; 116254721Semaste for (size_t i = 0; i < sizeof(g_color_tokens) / sizeof(g_color_tokens[0]); ++i) 117254721Semaste { 118254721Semaste if (!strncmp (tok_str, g_color_tokens[i].name, strlen(g_color_tokens[i].name))) 119254721Semaste { 120254721Semaste if (do_color) 121254721Semaste fmt.append (g_color_tokens[i].value); 122254721Semaste p = tok_str + strlen (g_color_tokens[i].name) - 1; 123254721Semaste break; 124254721Semaste } 125254721Semaste } 126254721Semaste } 127254721Semaste return fmt; 128254721Semaste } 129254721Semaste } 130254721Semaste} 131