thebookofcolors/thebookofcolors

340 lines
12 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"
}
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
printf "\e[3m0x1B + [ + <zero or more numbers, separated by semicolons> + <a letter>\e[0m\n"
newline
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
printf "\e[30m\\e[30;47mBlack\e[0m (shown on light grey background to aid visibility)\n"
printf "\e[31m\\e[31mRed\e[0m\n"
printf "\e[32m\\e[32mGreen\e[0m\n"
printf "\e[33m\\e[33mBrown\e[0m\n"
printf "\e[34m\\e[34mBlue\e[0m\n"
printf "\e[35m\\e[35mPurple\e[0m\n"
printf "\e[36m\\e[36mCyan\e[0m\n"
printf "\e[37m\\e[37;40mLight 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 'TODO\n'
}
section_background_colors() {
title "Codes 40-47; Regular Background Colors"
printf 'TODO\n'
}
section_256_colors() {
title "Codes 38;5 and 48;5; 256 Colors"
printf 'TODO\n'
}
section_rgb_colors() {
title "Codes 38;2 and 48;2; RGB Colors"
printf 'TODO\n'
}
section_bright_colors() {
title "Codes 90-97 and 100-107; Foreground and Background Bright Colors"
printf 'TODO\n'
}
section_italics() {
title "Code 3; Italics"
printf 'TODO\n'
}
section_underline() {
title "Code 4; Underline"
printf 'TODO\n'
}
section_dreaded_blinking_text_of_doom() {
title "Codes 5 and 6; Blinking"
printf 'TODO\n'
}
section_inverse() {
title "Code 7; Inverse"
printf 'TODO\n'
}
section_using_escape_sequences_in_shell_prompt() {
title "Using Escape Sequences in Shell Prompts"
printf 'TODO\n'
}
section_table_of_all_sgr_commands() {
title "Table of All SGR Commands"
printf 'TODO\n'
}
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_256_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 'When using these sequences in a Bash prompt (e.g. $PS1), be sure to surround the whole escape sequence with escaped brackets to prevent issues with Bash miscounting the number of characters in a line. For example: `export PS1=''\[\\e[36m\]\u@\h \$\[\\e[0m\]''` would create a cyan-colored prompt string.\n'
#printf '\n'
#
#printf '\e[4;37m\e[1;37mRegular Colors\e[0m\n'
#printf '\e[4;37mForeground\e[0m \e[4;37mBackground\e[0m\n'
#printf '\e[30m[30m Black\e[0m \e[40m[40m Black\e[0m\n'
#printf '\e[31m[31m Red\e[0m \e[41m[41m Red\e[0m\n'
#printf '\e[32m[32m Green\e[0m \e[42m[42m Green\e[0m\n'
#printf '\e[33m[33m Brown\e[0m \e[43m[43m Brown\e[0m\n'
#printf '\e[34m[34m Blue\e[0m \e[44m[44m Blue\e[0m\n'
#printf '\e[35m[35m Purple\e[0m \e[45m[45m Purple\e[0m\n'
#printf '\e[36m[36m Cyan\e[0m \e[46m[46m Cyan\e[0m\n'
#printf '\e[37m[37m Light grey\e[0m \e[47m[47m Light grey\e[0m\n'
#printf '\n'
#printf '\e[4;37m\e[1;37m1; (Boldness)\e[0m\n'
#printf 'Note that some terminals use the "bold" attribute to display an alternate version of the color, e.g. a lighter or darker shade.\n'
#printf 'The canonical names for these alternate colors are what is displayed below.\n'
#printf 'Other terminals actually support displaying text with this attribute in a bold typeface.\n'
#printf '\n'
#printf '\e[4;37mForeground\e[0m \e[4;37mBackground\e[0m\n'
#printf '\e[1;30m[1;30m Dark grey\e[0m \e[1;40m[1;40m Dark grey\e[0m\n'
#printf '\e[1;31m[1;31m Light red\e[0m \e[1;41m[1;41m Light red\e[0m\n'
#printf '\e[1;32m[1;32m Light green\e[0m \e[1;42m[1;42m Light green\e[0m\n'
#printf '\e[1;33m[1;33m Yellow\e[0m \e[1;43m[1;43m Yellow\e[0m\n'
#printf '\e[1;34m[1;34m Light blue\e[0m \e[1;44m[1;44m Light blue\e[0m\n'
#printf '\e[1;35m[1;35m Light purple\e[0m \e[1;45m[1;45m Light purple\e[0m\n'
#printf '\e[1;36m[1;36m Light cyan\e[0m \e[1;46m[1;46m Light cyan\e[0m\n'
#printf '\e[1;37m[1;37m White\e[0m \e[1;47m[1;47m White\e[0m\n'
#printf '\n'
#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'
#printf '\e[4;37m\e[1;37m4; (Underscore)\e[0m\n'
#printf 'This attribute displays text with an underline.\n'
#printf 'If the underline is always displayed in the foreground color. See the table below for more detail.\n'
#printf '\n'
#printf '\e[4;37mForeground\e[0m \e[4;37mBackground\e[0m\n'
#printf '\e[4;30m[4;30m Black\e[0m \e[4;40m[4;40m Black\e[0m\n'
#printf '\e[4;31m[4;31m Red\e[0m \e[4;41m[4;41m Red\e[0m\n'
#printf '\e[4;32m[4;32m Green\e[0m \e[4;42m[4;42m Green\e[0m\n'
#printf '\e[4;33m[4;33m Brown\e[0m \e[4;43m[4;43m Brown\e[0m\n'
#printf '\e[4;34m[4;34m Blue\e[0m \e[4;44m[4;44m Blue\e[0m\n'
#printf '\e[4;35m[4;35m Purple\e[0m \e[4;45m[4;45m Purple\e[0m\n'
#printf '\e[4;36m[4;36m Cyan\e[0m \e[4;46m[4;46m Cyan\e[0m\n'
#printf '\e[4;37m[4;37m Light grey\e[0m \e[4;47m[4;47m Light grey\e[0m\n'
#printf '\n'
#printf '\e[4;37m\e[1;37m5; (Blinkentext)\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 '\n'
#printf '\e[4;37mForeground\e[0m \e[4;37mBackground\e[0m\n'
#printf '\e[5;30m[5;30m Black\e[0m \e[5;40m[5;40m Black\e[0m\n'
#printf '\e[5;31m[5;31m Red\e[0m \e[5;41m[5;41m Red\e[0m\n'
#printf '\e[5;32m[5;32m Green\e[0m \e[5;42m[5;42m Green\e[0m\n'
#printf '\e[5;33m[5;33m Brown\e[0m \e[5;43m[5;43m Brown\e[0m\n'
#printf '\e[5;34m[5;34m Blue\e[0m \e[5;44m[5;44m Blue\e[0m\n'
#printf '\e[5;35m[5;35m Purple\e[0m \e[5;45m[5;45m Purple\e[0m\n'
#printf '\e[5;36m[5;36m Cyan\e[0m \e[5;46m[5;46m Cyan\e[0m\n'
#printf '\e[5;37m[5;37m Light grey\e[0m \e[5;47m[5;47m Light grey\e[0m\n'
#printf '\n'
#printf '\e[4;37m\e[1;37m7; (Inverse)\e[0m\n'
#printf 'Causes the foreground and background text to be flipped.\n'
#printf 'This is mostly a convenient shorthand for avoiding setting the foreground and background text simultaneously when you want one to be the default of the other.\n'
#printf '\n'
#printf '\e[4;37mForeground\e[0m \e[4;37mBackground\e[0m\n'
#printf '\e[7;30m[7;30m Black\e[0m \e[7;40m[7;40m Black\e[0m\n'
#printf '\e[7;31m[7;31m Red\e[0m \e[7;41m[7;41m Red\e[0m\n'
#printf '\e[7;32m[7;32m Green\e[0m \e[7;42m[7;42m Green\e[0m\n'
#printf '\e[7;33m[7;33m Brown\e[0m \e[7;43m[7;43m Brown\e[0m\n'
#printf '\e[7;34m[7;34m Blue\e[0m \e[7;44m[7;44m Blue\e[0m\n'
#printf '\e[7;35m[7;35m Purple\e[0m \e[7;45m[7;45m Purple\e[0m\n'
#printf '\e[7;36m[7;36m Cyan\e[0m \e[7;46m[7;46m Cyan\e[0m\n'
#printf '\e[7;37m[7;37m Light grey\e[0m \e[7;47m[7;47m Light grey\e[0m\n'
}
name="$(basename $0)"
if test "$name" = "thebookofcolors"; then
main | less -R
fi