ANSI escape code 본문

프로그램 일반

ANSI escape code

keydisk 2010. 9. 15. 11:24

 

1 ANSI escape code
원본 출처 : http://sunyzero.egloos.com/4282610
터미널 테스트 코드
 echo -e "\e[33m"
echo 문이 실행된 후에는 프롬프트 색상이 모두 노란색으로 바뀜. 이 후 타이핑하는 모든 글자는 다 노랗게 보임.
원상복귀는 echo -e "\e[m"

예제의 "\e[33m"과 같은 형태를 ANSI escape sequence라고 부르는데, echo문에서 이를 출력하여 실행하려면 ANSI escape code를 인식하는 옵션인 -e를 같이 넣어줘야 한다. 넣지 않으면 그냥 문자열로 인식해서 단순출력해버린다.

또한 ANSI escape sequence에서 시작문자인"\e["는 CSI(Control Sequence Introducer)라고 부르며 뒤의 33m, m 같은 것이 ANSI escape sequence의 코드 내용 부분이 되겠다.

CSI의 \e는 ASCII코드의 ESC키를 의미하므로 ESC의 ASCII값인 8진수 033(=decimal 27)을 사용하여 \033으로 표기할 수도 있다. 따라서 위의 "\e[33m"는 "\033[33m"으로 써도 된다.

ANSI Escape sequence = CSI + n [;+ ...] + letter                       CSI = \e[ = \033[CSI 다음에 나오는 n은 숫자이며 복수개가 나올때는 세미콜론(;)으로 구분한다. 마지막의 영문자 1개는 ANSI escape sequence의 명령(command)에 해당한다. 예를 들어 T는 스크롤 다운, m은 색상 변경등의 기능을 가진다. 색상 변경은 SGR(Select Graphic Rendition)이라고 부른다.

 

2. 명령 코드
원본 출처 : http://www.termsys.demon.co.uk/vtansi.htm

Device Status

The following codes are used for reporting terminal/display settings, and vary depending on the implementation:

Query Device Code	<ESC>[c
  • Requests a Report Device Code response from the device.

Report Device Code	<ESC>[{code}0c
  • Generated by the device in response to Query Device Code request.

Query Device Status	<ESC>[5n
  • Requests a Report Device Status response from the device.

Report Device OK	<ESC>[0n
  • Generated by the device in response to a Query Device Status request; indicates that device is functioning correctly.

Report Device Failure	<ESC>[3n
  • Generated by the device in response to a Query Device Status request; indicates that device is functioning improperly.

Query Cursor Position	<ESC>[6n
  • Requests a Report Cursor Position response from the device.

Report Cursor Position	<ESC>[{ROW};{COLUMN}R
  • Generated by the device in response to a Query Cursor Position request; reports current cursor position.


Terminal Setup

The h and l codes are used for setting terminal/display mode, and vary depending on the implementation. Line Wrap is one of the few setup codes that tend to be used consistently:

Reset Device		<ESC>c
  • Reset all terminal settings to default.

Enable Line Wrap	<ESC>[7h
  • Text wraps to next line if longer than the length of the display area.

Disable Line Wrap	<ESC>[7l
  • Disables line wrapping.


Fonts

Some terminals support multiple fonts: normal/bold, swiss/italic, etc. There are a variety of special codes for certain terminals; the following are fairly standard:

Font Set G0		<ESC>(
  • Set default font.

Font Set G1		<ESC>)
  • Set alternate font.


Cursor Control

Cursor Home 		<ESC>[{ROW};{COLUMN}H
  • Sets the cursor position where subsequent text will begin. If no row/column parameters are provided (ie. <ESC>[H), the cursor will move to the home position, at the upper left of the screen.

Cursor Up		<ESC>[{COUNT}A
  • Moves the cursor up by COUNT rows; the default count is 1.

Cursor Down		<ESC>[{COUNT}B
  • Moves the cursor down by COUNT rows; the default count is 1.

Cursor Forward		<ESC>[{COUNT}C
  • Moves the cursor forward by COUNT columns; the default count is 1.

Cursor Backward		<ESC>[{COUNT}D
  • Moves the cursor backward by COUNT columns; the default count is 1.

Force Cursor Position	<ESC>[{ROW};{COLUMN}f
  • Identical to Cursor Home.

Save Cursor		<ESC>[s
  • Save current cursor position.

Unsave Cursor		<ESC>[u
  • Restores cursor position after a Save Cursor.

Save Cursor & Attrs	<ESC>7
  • Save current cursor position.

Restore Cursor & Attrs	<ESC>8
  • Restores cursor position after a Save Cursor.


Scrolling

Scroll Screen		<ESC>[r
  • Enable scrolling for entire display.

Scroll Screen		<ESC>[{start};{end}r
  • Enable scrolling from row {start} to row {end}.

Scroll Down		<ESC>D
  • Scroll display down one line.

Scroll Up		<ESC>M
  • Scroll display up one line.


Tab Control

Set Tab 		<ESC>H
  • Sets a tab at the current position.

Clear Tab 		<ESC>[g
  • Clears tab at the current position.

Clear All Tabs 		<ESC>[3g
  • Clears all tabs.


Erasing Text

Erase End of Line	<ESC>[K
  • Erases from the current cursor position to the end of the current line.

Erase Start of Line	<ESC>[1K
  • Erases from the current cursor position to the start of the current line.

Erase Line		<ESC>[2K
  • Erases the entire current line.

Erase Down		<ESC>[J
  • Erases the screen from the current line down to the bottom of the screen.

Erase Up		<ESC>[1J
  • Erases the screen from the current line up to the top of the screen.

Erase Screen		<ESC>[2J
  • Erases the screen with the background colour and moves the cursor to home.


Printing

Some terminals support local printing:

Print Screen		<ESC>[i
  • Print the current screen.

Print Line		<ESC>[1i
  • Print the current line.

Stop Print Log		<ESC>[4i
  • Disable log.

Start Print Log		<ESC>[5i
  • Start log; all received text is echoed to a printer.


Define Key

Set Key Definition	<ESC>[{key};"{string}"p
  • Associates a string of text to a keyboard key. {key} indicates the key by its ASCII value in decimal.


Set Display Attributes

Set Attribute Mode	<ESC>[{attr1};...;{attrn}m
  • Sets multiple display attribute settings. The following lists standard attributes:
    0	Reset all attributes
    1	Bright
    2	Dim
    4	Underscore	
    5	Blink
    7	Reverse
    8	Hidden
    
    	Foreground Colours
    30	Black
    31	Red
    32	Green
    33	Yellow
    34	Blue
    35	Magenta
    36	Cyan
    37	White
    
    	Background Colours
    40	Black
    41	Red
    42	Green
    43	Yellow
    44	Blue
    45	Magenta
    46	Cyan
    47	White