printf "Escape codes begin with \`\\e\`, \`\\033\`, or \`\\x1b\`. These are all three different escape sequences which insert the ASCII character 27, or ESC. (This is why they're called ANSI \"escape\" codes, because they begin with a literal escape character). The syntax \`\\e\` is used throughout "
printf ". The choice is arbitrary, but consistent throughout.\n"
printf "The next character is a \`[\`. This combination of characters, an escape followed by an open bracket, is called the \"CSI\", or \"Control Sequence Introducer\", which indicates the start of a control code.\n"
printf "The CSI sequence we've gone over already. The easiest way to think about this is that the letter is the name of a function, and the semicolon-delimited numbers are the arguments to said function.\n"
printf "By this model of thinking about escape sequences like function calls means you would read \"\\\e[0;1;34m\" as m(0, 1, 34), and \"\\\e[A\" as A().\n"
printf "The \"m\" function is the one we're interested in. This is the \"SGR\", or \"Select Graphics Rendition\", code, and it tells the terminal to activate color and other text effects, which is what we're interested in.\n"
printf "It's possible that the actual colors you see may not match the names of the colors printed in the tables found throughout "
thebookofcolors
printf ".\n"
printf "This is because the colors displayed for each color code are dependent on both terminal implementation and individual user configuration, whereas the different color values have hardcoded names.\n"
printf "Depending on your terminal and individual configurations, what you see may vary mildly or wildly from what "
thebookofcolors "The Book"
printf " lists as the name of each color.\n"
thebookofcolors
printf " uses the canonical color names throughout, since there's no way to know what any individual reader will see.\n"
newline
printf "The colors chosen to be part of the standard palette may seem to be arbitrary (and arbitrarily ordered), but there is a logic to it -- after \e[30;47mblack\e[0m, \e[31mred\e[0m is first for highlighting error conditions, then \e[32mgreen\e[0m for successes, \e[33mbrown\e[0m for warnings, \e[34mblue\e[0m for information, and \e[35mpurple\e[0m and \e[36mcyan\e[0m for more obscure conditions.\n"
printf "The SGR code \"0\" means \"reset\". This will restore all previously set colors and styles to the terminal's default values.\n"
printf "This is used often to end a section of styling, because codes continue indefinitely after they've been set.\n"
printf "For example, if you use \e[32m\\\e[32m to set the text to green, then that style will persist until the color is either changed again with another foreground color code, or reset with \e[0m\\\e[0m.\n"
printf "The SGR attribute \`1\` enables \"boldness\".\n"
printf "The result of using this attribute is highly implementation-dependent -- depending on the terminal used, this can either change the color to a \"bright\" version of the standard color, change the font face to bold, or both.\n"
printf "Here is a table of the colors with the boldness attribute enabled, along with their alternate names:\n"
printf "Codes 40 through 47 are used to set the background color, as opposed to 30-37 being used to set the foreground (i.e. text) color.\n"
printf "40 through 47 select from the same eight-color palette as codes 30 through 37. Whether the \"boldness\" attribute will set the background color to the alternate \"bright\" color is implementation-dependent; both are shown here so you can see whether your terminal supports it.\n"
printf "When 8-bit color support started to become more common on graphics cards, some terminals added the ability to select from an extended 8-bit color palette, giving a total of 256 colors.\n"
printf "The syntax for selecting one of these colors is "
printf "Again, as graphics hardware improved, some terminals added support for 24-bit color, or \"true color\".\n"
printf "These are the same RGB color codes used commonly in web development, where they're commonly shown in hexadecimal format.\n"
printf "The syntax for selecting a 24-bit color using ANSI escape codes is "
italic "\\\e[38;2;r;g;bm"
printf " for foreground colors, or "
italic "\\\e[48;2;r;g;bm"
printf " for background colors.\n"
printf "The "
italic "r"
printf ", "
italic "g"
printf ", and "
italic "b"
printf " values should be a number from 0 to 255, representing the intensity of that color channel."
printf "\\\e[38;2;0;0;0m is black, while \\\e[38;2;255;255;255 is white.\n"
newline
printf "Obviously, displaying a table of all 16 million available colors is impractical, so a small sampling giving a good representation of the variety of colors is given below.\n"
printf "Codes 90 through 97 and 100 through 107 are used to access the alternate \"bright\" color palette.\n"
printf "This allows you to access these colors regardless of whether the \"boldness\" attribute causes these colors to be displayed in your particular terminal.\n"
printf "Codes 90 through 97 set the foreground color, whereas codes 100 through 107 set the background color.\n"
newline
subtitle "A Table of Bright Colors"
tableheader "Foreground"
printf " "
tableheader "Background"
newline
printf "\e[90m\\\e[90m Dark grey\e[0m \e[100m\\\e[100m Dark grey\e[0m\n"
printf "Also known as the dreaded blinking text of doom.\n"
printf "Causes text to blink. Some terminal emulators refuse to honor this attribute as a matter of good taste, although there are some semi-legitimate uses for it -- the \`ls\` program, for example, can be configured to cause listings of broken symlinks to blink.\n"
printf "Note that even if the blinking attribute is set on a background color, it's always the text that blinks, not the background.\n"
printf "The inverse attribute causes the foreground and background colors to be flipped.\n"
printf "Note that if you use \\\e[7m to flip the colors, you can't then use \\\e[7m to flip them back again. If you want to restore the original colors, you need to set them explicitly again.\n"
printf "The lack of this capability makes this attribute somewhat fiddly and superfluous; "
thebookofcolors
printf "' official recommendation is to always set foreground and background colors explicitly.\n"
printf "When using these sequences in a Bash prompt (e.g. $PS1), it's possible for issues to arise because of Bash \"miscounting\" the number of characters in the prompt string.\n"
printf "This is often the source of difficult-to-debug (or describe) issues with the terminal, such as characters being moved around or overwritten seemingly at random.\n"
newline
printf "In order to prevent these issues, all ANSI escape codes need to be surrounded with escaped brackets, i.e. \`\\[\` and \`\\]\`.\n"
printf "For example, if you wanted an entirely cyan-colored prompt string:\n"