ASCII
Each character in the ASCII table can be represented in 7 bits. For example, the null terminator is represented as 000 0000
The leftmost 8th bit is reserved for Unicode, which will label it as a 0
if the character is within the ASCII table
ASCII reserves the first 32 characters (0-31) as control characters, which are characters not intended to represent printable information.
Control characters refer to things a computer must process, such as an alert, a tab, or a null terminator. All of the
control characters begin with 000
. For instance, a horizontal tab is represented as 000 1001
The Racket with Brackets
After diving down a Wikipedia rabbit hole, I finally figured out what to call all of the punctuation used when coding. Brace yourselves (ha.)
Although by default, "bracket" refers to [these]
brackets, every type of bracket has a name, and a descriptor, as
shown in the table below:
Spoken Name | Technical Name | Character |
---|---|---|
Bracket | Square bracket | [ ] |
Brace | Curly bracket | { } |
Parenthesis | Round bracket | ( ) |
Chevron | Angle bracket | < > |
Modifier Keys
The precedence of modifier keys
- In Apple's list of Human Interface Guidelines they specify the order of modifier key symbols should adhere to the following precedence
-
⌃
-
⌥
-
⇧
-
⌘
-
fn
ANSI Colors
ANSI escape codes are a standard for in-band signaling to control the cursor location, color, and other options on text terminals. The ANSI x3.64 standard was released in 1979 and later updated by the 5th edition ECMA-48 standard in 1991, ISO/IEC 6429 in 1992, and ISO-8613-3 in 1995. ANSI escape codes are started using \e[ and contain numeric codes separated by semicolons. The escape code is terminated with an 'm'. ANSI Color Codes.
ANSI Color Escape Sequences
Code | Effect | Note |
---|---|---|
\x1b[ |
CSI | Control Sequence Indicator |
CSI n m | ANSICOLOR | ANSI color code (Select Graphic Rendition) |
CSI 38 ; 5 ; n m | 256COLOR | Foreground 8-bit 256 color code |
CSI 48 ; 5 ; n m | 256COLOR | Background 8-bit 256 color code |
CSI 38 ; 2 ; r ; g ; b m | TRUECOLOR | Foreground 24-bit RGB color code |
CSI 48 ; 2 ; r ; g ; b m | TRUECOLOR | Background 24-bit RGB color code |
The 16 ANSI Colors
Color | Code | Hex Value | Code | Hex Value |
---|---|---|---|---|
Black | 0 |
#000000
|
8 |
#555555
|
Red | 1 |
#aa0000
|
9 |
#ff5555
|
Green | 2 |
#00aa00
|
10 |
#55ff55
|
Yellow | 3 |
#aaaa00
|
11 |
#ffff55
|
Blue | 4 |
#0000aa
|
12 |
#5555ff
|
Magenta | 5 |
#aa00aa
|
13 |
#ff55ff
|
Cyan | 6 |
#00aaaa
|
14 |
#55ffff
|
White | 7 |
#aaaaaa
|
15 |
#ffffff
|
The xterm 256 colors
In 1999, Thomas Dickey, an xterm maintainer, using a patch created by Todd Larason to add support for 256 colors. These
colors form a compatible extension to the OG 16 ANSI colors and has been adopted by most terminal emulators, including
the macOS Terminal application, which runs on xterm
, as well as MUD clients.
To send a 256 color foreground color one must print \e[38;5;COLORm
where COLOR is a number
between 0 and 255.
For background colors, it's pretty much the same, but the escape sequence starts with 48
, instead of 38
. Therefore,
one must print \e[48;5;COLORm
where COLOR is a number between 0 and 255.
Name | Code | Dim | Code | Bold |
Azure | 025 |
0066bb |
033 |
0088ff |
Blue | 019 |
0000bb |
021 |
0000ff |
Cyan | 037 |
00bbbb |
051 |
00ffff |
Ebony | 016 |
000000 |
059 |
666666 |
Green | 034 |
00bb00 |
046 |
00ff00 |
Jade | 035 |
00bb66 |
048 |
00ff88 |
Lime | 070 |
66bb00 |
118 |
88ff00 |
Magenta | 127 |
bb00bb |
201 |
ff00ff |
Orange | 130 |
bb6600 |
208 |
ff8800 |
Pink | 125 |
bb0066 |
198 |
ff0088 |
Red | 124 |
bb0000 |
196 |
ff0000 |
Silver | 102 |
888888 |
188 |
dddddd |
Tan | 094 |
886600 |
178 |
ddbb00 |
Violet | 055 |
6600bb |
093 |
8800ff |
White | 145 |
bbbbbb |
231 |
ffffff |
Yellow | 142 |
bbbb00 |
226 |
ffff00 |
50 24 shades of gray
(Couldn't help it.) To display code 232 through 255 using 24 bit RGB colors the following hexadecimal values are suggested for the 24 shade grayscale: start out at 8 for the first color and increment the value by 10 for each increment. \x08, \x12, \x1C, \x26, etc.
Select Graphic Rendition (SGR)
SGR Color Codes are used to format output on terminals, as well as in many coding languages, such as java
. An SGR
color code can be expressed by typing the escape character 0x1b
followed by [
then the code number (e.g. 42
) and
then the letter m
. When you put that all together, it looks like \x1b[42m
.The formatting will persist until the
reset code is given, which is code #0. You can specify this with \x1b[0m
- It's worth noting special attention to the escape character, which is decimal number
27
or hex value0x1b
because it's often typed. For instance, try entering this command in your terminal. It will print a green background.
echo -e -n '\x1b[42m Green \x1b[0m\n'
Here are some of the most useful codes:
Font Style
Escape Code | Function |
---|---|
\x1b[0m |
reset all fonts, formats, colors, etc. |
\x1b[1m |
enable bold font |
\x1b[2m |
enable faded font |
\x1b[3m |
enable italic font |
\x1b[4m |
enable underlined font |
\x1b[5m |
enable blinking font |
\x1b[7m |
enable inverted-font |
\x1b[22m |
disable bold font |
\x1b[23m |
disable italic font |
\x1b[24m |
disable underlined font |
\x1b[25m |
disable blinking font |
\x1b[27m |
disable inverted-font |
Foreground Color
Color | Foreground | Background |
---|---|---|
black | \x1b[30m |
\x1b[40m |
red | \x1b[31m |
\x1b[41m |
green | \x1b[32m |
\x1b[42m |
yellow | \x1b[33m |
\x1b[43m |
blue | \x1b[34m |
\x1b[44m |
magenta | \x1b[35m |
\x1b[45m |
cyan | \x1b[36m |
\x1b[46m |
white | \x1b[37m |
\x1b[47m |
select 256-color | \x1b[38;5;<0-255> |
\x1b[48;5;<0-255> |
default | \x1b[39m |
\x1b[49m |
The 256 colors of xterm
The first 16 colors have RGB tuples predetermined by ANSI that don't follow the same formula as the others
typeset -A rgb256
let index=0
# First two rows, colors [0..15]
for ((row = 0; row <= 1; row += 1)); do
for ((blue = 0; blue <= 1; blue += 1)); do
for ((green = 0; green <= 1; green += 1)); do
for ((red = 0; red <= 1; red += 1)); do
done
done
done
done
# Second row, colors [8..15]
predeter
XColorTable() {
i=16
for ((r = 0; r <= 255; r+=40)); do
for ((g = 0; g <= 255; g+=40)); do
for ((b = 0; b <= 255; b+=40)); do
echo "Color$((i++)) = (${r}, ${g}, ${b})"
if ((b == 0)); then
b=55;
fi
done
if ((b == 0)); then
g=55;
fi
done
if ((r == 0)); then
r=55;
fi
done
for ((m = 8; m <= 238; m+=10)); do # Do Monochrome
echo "Color$((i++)) = (${m}, ${m}, ${m})"
done
}
SGR Cursor Movements
Cursor/Screen Color
Escape Code | Function |
---|---|
\Ec |
Reset screen to initial state |
Modifying Content
Escape Code | Function |
---|---|
\E[J / \E[0J |
Clear from cursor to end of screen |
\E[1J |
Clear from cursor to beginning of stream |
\E[2J |
Clear entire screen |
\E[3J |
Clear entire screen and delete all lines saved in the scrollback buffer |
\E[K / \E[0K |
Erase from cursor to end of line |
\E[1K |
Erase start-of-line up until the cursor |
\E[2K |
Erase the entire line |
\E[2X |
Erase 2 characters (replaced with whitespace) |
\E[2P |
Delete 2 characters (left-shift the text that follows) |
\E[2M |
Delete 2 lines |
\E[2@ |
Insert 2 spaces (right-shift the text that follows) |
\E[2L |
Insert 2 lines (right-shift the text that follows) |
Modify Screen
Escape Code | Function |
---|---|
\E[2S |
Scroll up 2 lines |
\E[2T |
Scroll down 2 lines |
Cursor Movement
Escape Code | Function |
---|---|
\E[2A |
Shift cursor up by 2 rows |
\E[2B |
Shift cursor down 2 rows |
\E[2C |
Shift cursor right 2 columns |
\E[2D |
Shift cursor left 2 columns |
\E[2E |
Shift cursor to the 1st column, 2 rows below |
\E[2F |
Shift cursor to the 1st column, 2 rows above |
\E[2G |
Move cursor to column 2 (current row) |
\E[2d |
Move cursor to row 2 (current column) |
\E[i;jH |
Move cursor to row i , column j |
Misc.
Escape Code | Function |
---|---|
\E[nb |
Repeat previous character n times |
\E7 |
Save cursor position |
\E8 |
Restore cursor position |
-
Repeat the last character 2 times
print -N '1234567890' '\E[2b'
123456789000
-
Replace a character
print -N {9..1} '\E[3D' '_' '\E[2C'
1_3456789
-
Total black magic since I forgot what the instruction set was
print -N '#####' $'\E7\E[2G1\E82'
#1###2
-
Go to the 2nd row, add a 1, return, add a 2
print -N '#####' '\E7' '\E[2G' '1' '\E8' '2'
#1###2
-
Go back 2 columns, up 1 row, add a '0', return
print -N '####\n####' '\E7' '\E[2D' '\E[1A' '0' '\E8'
-
Go back 3 columns, and erase 2 characters
print -N 'hello' '\E[3D' '\E[2X'
he o
-
Go back three columns, and remove two characters
print -N 'hello '\E[3D '\E[2P'
heo
-
Replace the '+' in the 1st column, 2nd-to-last row with '-'
print -N +{5..1}$'\n' '\E7' '\E[2A' '-' '\E8'
+5 +4 +3 -2 +1
-
Add two blank lines
print -N +{5..1}$'\n' '\E[3A' '\E[2L' '\E[2B' '\E[3B'
+5 +4
+3 +2 +1
-
The program I wrote for practice,
globetrot
:for row in {2..${LINES}}; do # Print a row of '#' characters print -f '#'%.s {1..${COLUMNS}} # Erase the left-most and right-most characters # (save) (left 1) (erase 1) (col 1) (erase 1) (return) print -N '\E7' '\E[1D' '\E[X' '\E[1G' '\E[1X' '\E8' '\n' done # Erase all characters on the first row print -N '\E7' '\E[1d' '\E[2K' '\E8' # Erase all characters on the last row print -N '\E[1A' '\E[2K' '\E[1B' typeset -i i typeset -i j # Walk the '@' symbol across the first row [left -> right] for (( j=1; j <= ${COLUMNS}; j+=1 )); do print -N '\E7' "\E[1;${j}H" '\E[2K' '@' '\E8' sleep 0.01 done # Anchor the symbol to the last column ((j=${COLUMNS})) # Walk the '@' symbol along the last column [top -> bottom] for (( i=2; i < ${LINES}; i+=1 )); do # Erase the previous '@' print -N '\E7' "\E[$((i-1));${j}H" '\E[X' '\E8' # Create the next '@' print -N '\E7' "\E[${i};${j}H" '@' '\E8' sleep 0.01 done ((i=${LINES}-1)) # Walk the '@' symbol along the last row, [right -> left] for (( j=${COLUMNS}-1; j>=0; j-=1 )); do # Erase the previous '@' print -N '\E7' "\E[${i};$((j+1));H" '\E[X' '\E8' # Create the next '@' print -N '\E7' "\E[${i};${j}H" '@' '\E8' sleep 0.01; done # Anchor the symbol to the first column ((j=1)) # Walk the '@' symbol along the first column [bottom -> top] for (( i=${LINES}-2; i >= 0; i-=1 )); do # Erase the previous '@' print -N '\E7' "\E[$((i+1));${j}H" '\E[X' '\E8' # Create the next '@' print -N '\E7' "\E[${i};${j}H" '@' '\E8' sleep 0.01 done
Unicode
Unicode Transformation Format (UTF)
Unicode Transformation Format (UTF) is one of the mapping methods engineered to encode text. It does this by mapping code points to code values. Each code value is a unique sequence of bytes.
UTF-16
The UTF-16 encoding system, is not as simple as it's name suggests. Each char is not encoded with 16 bits, as is commonly assumed. UTF-16 is a variable-width encoding format.
Java's char
object is encoded using UTF-16 and so are Windows filenames, as
well as the C++ RESTful API SDK written my Microsoft, as well as the macOS
operating system.
It's rarely advantageous to use UTF-16 over UTF-8. The only time it will result in a smaller file size is if the majority of text in the file consists of Chinese or Japanese characters. Even so, if there is a large amount of whitespace (which is an ASCII character) then the UTF-8 encoding would still result in a smaller file size. The standard norm is increasingly becoming UTF-8, and the trend shows no sign of slowing down.
UTF-32
UTF-32, unlike its brothers, is a fixed-width encoding format. Every character is guaranteed to be represented by exactly 4 bytes. UTF-32 is rarely used. Requiring every character to be represented with 4 bytes results in a significant increase in file size. It is slightly faster to read than UTF-8 but the difference is barely measurable.
Lastly, UTF-32 is problematic because it results in encoding many 8-bit strings
of 0
's. Traditional software interprets this as the null terminator, which
signals the end of the string, which would truncate the remaining information
previously encoded by UTF-32.