thebookofcolors/thebookofcolors

491 lines
19 KiB
Bash
Executable File

#!/bin/sh
set -e
thebookofcolors() {
printf "\e[4;7;36m"
if test -z "$1"; then
printf "The Book of Colors"
else
printf "$1"
fi
printf "\e[0m"
}
newline() {
if test -z "$1"; then
printf "\n"
return
fi
for i in $(seq 1 $1); do
printf "\n"
done
}
title() {
if test -z "$1"; then
printf "No title provided." 1>&2
exit 1
fi
printf "\e[1;4;37m"
printf "$1"
printf "\e[0m\n"
}
subtitle() {
if test -z "$1"; then
printf "No subtitle provided." 1>&2
exit 1
fi
printf "\e[4;37m"
printf "$1"
printf "\e[0m"
if test -z "$2"; then printf "\n"; fi
}
tableheader() {
subtitle "$1" "noreturn"
}
italic() {
if test -z "$1"; then
printf "No title provided." 1>&2
exit 1
fi
printf "\e[3;37m"
printf "$1"
printf "\e[0m"
}
section_introduction() {
title "Introduction"
thebookofcolors
printf " is an in-terminal manual on ANSI escape codes, or \"color codes\", for use in terminal emulators.\n"
printf "Examples are given not only in written form, but \"practical\" form -- every escape code and option described in "
thebookofcolors
printf " is used in "
thebookofcolors "The Book"
printf " itself.\n"
}
section_disclaimers() {
title "Disclaimers"
thebookofcolors
printf " is open-source software and is provided as-is with NO WARRANTY under applicable law.\n"
printf "If any escape codes persist to your terminal session after viewing "
thebookofcolors "The Book"
printf ", use the \`reset\` command to restore your terminal.\n"
printf "If you believe you have found a bug, please open an issue on the repository, which can be found at <REPOSITORY_URL_GOES_HERE>.\n"
printf "If you have a fix for said bug, please submit a pull request instead.\n"
}
section_anatomy() {
title "Anatomy of an ANSI Escape Code"
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 "
thebookofcolors
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"
newline
printf "Overall, a control sequence is formatted like so:\n"
newline
italic "0x1B + [ + <zero or more numbers, separated by semicolons> + <a letter>"
newline 2
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"
newline
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"
}
section_regular_colors() {
title "Codes 30-37; Regular Foreground Colors"
printf "Originally, hardware video terminals only had support for eight standard colors.\n"
printf "The SGR control codes 30 through 37 are used to set the foreground color to each of these colors.\n"
printf "These colors and their respective escape codes are shown in the following table:\n"
newline
subtitle "A Listing of Basic Colors"
printf "\e[30;47m\\\e[30m Black\e[0m (shown on light grey background to aid visibility)\n"
printf "\e[31m\\\e[31m Red\e[0m\n"
printf "\e[32m\\\e[32m Green\e[0m\n"
printf "\e[33m\\\e[33m Brown\e[0m\n"
printf "\e[34m\\\e[34m Blue\e[0m\n"
printf "\e[35m\\\e[35m Purple\e[0m\n"
printf "\e[36m\\\e[36m Cyan\e[0m\n"
printf "\e[37m\\\e[37;40m Light grey (shown on black background to aid visibility)\e[0m\n"
}
section_notes_on_names() {
title "Notes on the Names of Colors"
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"
}
section_reset() {
title "Code 0; Reset"
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"
}
section_bold_colors() {
title "Code 1; Bold Color"
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"
newline
subtitle "A Listing of Basic Colors with Bold Text"
printf "\e[1;30;47m\\\e[1;30m Dark grey\e[0m (shown on light grey)\n"
printf "\e[1;31m\\\e[1;31m Light red\e[0m\n"
printf "\e[1;32m\\\e[1;32m Light green\e[0m\n"
printf "\e[1;33m\\\e[1;33m Yellow\e[0m\n"
printf "\e[1;34m\\\e[1;34m Light blue\e[0m\n"
printf "\e[1;35m\\\e[1;35m Light purple\e[0m\n"
printf "\e[1;36m\\\e[1;36m Light cyan\e[0m\n"
printf "\e[1;37;40m\\\e[1;37m White (shown on black)\e[0m\n"
}
section_background_colors() {
title "Codes 40-47; Background Colors"
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"
newline
subtitle "A Table of Background Colors"
tableheader "Regular"
printf " "
tableheader "Bold"
newline
printf "\e[37;40m\\\e[40m Black\e[0m \e[1;37;40m\\\e[1;40m Dark grey\e[0m\n"
printf "\e[41m\\\e[41m Red\e[0m \e[1;41m\\\e[1;41m Light red\e[0m\n"
printf "\e[42m\\\e[42m Green\e[0m \e[1;42m\\\e[1;42m Light green\e[0m\n"
printf "\e[43m\\\e[43m Brown\e[0m \e[1;43m\\\e[1;43m Yellow\e[0m\n"
printf "\e[44m\\\e[44m Blue\e[0m \e[1;44m\\\e[1;44m Light blue\e[0m\n"
printf "\e[45m\\\e[45m Purple\e[0m \e[1;45m\\\e[1;45m Light purple\e[0m\n"
printf "\e[46m\\\e[46m Cyan\e[0m \e[1;46m\\\e[1;46m Light cyan\e[0m\n"
printf "\e[30;47m\\\e[47m Light grey\e[0m \e[1;30;47m\\\e[1;47m White\e[0m\n"
}
section_8bit_colors() {
title "Codes 38;5 and 48;5; 8-bit (256) Colors"
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 "
italic "\\\e[38;5;n"
printf " for foreground colors, or "
italic "\\\e[48;5;n"
printf " for background colors.\n"
printf "The value chosen for "
italic "n"
printf " determines which color is selected. See the information in the following two subsections to see the range of colors available.\n"
newline
subtitle "Ranges of Available Colors"
printf " 0- 7: Standard colors (the same as \\\e[30m through \\\e[37m)\n"
printf " 8- 15: Bright colors (the same as \\\e[90m through \\\e[97m)\n"
printf " 16-231: A 6x6x6 cube of RGB colors\n"
printf "232-255: Grayscale from light to dark in 24 increments\n"
newline
subtitle "A Table of Available Colors"
for i in $(seq 0 255); do
if test "$(echo $i % 8 | bc)" = "0" -a "$i" != "0"; then
printf "\n"
fi
printf "\e[38;5;${i}m\\\e[38;5;%-3d\e[0m " "$i"
done
newline
}
section_rgb_colors() {
title "Codes 38;2 and 48;2; RGB Colors"
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"
subtitle "A Sample of RGB Colors"
printf "\e[38;2;7;21;205m\\\e[38;2;7;21;205m\e[0m\n"
printf "\e[38;2;181;54;218m\\\e[38;2;181;54;218m\e[0m\n"
printf "\e[38;2;224;7;7m\\\e[38;2;224;7;7m\e[0m\n"
printf "\e[38;2;74;201;37m\\\e[38;2;74;201;37m\e[0m\n"
printf "\e[38;2;161;0;0m\\\e[38;2;161;0;0m\e[0m\n"
printf "\e[38;2;161;80;0m\\\e[38;2;161;80;0m\e[0m\n"
printf "\e[38;2;161;161;0m\\\e[38;2;161;161;0m\e[0m\n"
printf "\e[38;2;98;98;98m\\\e[38;2;98;98;98m\e[0m\n"
printf "\e[38;2;65;102;0m\\\e[38;2;65;102;0m\e[0m\n"
printf "\e[38;2;0;129;65m\\\e[38;2;0;129;65m\e[0m\n"
printf "\e[38;2;0;130;130m\\\e[38;2;0;130;130m\e[0m\n"
printf "\e[38;2;0;86;130m\\\e[38;2;0;86;130m\e[0m\n"
printf "\e[38;2;0;0;86m\\\e[38;2;0;0;86m\e[0m\n"
printf "\e[38;2;43;0;57m\\\e[38;2;43;0;57m\e[0m\n"
printf "\e[38;2;106;0;106m\\\e[38;2;106;0;106m\e[0m\n"
printf "\e[38;2;119;0;60m\\\e[38;2;119;0;60m\e[0m\n"
printf "\e[38;2;0;213;242m\\\e[38;2;0;213;242m\e[0m\n"
printf "\e[38;2;255;111;243m\\\e[38;2;255;111;243m\e[0m\n"
printf "\e[38;2;242;164;0m\\\e[38;2;242;164;0m\e[0m\n"
printf "\e[38;2;32;148;1m\\\e[38;2;32;148;1m\e[0m\n"
printf "\e[38;2;146;146;146m\\\e[38;2;146;146;146m\e[0m\n"
printf "\e[38;2;50;50;50m\\\e[38;2;50;50;50m\e[0m\n"
}
section_bright_colors() {
title "Codes 90-97 and 100-107; Bright Colors"
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 "\e[91m\\\e[91m Light red\e[0m \e[101m\\\e[101m Light red\e[0m\n"
printf "\e[92m\\\e[92m Light green\e[0m \e[102m\\\e[102m Light green\e[0m\n"
printf "\e[93m\\\e[93m Yellow\e[0m \e[103m\\\e[103m Yellow\e[0m\n"
printf "\e[94m\\\e[94m Light blue\e[0m \e[104m\\\e[104m Light blue\e[0m\n"
printf "\e[95m\\\e[95m Light purple\e[0m \e[105m\\\e[105m Light purple\e[0m\n"
printf "\e[96m\\\e[96m Light cyan\e[0m \e[106m\\\e[106m Light cyan\e[0m\n"
printf "\e[97m\\\e[97m White\e[0m \e[107m\\\e[107m White\e[0m\n"
}
section_italics() {
title "Code 3; Italics"
printf "Code 3 enables "
italic "italic"
printf " text.\n"
}
section_underline() {
title "Code 4; Underscore"
printf "Code 4 adds an underscore, or underline, to text.\n"
printf "Note that the underline is always shown in the foreground color; see the table below for details.\n"
newline
subtitle "A Table of Underlined Text"
tableheader "Foreground"
printf " "
tableheader "Background"
newline
printf "\e[4;30m\\\e[4;30m Black\e[0m \e[4;40m\\\e[4;40m Black\e[0m\n"
printf "\e[4;31m\\\e[4;31m Red\e[0m \e[4;41m\\\e[4;41m Red\e[0m\n"
printf "\e[4;32m\\\e[4;32m Green\e[0m \e[4;42m\\\e[4;42m Green\e[0m\n"
printf "\e[4;33m\\\e[4;33m Brown\e[0m \e[4;43m\\\e[4;43m Brown\e[0m\n"
printf "\e[4;34m\\\e[4;34m Blue\e[0m \e[4;44m\\\e[4;44m Blue\e[0m\n"
printf "\e[4;35m\\\e[4;35m Purple\e[0m \e[4;45m\\\e[4;45m Purple\e[0m\n"
printf "\e[4;36m\\\e[4;36m Cyan\e[0m \e[4;46m\\\e[4;46m Cyan\e[0m\n"
printf "\e[4;37m\\\e[4;37m Light grey\e[0m \e[4;47m\\\e[4;47m Light grey\e[0m\n"
}
section_dreaded_blinking_text_of_doom() {
title "Codes 5 and 6; Blinking"
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"
newline
subtitle "A Table of Blinking Texts"
tableheader "Foreground"
printf " "
tableheader "Background"
newline
printf "\e[5;30m\\\e[5;30m Black\e[0m \e[5;40m\\\e[5;40m Black\e[0m\n"
printf "\e[5;31m\\\e[5;31m Red\e[0m \e[5;41m\\\e[5;41m Red\e[0m\n"
printf "\e[5;32m\\\e[5;32m Green\e[0m \e[5;42m\\\e[5;42m Green\e[0m\n"
printf "\e[5;33m\\\e[5;33m Brown\e[0m \e[5;43m\\\e[5;43m Brown\e[0m\n"
printf "\e[5;34m\\\e[5;34m Blue\e[0m \e[5;44m\\\e[5;44m Blue\e[0m\n"
printf "\e[5;35m\\\e[5;35m Purple\e[0m \e[5;45m\\\e[5;45m Purple\e[0m\n"
printf "\e[5;36m\\\e[5;36m Cyan\e[0m \e[5;46m\\\e[5;46m Cyan\e[0m\n"
printf "\e[5;37m\\\e[5;37m Light grey\e[0m \e[5;47m\\\e[5;47m Light grey\e[0m\n"
newline
printf "Code 6 enables \"rapid blink\", which is not widely supported.\n"
printf "Here is a sample using code 6; if it blinks at the same rate as the table above, then your terminal doesn't support rapid blink.\n"
printf "\e[6;31m\\\e[6;31m Red\e[0m \e[6;41m\\\e[6;41m Red\e[0m\n"
}
section_inverse() {
title "Code 7; Inverse"
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"
newline
subtitle "A Table of Inverted Colors"
tableheader "Foreground"
printf " "
tableheader "Background"
newline
printf "\e[7;30m\\\e[7;30m Black\e[0m \e[7;40m\\\e[7;40m Black\e[0m\n"
printf "\e[7;31m\\\e[7;31m Red\e[0m \e[7;41m\\\e[7;41m Red\e[0m\n"
printf "\e[7;32m\\\e[7;32m Green\e[0m \e[7;42m\\\e[7;42m Green\e[0m\n"
printf "\e[7;33m\\\e[7;33m Brown\e[0m \e[7;43m\\\e[7;43m Brown\e[0m\n"
printf "\e[7;34m\\\e[7;34m Blue\e[0m \e[7;44m\\\e[7;44m Blue\e[0m\n"
printf "\e[7;35m\\\e[7;35m Purple\e[0m \e[7;45m\\\e[7;45m Purple\e[0m\n"
printf "\e[7;36m\\\e[7;36m Cyan\e[0m \e[7;46m\\\e[7;46m Cyan\e[0m\n"
printf "\e[7;37m\\\e[7;37m Light grey\e[0m \e[7;47m\\\e[7;47m Light grey\e[0m\n"
}
section_using_escape_sequences_in_shell_prompt() {
title "Using Escape Sequences in Shell Prompts"
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"
printf " - WRONG: \`PS1='\\\\e[36m\u@\h \\$\\\\e[0m'\`\n"
printf " - RIGHT: \`PS1='\[\\\\e[36m\]\u@\h \\$\[\\\\e[0m\]'\`\n"
}
section_table_of_all_sgr_commands() {
title "Table of All SGR Commands"
printf "Below is a table of all possible SGR parameters, from 0 to 107, so you can see which ones have support in your terminal.\n"
newline
for i in $(seq 0 107); do
if test "$(echo $i % 10 | bc)" = "0" -a "$i" != "0"; then
printf "\n"
fi
printf "\e[${i}m\\\e[%dm\e[0m" "$i"
if test "$i" -lt "10"; then printf " "; fi
if test "$i" -lt "100"; then printf " "; fi
printf " "
done
newline
}
section_credits() {
title "Credits"
printf 'TODO\n'
}
main() {
thebookofcolors
newline 2
section_introduction
newline
section_disclaimers
newline
section_anatomy
newline
section_regular_colors
newline
section_notes_on_names
newline
section_reset
newline
section_bold_colors
newline
section_background_colors
newline
section_8bit_colors
newline
section_rgb_colors
newline
section_bright_colors
newline
section_italics
newline
section_underline
newline
section_dreaded_blinking_text_of_doom
newline
section_inverse
newline
section_using_escape_sequences_in_shell_prompt
newline
section_table_of_all_sgr_commands
newline
section_credits
newline
#printf '\e[4;37m\e[1;37m2; (Test)\e[0m\n'
#printf '\e[2;34m[2;34m Light blue\e[0m \e[2;44m[2;44m Light blue\e[0m\n'
#printf '\n'
#printf '\e[4;37m\e[1;37m3; (Test)\e[0m\n'
#printf '\e[3;34m[3;34m Light blue\e[0m \e[3;44m[3;44m Light blue\e[0m\n'
#printf '\n'
}
name="$(basename $0)"
if test "$name" = "thebookofcolors"; then
main | less -R
fi