ANSI escape code를 이용하여 Terminal의 Color 및 다양한 변화를 줄 수 가 있다.
물론 개별 Terminal 마다 지원되는 부분이 다를 수 있으므로 주의하자
ANSI escape code는 ESC Key Code기반으로 Print를 이용하여 다양한 설정가능하며, 주로 내가 사용할 것은 CSI->SGR로 색상설정이다.
아래의 내용은 전부 Wiki에서 가져왔으며, 세부사항은 Wiki를 보도록하자.
ANSI_escape_code 정보
https://en.wikipedia.org/wiki/ANSI_escape_code
https://en.wikipedia.org/wiki/C0_and_C1_control_codes
Terminal color (색상정보 값 확인)
https://misc.flogisoft.com/bash/tip_colors_and_formatting
1.1 Escape Sequences
ESC (dec 27 / hex 0x1B / oct 033) 의 ESC 값을 기반으로 사용되지는 Sequences들을 말한다.
글자색상변화 하고 싶다면 아래의 CSI->SGR 로 설정을 하면된다.
이외에도 다양한 기능이 있으니, 아래의 표를 보고 알아두자.
Sequence | C1 | Short | Name | Effect |
---|---|---|---|---|
ESC N | 0x8E | SS2 | Single Shift Two | Select a single character from one of the alternative character sets. In xterm, SS2 selects the G2 character set, and SS3 selects the G3 character set.[19] |
ESC O | 0x8F | SS3 | Single Shift Three | |
ESC P | 0x90 | DCS | Device Control String | Terminated by ST. Xterm's uses of this sequence include defining User-Defined Keys, and requesting or setting Termcap/Terminfo data.[19] |
ESC [ | 0x9B | CSI | Control Sequence Introducer | Most of the useful sequences, see next section. |
ESC \ | 0x9C | ST | String Terminator | Terminates strings in other controls.[18]:8.3.143 |
ESC ] | 0x9D | OSC | Operating System Command | Starts a control string for the operating system to use, terminated by ST.[18]:8.3.89 In xterm, they may also be terminated by BEL.[19] In xterm, the window title can be set by OSC 0;this is the window title BEL . |
ESC X | 0x98 | SOS | Start of String | Takes an argument of a string of text, terminated by ST. The uses for these string control sequences are defined by the application[18]:8.3.2,8.3.128 or privacy discipline.[18]:8.3.94 These functions are not implemented and the arguments are ignored by xterm.[19] |
ESC ^ | 0x9E | PM | Privacy Message | |
ESC _ | 0x9F | APC | Application Program Command | |
ESC c | RIS | Reset to Initial State | Resets the device to its original state. This may include (if applicable): reset graphic rendition, clear tabulation stops, reset to default font, and more.[20] |
- CSI sequences
세부사항은 우측 Effect 부분을 참조
Code | Short | Name | Effect |
---|---|---|---|
CSI n A | CUU | Cursor Up | Moves the cursor n (default 1 ) cells in the given direction.If the cursor is already at the edge of the screen, this has no effect. |
CSI n B | CUD | Cursor Down | |
CSI n C | CUF | Cursor Forward | |
CSI n D | CUB | Cursor Back | |
CSI n E | CNL | Cursor Next Line | Moves cursor to beginning of the line n (default 1 ) lines down. (not ANSI.SYS) |
CSI n F | CPL | Cursor Previous Line | Moves cursor to beginning of the line n (default 1 ) lines up. (not ANSI.SYS) |
CSI n G | CHA | Cursor Horizontal Absolute | Moves the cursor to column n (default 1 ). (not ANSI.SYS) |
CSI n ; m H | CUP | Cursor Position | Moves the cursor to row n, column m. The values are 1-based, and default to 1 (top left corner) if omitted.A sequence such as CSI ;5H is a synonym for CSI 1;5H as well as CSI 17;H is the same as CSI 17H and CSI 17;1H |
CSI n J | ED | Erase in Display | Clears part of the screen. If n is 0 (or missing), clear from cursor to end of screen.If n is 1 , clear from cursor to beginning of the screen.If n is 2 , clear entire screen (and moves cursor to upper left on DOS ANSI.SYS).If n is 3 , clear entire screen and delete all lines saved in the scrollback buffer(this feature was added for xterm and is supported by other terminal applications). |
CSI n K | EL | Erase in Line | Erases part of the line. If n is 0 (or missing), clear from cursor to the end of the line.If n is 1 , clear from cursor to beginning of the line.If n is 2 , clear entire line. Cursor position does not change. |
CSI n S | SU | Scroll Up | Scroll whole page up by n (default 1 ) lines.New lines are added at the bottom. (not ANSI.SYS) |
CSI n T | SD | Scroll Down | Scroll whole page down by n (default 1 ) lines.New lines are added at the top. (not ANSI.SYS) |
CSI n ; m f | HVP | Horizontal Vertical Position | Same as CUP, but counts as a format effector function (like CR or LF) rather than an editor function (like CUD or CNL).[1]:AppA.1 This can lead to different handling in certain terminal modes.[18]:AnnexA |
CSI n m | SGR | Select Graphic Rendition | Sets the appearance of the following characters, see SGR parameters below. |
CSI 5i | AUX Port On | Enable aux serial port usually for local serial printer | |
CSI 4i | AUX Port Off | Disable aux serial port usually for local serial printer |
|
CSI 6n | DSR | Device Status Report | Reports the cursor position (CPR) to the application as (as though typed at the keyboard) ESC[n;mR , where n is the row and m is the column.) |
CSI를 시작으로 상위 명령들을 간단히 실행을 해보자
- echo 기반으로 상위 적용 Simple 예제
$ man echo NAME echo - display a line of text SYNOPSIS echo [SHORT-OPTION]... [STRING]... echo LONG-OPTION DESCRIPTION Echo the STRING(s) to standard output. -n do not output the trailing newline -e enable interpretation of backslash escapes // backslash를 이용하여 ESC Code를 사용 -E disable interpretation of backslash escapes (default) If -e is in effect, the following sequences are recognized: // echo -e 했을 경우 backslash 사용하여 출력예제 \\ backslash \a alert (BEL) \b backspace \c produce no further output \e escape \f form feed \n new line \r carriage return \t horizontal tab \v vertical tab \0nnn the character whose ASCII code is NNN (octal). NNN can be 0 to 3 octal digits \xHH the eight-bit character whose value is HH (hexadecimal). HH can be one or two hex digits // TEST Normal $ echo -e "TEST " // TEST CSI n J ( Erase in Display ) $ echo -e "\x1b[1J TEST " // TEST CSI n;m H (Cursor Position ) $ echo -e "\x1b[1;10H TEST "
Linux command로 echo를 이용하여 색상 및 상위 명령어를 표현시에는 -e 옵션을 반드시 적용
1.2 Terminal의 색상 설정변경 (CSI->SGR)
Terminal의 색상 설정 및 Blink를 비롯하여, Bold / Underline등 다양한 설정이 가능하다.
색상또한 본인이 원하면, 다양하게 설정이 가능하다.
- SGR parameters (Set Graphics Rendition)
- CSI n m (ESC[ n m)로 동작되며, n값은 아래의 Code값
- Color Table은 3종류로 선택가능하며, 3/4bit , 8 bit , 24 bit 모드로 동작가능
Code | Effect | Note |
---|---|---|
0 | Reset / Normal | all attributes off |
1 | Bold or increased intensity | |
2 | Faint (decreased intensity) | |
3 | Italic | Not widely supported. Sometimes treated as inverse. |
4 | Underline | |
5 | Slow Blink | less than 150 per minute |
6 | Rapid Blink | MS-DOS ANSI.SYS; 150+ per minute; not widely supported |
7 | Reverse video | swap foreground and background colors |
8 | Conceal | Not widely supported. |
9 | Crossed-out | Characters legible, but marked for deletion. |
10 | Primary(default) font | |
11–19 | Alternative font | Select alternative font n − 10 |
20 | Fraktur | Rarely supported |
21 | Doubly underline or Bold off | Double-underline per ECMA-48.[26] See discussion |
22 | Normal color or intensity | Neither bold nor faint |
23 | Not italic, not Fraktur | |
24 | Underline off | Not singly or doubly underlined |
25 | Blink off | |
27 | Inverse off | |
28 | Reveal | conceal off |
29 | Not crossed out | |
30–37 | Set foreground color | See color table below 3/4bit color |
38 | Set foreground color | Next arguments are 5;n or 2;r;g;b , see below |
39 | Default foreground color | implementation defined (according to standard) |
40–47 | Set background color | See color table below 3/4bit color |
48 | Set background color | Next arguments are 5;n or 2;r;g;b , see below |
49 | Default background color | implementation defined (according to standard) |
51 | Framed | |
52 | Encircled | |
53 | Overlined | |
54 | Not framed or encircled | |
55 | Not overlined | |
60 | ideogram underline or right side line | Rarely supported |
61 | ideogram double underline or double line on the right side | |
62 | ideogram overline or left side line | |
63 | ideogram double overline or double line on the left side | |
64 | ideogram stress marking | |
65 | ideogram attributes off | reset the effects of all of 60 –64 |
90–97 | Set bright foreground color | aixterm (not in standard) 3/4bit |
100–107 | Set bright background color | aixterm (not in standard) 3/4bit |
현재 내 Terminal은 Blink가 미동작하며, 각 Terminal마다 동작 사항은 다를 것 같다.
주로 많이 사용하는 모드는 3/4bit의 color 모드로 보면 가장 심플하게 적용하자.
- 3/4 bit color 설정 및 Bold TEST
- \x1b[1m : Bold
- \x1b[93m : Bright Yellow (3/4bit)
- \x1b[0m : Reset 을 할 경우 상위 설정된 값들이 모두 초기화
$ echo -e "\x1b[1m \x1b[93m TEST \x1b[0m"
- 3/4 bit color ";"를 사용하여 SGR를 동시적용 TEST
- \x1b[1;93;4m : Bold/Bright Yellow (3/4bit) /Underline
- \x1b[0m : Reset
//Linux 에서 동시적용시 $ echo -e "\x1b[1;93;4;6m TEST \x1b[0m" // Hex ESC 1: Bold , 93: Bright Yellow , 4: Underline 6: Rapid Blink $ echo -e "\033[1;93;4;6m TEST \033[0m" // Oct ESC 상위동일 //Bright foreground color (3/4 bit) $ echo -e "\x1b[1;90m TEST \x1b[0m" // Hex ESC 1: Bold , 90: Bright Black (Gray) $ echo -e "\x1b[1;91m TEST \x1b[0m" // Hex ESC 1: Bold , 91: Bright Red $ echo -e "\x1b[1;92m TEST \x1b[0m" // Hex ESC 1: Bold , 92: Bright Green $ echo -e "\x1b[1;93m TEST \x1b[0m" // Hex ESC 1: Bold , 93: Bright Yellow $ echo -e "\x1b[1;94m TEST \x1b[0m" // Hex ESC 1: Bold , 94: Bright Blue $ echo -e "\x1b[1;95m TEST \x1b[0m" // Hex ESC 1: Bold , 95: Bright Magenta $ echo -e "\x1b[1;96m TEST \x1b[0m" // Hex ESC 1: Bold , 96: Bright Cyan $ echo -e "\x1b[1;97m TEST \x1b[0m" // Hex ESC 1: Bold , 97: Bright Whight //Foreground color (3/4 bit) , $ echo -e "\x1b[1;30m TEST \x1b[0m" // Hex ESC 1: Bold , 30: Black $ echo -e "\x1b[1;31m TEST \x1b[0m" // Hex ESC 1: Bold , 31: Red $ echo -e "\x1b[1;32m TEST \x1b[0m" // Hex ESC 1: Bold , 32: Green $ echo -e "\x1b[1;33m TEST \x1b[0m" // Hex ESC 1: Bold , 33: Yellow $ echo -e "\x1b[1;34m TEST \x1b[0m" // Hex ESC 1: Bold , 34: Blue $ echo -e "\x1b[1;35m TEST \x1b[0m" // Hex ESC 1: Bold , 35: Magenta $ echo -e "\x1b[1;36m TEST \x1b[0m" // Hex ESC 1: Bold , 36: Cyan $ echo -e "\x1b[1;37m TEST \x1b[0m" // Hex ESC 1: Bold , 37: Whight
//Bell Sound,
$ echo -e '\007'
$ echo -e '\a'
https://rosettacode.org/wiki/Terminal_control/Ringing_the_terminal_bell
//Window Shell 인 CMD에서 실행 "\x"or "\033" 대신 Ctrl+[ 눌르면, "^[" 생성 > echo ^[[1;93;4;6m TEST ^[[0m // Hex ESC 1: Bold , 93: Bright Yellow , 4: Underline 6: Rapid Blinkhttps://stackoverflow.com/questions/2048509/how-to-echo-with-different-colors-in-the-windows-command-line
//Batch파일생성시 ESC Key 생성문제 때문에 아래와 같이 setESC사용
setlocal
call :setESC
echo %ESC%[1;31m
ECHO -----------------------------
ECHO ------ COLOR TEST
ECHO -----------------------------
echo %ESC%[0m
echo %ESC%[101;93m STYLES %ESC%[0m
echo %ESC%[0mReset%ESC%[0m
echo %ESC%[1mBold%ESC%[0m
echo %ESC%[4mUnderline%ESC%[0m
echo %ESC%[7mInverse%ESC%[0m
echo.
echo %ESC%[101;93m NORMAL FOREGROUND COLORS %ESC%[0m
echo %ESC%[30mBlack%ESC%[0m (black)
echo %ESC%[31mRed%ESC%[0m
echo %ESC%[32mGreen%ESC%[0m
echo %ESC%[33mYellow%ESC%[0m
echo %ESC%[34mBlue%ESC%[0m
echo %ESC%[35mMagenta%ESC%[0m
echo %ESC%[36mCyan%ESC%[0m
echo %ESC%[37mWhite%ESC%[0m
echo.
echo %ESC%[101;93m NORMAL BACKGROUND COLORS %ESC%[0m
echo %ESC%[40mBlack%ESC%[0m
echo %ESC%[41mRed%ESC%[0m
echo %ESC%[42mGreen%ESC%[0m
echo %ESC%[43mYellow%ESC%[0m
echo %ESC%[44mBlue%ESC%[0m
echo %ESC%[45mMagenta%ESC%[0m
echo %ESC%[46mCyan%ESC%[0m
echo %ESC%[47mWhite%ESC%[0m
echo.
PAUSE
:setESC
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set ESC=%%b
exit /B 0
)
exit /B 0
https://gist.github.com/mlocati/fdabcaeb8071d5c75a2d51712db24011
ESC[ 48;5;⟨n⟩ m Select background color (n : 0~255)
ESC[ 48;2;⟨r⟩;⟨g⟩;⟨b⟩ m Select RGB background color
간단히 아래와 같이 Shell scirpt로 작성하여 출력
http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html
- 8bit Color 적용방법
ESC[ 48;5;⟨n⟩ m Select background color (n : 0~255)
- 24bit Color 적용방법 (24bit까지 필요성은 현재 없어보임)
ESC[ 48;2;⟨r⟩;⟨g⟩;⟨b⟩ m Select RGB background color
간단히 아래와 같이 Shell scirpt로 작성하여 출력
- 8bit TEST Color Shell Script
$ vi 8bitTestColor.sh #!/bin/bash FOR=0 BAK=255 for((FOR=0,BAK=255;FOR<255;FOR++,BAK--)); do echo -e "\x1b[38:5:${FOR}m \x1b[48:5:${BAK}m TEST \x1b[0m" done $ chmod +x ./8bitTestColor.sh $ ./8bitTestColor.sh
- Python을 이용한 다양한 Color 설정
http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html
- C언어 적용 및 TEST
$ vi color_test.c #include <stdio.h> #define RST "\x1B[0m" // Normal Reset #define BLD "\x1B[1m" // Bold #define BLK "\x1B[30m" // Black #define RED "\x1B[31m" // Red #define GRN "\x1B[32m" // Green #define YEL "\x1B[33m" // Yellow #define BLU "\x1B[34m" // Blue #define MAG "\x1B[35m" // Magenta #define CYN "\x1B[36m" // Cyan #define WHT "\x1B[37m" // Light Gray #define GRA "\x1B[90m" // Dark Gray #define LRD "\x1B[91m" // Light RED #define LGR "\x1B[92m" // Light Green #define LYL "\x1B[93m" // Light Yellow #define LBL "\x1B[94m" // Light Blue #define LMA "\x1B[95m" // Light Magenta #define LCY "\x1B[96m" // Light Cyan #define LWH "\x1B[97m" // Whight int main() { //Color printf("%s %s TEST MY Color %s \n", BLD,RED,RST); //Bell sound printf("\a"); }
댓글 없음 :
댓글 쓰기