Common ANSI control code table

【Cursor Control】

Control code description
[X;YH Move the cursor to the position of row X and column Y.
[nA Moves the cursor up n columns. <<If it reaches the top of the screen, it is invalid>>
[nB The cursor moves down n columns. <<If it goes to the bottom of the screen, it is invalid>>
[nC The cursor moves to the right n lines. <<If it has reached the far right of the screen, it is invalid>>
[nD The cursor moves to the left n lines. <<It is invalid if it has reached the far left of the screen>>
[6n Reports the cursor position.
[s Saves the current cursor position.
[u Take out the saved cursor position and use it.
[?25l hide cursor
[?25h show cursor

【Screen Control】

Control code description
[2J Clear the screen. <<The function is the same as the CLS command of DOS>>
[K Clears all characters from the cursor position to the end of this column.

【Display character attribute control】

Control code description
[n1;n2;...m Set the attribute state of the displayed character.
If there are more than two settings, separate the codes (n1;n2;..) with a semicolon.
Unless reset, the properties of the original settings are always retained.

code significance
0 All attributes OFF, that is, return to normal display mode (Normal)
1 Brightness
4 Underline
5 Flashing Display (Flash)
7 Inverse display (Inverse)
8 Invisable
prospect background colour
30 40 black
31 41 Red
32 42 green
33 43 yellow
34 44 blue
35 45 Fuchsia
36 46 cyan
37 47 White

【Font Control】

control code illustrate
[0I restore normal font
[1I Song Dynasty
[2I black body
[3I italics

1. Lock\Open keyboard code

   Code definition:
     Lock the keyboard: ESC[2h
     Open the keyboard: ESC[2l
   Application:
   1. Directly type Esc+[+2+h four keys on the keyboard in sequence, the keyboard will be locked.
   2. Include the lock code in the C language program display string: printf("%c[2h string",'\033').
   3. Use the echo command in the shell program: echo "^[[2h string". Note that the input method of the Esc key in the vi editor is: Ctrl+v, then press the Esc key, and ^[ is displayed on the screen to indicate that it has been entered.
   If the unlock code is also displayed, the keypad is unlocked.

Second, delete the character code

   Code definition:
   ESC[nX: Clear n characters to the right of the cursor, the cursor does not move.
   ESC[K or ESC[OK; clears all characters to the right of the cursor, and the cursor does not move.
   ESC[1K: Clear all characters to the left of the cursor, and the cursor does not move.
   ESC[2K: Clear the entire line without moving the cursor.
   ESC[J or ESC[OJ: Clears all characters in the lower right screen of the cursor, and the cursor does not move.
   ESC[1J: Clears all characters in the upper left screen of the cursor, and the cursor does not move.
   ESC[2J or ESCc: Clear the screen and move the cursor to the upper left corner.
   ESC[nM: Delete n lines under the cursor, move the remaining lines up, and the cursor does not move.
   ESC[nP: Delete n characters to the right of the cursor, move the rest to the left, and the cursor does not move.
   Application:
   Generally used in self-developed editors or applications involving line editing.

3. Insert character code

   Code definition:
   ESC[n@: Insert n characters at the current cursor.
   ESC[nL: Insert n lines under the current cursor.
   Application:
   Generally used in self-developed editors or applications involving line editing.

4. Move the cursor

   Code definition:
   ESC[nA: Move the cursor up n lines.
   ESC[nB: Move the cursor down n lines.
   ESC[nC: Move the cursor to the right by n characters.
   ESC[nD: Move the cursor to the left by n characters.
   ESC[n;mH : The cursor is positioned at the nth row and m column (similar to the code ESC[n;mf).
   Application:
   1. Generally used in self-developed editors or applications involving line editing.
   2. Position the cursor in the menu program edited by the shell, such as:
   echo "^[[10;30H, please select: [ ]^[[9C\c", then first position the cursor to 10 lines and 30 columns, and then display "Please Select [ ]", and finally move the cursor 9 characters to the right to locate the brackets and wait for the user's response.

Five, define the character display attribute code

   Code definition:
   ESC[p;p;p;...m Where the attribute value p can be one or more, the specific definition is as follows:
   p Meaning
   0 Clear all attributes
   1 Highlight display
   4 Underline (if supported by hardware)
   5 Flash ( If supported by hardware)
   25 No flicker
   7 Inverse field (front and background swap)
   27 No inverse field
   8 Hidden (do not show)
   10 Select base font
   11 选择第一替代字体;让ASCII值小于32的字 If processing of a directive fails, no further input is read, and scanf() returns. A failure can be either of the following: input failure, mean ing that input characters were unavailable, or matching failure, meaning that the input was inappropriate (see below).The format strin g consists of a sequence of directives which describe how to process the sequence of input characters. If processing of a directi ve fails, no further input is read, and scanf() returns. A failure can be either of the following: input failure, meaning that inp ut characters were unavailable, or matching failure, meaning that the input was inappropriate (see below).The format string consists o f a sequence of directives which describe how to process the sequence of input characters. If processing of a directive fails, no further input is read,and scanf() returns. A failure can be either of the following: input failure, meaning that input character s were unavailable, or matching failure, meaning that the input was inappropriate (see below).符显示时直接取自ROM芯片内
   12 Select the second alternate font; compress and expand the high    -    order
   ASCII    code    values    ​​before displaying
   as    ROM    characters ;white foreground with white underline    39    off underline sign    40 background black    41 background red    42 background green    43 background brown    44 background blue    45 background purple    46 background blue green    47    background white , has its limitations in practical application, the control codes mentioned above are most used in daily life, especially suitable for the design of color pop-up three-dimensional menus. For example, the on-duty program in the central computer room that I designed has a very friendly interface due to the use of these control codes.

















6. Other codes

   Code definition:
   ESC7: Save current cursor position parameters and character attributes.
ESC8: Restore the saved cursor position parameters and character attributes.
   \n : Insert a newline character
   \t : Insert a Tab key.
   \r : Insert a carriage return.
   ^G : Insert a bell character (the pc honks once), press Ctrl+g in vi to input.
   \c : used in shell programs without newlines.
   Application:
   1. User interface design.
   2. It is used in the program that the central computer room sends a notification to the savings counter (combined with the Shida terminal screen save code ESC[/n; mr__ saves the content from line n to line m, and restores the code ESC[/11)

7. Application examples

   1. Color three-dimensional menu (shell language):
  

setcolor-b white
   clear
   while true
   do
   echo
   ^[[8;20H^[[34;43m┍—————————┒^[[47m
   ^[[9;20H^[[34;43m | 1 日 间 业 务 | ^[[40m ^[[47m
   ^[[10;20H^[[34;43m| 2 环 境 维 护 | ^[[40m ^[[47m
   ^[[11;20H^[[34;43m| 请 选 择 [ ]| ^[[40m^[[47m
   ^[[12;20H^[[34;43m┕      ┛^[[40m^[[47m
   ^[[13;22H^[[40m     ^[[47m
   echo″ ^[[11;40H\c″
   read acc
   done

   2. Send a notice to the savings house (C language):
  

 #include<stdio.h>
   main()
   {
   printf(″%c7%c[/2;4r″,'\033',\033');
   printf(″%c[2;5H%c[2K 紧急通知 ″,' \033','\033');
   printf(″%c[3;5H%c[2K由于线路问题,今天不办新余市通存通兑业务“,'[033','\033');
   printf(″%c[4;5H%c[2K 中心机房 1998.11.20%c8″,'\033','033','\033');
   fflush(stdout);
   sleep(10);
   printf “%c[/11″,'\033');
   fflush(stdout);
   }

   Description: The program first saves the cursor position, and saves 2 to 4 lines of content at the same time, then locates the cursor to 2 lines and 5 columns, clears the original content of the line, and then displays the words "emergency notification". After the notification is displayed, the cursor position is restored immediately, and then After waiting for 10 seconds, restore the original content of the screen

Reprinted from: https://blog.csdn.net/sb___itfk/article/details/46534723

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325643815&siteId=291194637