I'm not an advanced console app ** I just happened to know CSI while I was making various things, so I wrote it.
A type of ** escape sequence **.
The first thing that comes to mind when you hear the word "escape" is \ n
.
\ n
is a very simple escape that means a newline in Unix, but this time we'll use a slightly more complicated special expression.
By the way, CSI is an acronym for * Control Sequence Introducer *.
Typical ones --Move the cursor --Erase the screen (Erase everything before the cursor, delete everything after the cursor) --Clear line --Erase characters --Erase characters in rectangular units --Character style (bold, font color, background color, etc.) settings --Control of window information (size, etc.) etc…
** here ** has all the control types and methods.
~~ ** Bash doesn't seem to escape easily ** (reference). ~~
[Addition: Comment information]
It seems that you can escape with Bash. Escapes are applied with the -e
option, such as ʻecho -e`.
Also, the above link was another discussion. We will correct.
Now, in the previous example, we have shown \ n
for line breaks.
Since CSI can control multiple things, the control code is not one, but there is a prefix to recognize that it is CSI.
That is ** \ e [
** or ** \ 033 [
** or ** \ 0x1b [
**.
[Addition]
I thought bash couldn't escape, just \ e [
didn't support it. It is possible with \ 033 [
.
Since e is a character type, 033 is an octal number, and 0x1b is a hexadecimal number, it is essentially the same, but e is easier.
CSI is a kind of [^ 1] escape sequence in the narrow sense. Inserting \ e
at the beginning tells you the beginning of the escape sequence, and [
tells you that it is CSI.
CSI control changes depending on what you write after this.
Control characters (no spaces required) | motion |
---|---|
n A | n lines(1 line if not specified)Raise the cursor |
n B | n lines(1 line if not specified)Lower the cursor |
n C | n characters(1 character unless specified)Advance the cursor |
n D | n characters(1 character unless specified)Move the cursor back |
n E | Place the cursor on n lines(1 line if not specified)Align to the beginning below |
n F | Place the cursor on n lines(1 line if not specified)Fit to the beginning of the top |
n G | Place the cursor on the nth character of the current line(1st character if not specified)To match |
n; m H | Place the cursor on the nth line, m character(1 character per line unless otherwise specified:upper left)To match |
n J | Clear screen (details will be described later) |
n K | Delete line (details will be described later) |
n S | n lines (1 line if not specified) Scroll the screen to proceed |
n T | n lines (1 line if not specified) Scroll back the screen |
n L | Insert n lines (1 line if not specified) before the line where the cursor is |
n M | Delete n lines (1 line if not specified) from the line where the cursor is |
n P | Delete n characters (1 character if not specified) from the cursor position |
n P | Replace n characters (1 character if not specified) with blanks from the cursor position |
n ` | Move the cursor to the nth character of the current line (1st character if not specified) |
n a | Advance the cursor by n characters (1 character if not specified) |
n d | Raises the cursor n lines at the current position (1 line if not specified) |
n m | SGR(Select Graphic Rendition)Specify parameters (details will be described later) |
s | Remember the current cursor position |
u | Call the memorized cursor position |
> 3; a; b; c; d J | (b, a)From(d, c)Erase rectangle up to |
> 3; a; b; K | Erase characters a to b on the current line |
n | behavior |
---|---|
Unspecified | Erase behind the cursor to the beginning of the screen (line) |
0 | Erase behind the cursor to the beginning of the screen (line) |
1 | Erase before the cursor to the end of the screen (line) |
2 | Erase the entire screen (line) |
n | meaning |
---|---|
None | Parameter reset |
0 | Parameter reset |
1 | Bold |
2 | Light print (not widely supported) |
3 | Italic (not widely supported, inverted)(n=7)Can also be) |
4 | Underline |
5 | Blinking: 150 times/Within minutes |
6 | Fast flashing: 150 times/More than a minute (not widely supported) |
7 | Reverse display: Foreground and background colors are swapped |
8 | Hide text (not widely supported) |
9 | Strikethrough (not widely supported) |
10 | Default font |
11–19 | (n-10)Use the second alternative font |
20 | Fraktur font(Mostlyunsupported) |
21 | Bold OFF (not widely supported),Double underline (almost unsupported) |
22 | Restore color and emphasis (bold / light) |
23 | Restore italics and fraktur |
24 | Cancel underline |
25 | Cancel blinking |
28 | Unhide text |
29 | Erase the strikethrough |
30–37 | Foreground color (essentially text color)(n-30)Change to number (color number will be described later) |
38 | Foreground color setting expansion.\e[38;5;n You can specify the color index with(0...255)。\e[38;2;r;g;b You can set RGB with(0 each...255)。 |
39 | Reset foreground color |
40–47 | Background color(n-40)Change to number (color number will be described later) |
48 | Background scenery setting expansion.\e[48;5;n You can specify the color index with(0...255)。\e[48;2;r;g;b You can set RGB with(0 each...255)。 |
49 | Reset background color |
What's this unsupported storm ...
number | color |
---|---|
0 | black |
1 | Red |
2 | Green |
3 | yellow |
4 | Blue |
5 | Magenta |
6 | cyan |
7 | White |
It looks like this when you roughly display each color.
When actually setting the SGR
echo "\e[31mred\e[m"
If you do, it will be displayed as red
in red letters.
Also,
echo "\e[31;43mred&yellow\e[m"
If you do, it will display red & yellow
with a red character yellow background.
You can set multiple options at once by typing the options separated by ;
in this way.
** Be sure to turn off the option at the end with \ e [m
or \ e [0m
"to avoid unexpected behavior after CSI exits. ** **
[^ 1]: Strictly speaking, \ n
is not an escape sequence. In a narrow sense, it refers to a control byte string starting with ʻESC (0x1B)`.
Recommended Posts