본문 바로가기
DSP, MCU/펌웨어

터미널 제어문자 ANSI escape code / 터미널에 글자색 배경색 입히기

by eteo 2023. 2. 20.

 

터미널 제어 문자는 컴퓨터 터미널 화면의 커서 위치 및 색상 등을 제어하는 용도로 사용되는 특수한 문자들로 이에 대한 표준은 ANSI escape code이다.

 

1970년대 최초로 이러한 터미널 제어기능을 지원하는 VT100이라는 터미널 에뮬레이터가 등장하였고, 이후에 나온 터미널들도 VT100과 호환성을 가질 수 있도록 기존 제어 문자에 확장된 기능을 추가하여 ANSI escape code가 ANSI 표준으로 제정되게 되었다.

 

 

 

 

사용 예시

 

print("\033[31;43mHello, World!\033[0m\r\n")

 

여기서 \033 은 터미널 제어를 위한 ANSI 이스케이프 시퀀스(컨트롤 문자인 ESC(escape)의 ASCII 코드값)로 "\033" 뒤에 명령인수가 온다.

 

[31m은 텍스트를 빨간색으로 지정하고,

[43m은 배경색을 노란색으로 설정한다.

;는 구분자이고 [0m으로 텍스트 스타일을 초기화한다.

 

 

 

 

 

 

 

 

참고로 시리얼 모니터에서 Clear Screen을 했을 때 입력되는 터미널 제어 문자는 아래와 같다.

 

\033[2J

 

[2J는 Clear Screen을 의미하는 컨트롤 코드로서, 현재 터미널 화면의 내용을 모두 지우고 커서를 화면의 맨 처음(좌측 상단)으로 이동시킨다.

 

 

 

 

참고글

 

 

http://matthieu.benoit.free.fr/68hc11/vt100.htm

 

VT100 ESCape Codes

 

matthieu.benoit.free.fr

 

http://domoticx.com/terminal-codes-ansivt100/

 

Terminal codes (ANSI/VT100) - DomoticX Knowledge Center

Terminal (control-)codes are needed to give specific commands to your terminal. This can be related to switching colors or positioning the cursor.

domoticx.com

 

 

https://en.wikipedia.org/wiki/ANSI_escape_code

 

ANSI escape code - Wikipedia

From Wikipedia, the free encyclopedia Method used for display options on video text terminals ANSI escape sequences are a standard for in-band signaling to control cursor location, color, font styling, and other options on video text terminals and terminal

en.wikipedia.org

 

 

'DSP, MCU > 펌웨어' 카테고리의 다른 글

MDIO(Management Data Input/Output)  (0) 2023.03.12
UML State Machine  (0) 2023.02.27
CAN FD 프로토콜  (0) 2023.02.20
YMODEM, XMODEM 프로토콜  (0) 2023.02.15
데이터 무결성 검사, 체크섬과 CRC  (0) 2023.02.11