"CSI" that I want to teach beginners in interactive console application production

Introduction

I'm not an advanced console app ** I just happened to know CSI while I was making various things, so I wrote it.

What is CSI

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 *.

What can i do

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.

About operation

~~ ** 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.

Try using CSI

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.

CSI control list (partial)

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

Behavior of screen erase (* CSI * * n * J) and line erase (* CSI * * n * K)

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)

SGR parameters (typical)

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;nYou can specify the color index with(0...255)。\e[38;2;r;g;bYou 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;nYou can specify the color index with(0...255)。\e[48;2;r;g;bYou can set RGB with(0 each...255)。
49 Reset background color

What's this unsupported storm ...

Color number

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. スクリーンショット 2016-08-05 10.39.55.png

How to use SGR

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

"CSI" that I want to teach beginners in interactive console application production
Convenient Linux keyboard operation that I want to teach myself when I was in school
I want to output to the console coolly
I want to print in a comprehension
I want to use IPython Qt Console
I want to embed Matplotlib in PySimpleGUI
I made a web application in Python that converts Markdown to HTML
I want to use the Django Debug Toolbar in my Ajax application
I want to pin Datetime.now in Django tests
I want to create a window in Python
I want to create a priority queue that can be updated in Python (2.7)
I want to store DB information in list
I want to merge nested dicts in Python
I want to create a web application that uses League of Legends data ①
I want to display the progress in Python!
The story of Linux that I want to teach myself half a year ago
I want to write in Python! (1) Code format check
Even beginners want to say "I fully understand Python"
I want to embed a variable in a Python string
I want to easily implement a timeout in python
Python that I would like to recommend to programming beginners
I want to transition with a button in flask
I want to use self in Backpropagation (tf.custom_gradient) (tensorflow)
I want to write in Python! (2) Let's write a test
Even in JavaScript, I want to see Python `range ()`!
I want to develop an Android application on Android (debugging)
I want to randomly sample a file in Python
I want to work with a robot in python.
I want to write in Python! (3) Utilize the mock
I want to say that there is data preprocessing ~
I want to use the R dataset in python
I want to do something in Python when I finish
I want to manipulate strings in Kotlin like Python!
Super easy molecular phylogenetic tree creation technique that I do not want to teach anyone
[For beginners] I want to explain the number of learning times in an easy-to-understand manner.
I want to easily delete columns containing NA in R
I want to do something like sort uniq in Python
[Django] I want to log in automatically after new registration
I want to make the Dictionary type in the List unique
[Introduction to Pytorch] I want to generate sentences in news articles
I want to count unique values in arrays and tuples
I want to align the significant figures in the Numpy array
I want to be able to run Python in VS Code
I want to make input () a nice complement in python
I didn't want to write the AWS key in the program
[Python] I tried to explain words that are difficult for beginners to understand in an easy-to-understand manner.
I want to solve Sudoku (Sudoku)
I want to use a wildcard that I want to shell with Python remove
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
I want to run Rails with rails s even in vagrant environment
Qiskit: I want to create a circuit that creates arbitrary states! !!
LINEbot development, I want to check the operation in the local environment
[Python / AWS Lambda layers] I want to reuse only module in AWS Lambda Layers
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I want to create a pipfile and reflect it in docker
I want to make the second line the column name in pandas
I want to pass the G test in one month Day 1
I want to know the population of each country in the world.
[For beginners] I want to get the index of an element that satisfies a certain conditional expression