1*38fd1498Szrj /* Terminal color manipulation macros.
2*38fd1498Szrj    Copyright (C) 2005-2018 Free Software Foundation, Inc.
3*38fd1498Szrj 
4*38fd1498Szrj This file is part of GCC.
5*38fd1498Szrj 
6*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
7*38fd1498Szrj the terms of the GNU General Public License as published by the Free
8*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
9*38fd1498Szrj version.
10*38fd1498Szrj 
11*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14*38fd1498Szrj for more details.
15*38fd1498Szrj 
16*38fd1498Szrj You should have received a copy of the GNU General Public License
17*38fd1498Szrj along with GCC; see the file COPYING3.  If not see
18*38fd1498Szrj <http://www.gnu.org/licenses/>.  */
19*38fd1498Szrj 
20*38fd1498Szrj #ifndef GCC_COLOR_MACROS_H
21*38fd1498Szrj #define GCC_COLOR_MACROS_H
22*38fd1498Szrj 
23*38fd1498Szrj /* Select Graphic Rendition (SGR, "\33[...m") strings.  */
24*38fd1498Szrj /* Also Erase in Line (EL) to Right ("\33[K") by default.  */
25*38fd1498Szrj /*    Why have EL to Right after SGR?
26*38fd1498Szrj 	 -- The behavior of line-wrapping when at the bottom of the
27*38fd1498Szrj 	    terminal screen and at the end of the current line is often
28*38fd1498Szrj 	    such that a new line is introduced, entirely cleared with
29*38fd1498Szrj 	    the current background color which may be different from the
30*38fd1498Szrj 	    default one (see the boolean back_color_erase terminfo(5)
31*38fd1498Szrj 	    capability), thus scrolling the display by one line.
32*38fd1498Szrj 	    The end of this new line will stay in this background color
33*38fd1498Szrj 	    even after reverting to the default background color with
34*38fd1498Szrj 	    "\33[m', unless it is explicitly cleared again with "\33[K"
35*38fd1498Szrj 	    (which is the behavior the user would instinctively expect
36*38fd1498Szrj 	    from the whole thing).  There may be some unavoidable
37*38fd1498Szrj 	    background-color flicker at the end of this new line because
38*38fd1498Szrj 	    of this (when timing with the monitor's redraw is just right).
39*38fd1498Szrj 	 -- The behavior of HT (tab, "\t") is usually the same as that of
40*38fd1498Szrj 	    Cursor Forward Tabulation (CHT) with a default parameter
41*38fd1498Szrj 	    of 1 ("\33[I"), i.e., it performs pure movement to the next
42*38fd1498Szrj 	    tab stop, without any clearing of either content or screen
43*38fd1498Szrj 	    attributes (including background color); try
44*38fd1498Szrj 	       printf 'asdfqwerzxcv\rASDF\tZXCV\n'
45*38fd1498Szrj 	    in a bash(1) shell to demonstrate this.  This is not what the
46*38fd1498Szrj 	    user would instinctively expect of HT (but is ok for CHT).
47*38fd1498Szrj 	    The instinctive behavior would include clearing the terminal
48*38fd1498Szrj 	    cells that are skipped over by HT with blank cells in the
49*38fd1498Szrj 	    current screen attributes, including background color;
50*38fd1498Szrj 	    the boolean dest_tabs_magic_smso terminfo(5) capability
51*38fd1498Szrj 	    indicates this saner behavior for HT, but only some rare
52*38fd1498Szrj 	    terminals have it (although it also indicates a special
53*38fd1498Szrj 	    glitch with standout mode in the Teleray terminal for which
54*38fd1498Szrj 	    it was initially introduced).  The remedy is to add "\33K"
55*38fd1498Szrj 	    after each SGR sequence, be it START (to fix the behavior
56*38fd1498Szrj 	    of any HT after that before another SGR) or END (to fix the
57*38fd1498Szrj 	    behavior of an HT in default background color that would
58*38fd1498Szrj 	    follow a line-wrapping at the bottom of the screen in another
59*38fd1498Szrj 	    background color, and to complement doing it after START).
60*38fd1498Szrj 	    Piping GCC's output through a pager such as less(1) avoids
61*38fd1498Szrj 	    any HT problems since the pager performs tab expansion.
62*38fd1498Szrj 
63*38fd1498Szrj       Generic disadvantages of this remedy are:
64*38fd1498Szrj 	 -- Some very rare terminals might support SGR but not EL (nobody
65*38fd1498Szrj 	    will use "gcc -fdiagnostics-color" on a terminal that does not
66*38fd1498Szrj 	    support SGR in the first place).
67*38fd1498Szrj 	 -- Having these extra control sequences might somewhat complicate
68*38fd1498Szrj 	    the task of any program trying to parse "gcc -fdiagnostics-color"
69*38fd1498Szrj 	    output in order to extract structuring information from it.
70*38fd1498Szrj       A specific disadvantage to doing it after SGR START is:
71*38fd1498Szrj 	 -- Even more possible background color flicker (when timing
72*38fd1498Szrj 	    with the monitor's redraw is just right), even when not at the
73*38fd1498Szrj 	    bottom of the screen.
74*38fd1498Szrj       There are no additional disadvantages specific to doing it after
75*38fd1498Szrj       SGR END.
76*38fd1498Szrj 
77*38fd1498Szrj       It would be impractical for GCC to become a full-fledged
78*38fd1498Szrj       terminal program linked against ncurses or the like, so it will
79*38fd1498Szrj       not detect terminfo(5) capabilities.  */
80*38fd1498Szrj 
81*38fd1498Szrj #define COLOR_SEPARATOR		";"
82*38fd1498Szrj #define COLOR_NONE		"00"
83*38fd1498Szrj #define COLOR_BOLD		"01"
84*38fd1498Szrj #define COLOR_UNDERSCORE	"04"
85*38fd1498Szrj #define COLOR_BLINK		"05"
86*38fd1498Szrj #define COLOR_REVERSE		"07"
87*38fd1498Szrj #define COLOR_FG_BLACK		"30"
88*38fd1498Szrj #define COLOR_FG_RED		"31"
89*38fd1498Szrj #define COLOR_FG_GREEN		"32"
90*38fd1498Szrj #define COLOR_FG_YELLOW		"33"
91*38fd1498Szrj #define COLOR_FG_BLUE		"34"
92*38fd1498Szrj #define COLOR_FG_MAGENTA	"35"
93*38fd1498Szrj #define COLOR_FG_CYAN		"36"
94*38fd1498Szrj #define COLOR_FG_WHITE		"37"
95*38fd1498Szrj #define COLOR_BG_BLACK		"40"
96*38fd1498Szrj #define COLOR_BG_RED		"41"
97*38fd1498Szrj #define COLOR_BG_GREEN		"42"
98*38fd1498Szrj #define COLOR_BG_YELLOW		"43"
99*38fd1498Szrj #define COLOR_BG_BLUE		"44"
100*38fd1498Szrj #define COLOR_BG_MAGENTA	"45"
101*38fd1498Szrj #define COLOR_BG_CYAN		"46"
102*38fd1498Szrj #define COLOR_BG_WHITE		"47"
103*38fd1498Szrj #define SGR_START		"\33["
104*38fd1498Szrj #define SGR_END			"m\33[K"
105*38fd1498Szrj #define SGR_SEQ(str)		SGR_START str SGR_END
106*38fd1498Szrj #define SGR_RESET		SGR_SEQ("")
107*38fd1498Szrj 
108*38fd1498Szrj #endif  /* GCC_COLOR_MACROS_H */
109