signaling

In our daily life, we often make phone calls. When the receiver is picked up, the phone sends a pick up message to the switch. Then we hear a continuous “buzz”, which is sent by the switch, telling us that we can dial. When you dial the other party, you will hear the “da-da-da” voice calling the other party. This is the message sent by the exchange office, telling us that the other party is calling to answer the call. .

The off-hook information, dial-up information, and ring-back information are used to establish communication between the two parties. Such information is called signaling. (Signaling is the language of communication between switches)

in-bind signling

The Chinese translation is called in-band signaling. Signaling is the information used to establish a communication relationship between two parties. After communication is established, the message is the content of communication. In – band signaling means that signaling and communication content use the same channel. Specific principles and knowledge is very complex, here only to understand.

ANSI escape code

ANSI escape sequences are in-bind signaling standards for command line terminals that control cursor position, font color, and other terminal options. Typically, certain sequences of bytes (as defined by in-band signaling) are embedded in the text, most of them beginning with the ESC escape character and the “[” character, which are interpreted by the terminal as corresponding instructions rather than ordinary character encodings.

In the terminal, there are characters in the ASCII encoding that cannot be used for printing display, such as ‘\ A ‘(0x7) for ring and ‘\n’ (0x0A) for newline. These characters are called control characters. The control character ‘\e’ (0x1B), which represents ESC, the function of the ESC key on the keyboard. ESC is a contraction of the word escape, which means to escape. The occurrence of this Escape character in the text indicates that the following character is the ANSI Escape code encoding.

If the terminal output does not support ANSI escape codes, you may see the following information:

ESC[1; 30m 30 �[1;30m 30 �[1;30m 30 �[1;30m 30 �[1;30m 30 �[1;30m 30 �[0m �[1;31m 31 �[1; 31M 31 �[1; 31M 31 �[1; 31M 31 �[1; 31M 31 �[1; 31M 31 �[1; 31M 31 �[0M �[1; 32M 32 �[1; 32M 32 �[1;32m 32 [1; 32 m � � [1; 32 m � [1; 32 m � [1; 32 m � [1; 32 m � [0 m...Copy the code

ANSI color

The ANSI Escape Code encoding has special controls for character colors, such as:

\e[37;44;3;1m
Copy the code
  • \eRepresents the start ANSI Escape code
  • [CSI, Control Sequence Introducer
  • 37. 44. 4. 1Representatives to; Delimited text style control, where 37 means the foreground of the text is white, 44 means the background is blue, 3 means italic, and 1 means bold
  • mRepresents the end control character sequence

For example, from the terminal command line

echo -e "\e[37;44;3;1mLYL\e[0m"
Copy the code

The -e argument is used to enable echo command control transcoding, ending with \e[0m for resetting text styles.

Because the hexadecimal code of the \ E control character is 0x1B and the hexadecimal code of the \e control character is 033, the following representation is equivalent:

  • \e[0m
  • \x1b[0m
  • \x1B[0m
  • \033[0m
  • \u001b[0m

Common text style control character

code role note
0 Reset/Normal Close all properties.
1 Bold or increase strength
2 Weaken (reduce strength) Not widely supported.
3 italics Not widely supported. Sometimes seen as an inverse display.
4 The underline
5 Slow flashing Less than 150 beats per minute.
6 Rapid flashing Ms-dos ANSI. SYS; More than 150 per minute; Not widely supported.
7 Reverse video Foreground color and background color exchange.
8 hidden Not widely supported.
9 delete Characters clear, but marked for deletion. Not widely supported.
10 Primary (default) font
11–19 To replace the font Choose an alternative font
20 Angle of body There is little support.
21 Turn off bold or double underline Closed bold is not widely supported; Double underline has little support.
22 Normal color or strength Neither strong nor weak.
23 Non-italic, non-angular
24 Turn off underscores Remove single and double underscores.
25 Closed flashing
27 Closed the show
28 Close hide
29 Close the delete
30–37 Set the foreground See color chart below.
38 Set the foreground The next parameter is 5; N or 2; r; g; B, see you next.
39 Default foreground color Defined by the implementation (according to the standard).
40–47 Set the background color See color chart below.
48 Set the background color The next parameter is 5; N or 2; r; g; B, see you next.
49 Default background color Defined by the implementation (according to the standard).
51 Framed
52 Encircled
53 On the line
54 Not framed or encircled
55 Close off line
60 Ideographic text underline or right line There is little support.
61 Ideographic double underline or double right line
62 Underline or line to the left of ideograms
63 Ideographic double crossed or double left line
64 Ideograms focus on signs
65 The ideograph property is closed Reset all effects from 60 to 64.
90-97. Set up bright foreground views Aixterm (non-standard).
100-107. Set a bright background color Aixterm (non-standard).

The following figure corresponds to the case of only one color code in the above table (color code only supports 3 or 4 bits, i.e. only 8 or 16).

Why 8 or 16? Since there were only eight colors in the original specification, they were given only names. SGR parameters 30-37 select the foreground color and 40-47 select the background color. Quite a few terminals implement “bold” (SGR code 1) as a brighter color rather than a different font, thus providing eight additional foreground colors, but generally not for background colors

The following figure shows the color selection of the setting background corresponding to code 38 or 48 (i.e., 8-bit code, 256 colors in total (2^8))

  • 0-7: Standard color (same as ESC [30 — 37 m)
  • 8-15: high intensity color (same as ESC [90 — 97 m)
  • (0 ≤ r, g, b ≤ 5) : 16 + 36 × R + 6 × g + b (0 ≤ r, g, b ≤ 5)
  • 232-255:24 grayscale colors from black to white

The code format is as follows

\ [e... 38; 5; < n >... m # select foreground \ e [... 48; 5; < n >... m # select the background colorCopy the code

With newer iterations of graphics cards, some terminals now support rendering 24-bit colors. Format is as follows

\ [e... 38; 2, < r >, < g >, < b >... m # select RGB foreground \ e [... 48; 2, < r >, < g >, < b >... m # select RGB background colorCopy the code

The font style controls above can be mixed, while the foreground and background colors are set below

This article focuses on ANSI color, other terminal control input and output content is not studied.

The practical application

When developing logging components on the Web side or command-line terminals on Web pages, knowing ANSI escape codes makes your job easier. Because it might save you from staring at a bunch of gibberish. There are, of course, third-party libraries that can be used to translate ANSI Color into HTML output to make things easier. For example github.com/drudru/ansi…

reference

  • www.istr.cn/archives/29…
  • Zh.wikipedia.org/wiki/ANSI%E…