Chinaunix首页 | 论坛 | 博客
  • 博客访问: 215418
  • 博文数量: 127
  • 博客积分: 1998
  • 博客等级: 上尉
  • 技术积分: 1432
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-07 16:41
文章分类

全部博文(127)

文章存档

2014年(41)

2013年(1)

2012年(85)

分类: C/C++

2012-04-09 20:55:59

From Wikipedia, the free encyclopedia

ANSI escape sequences are characters embedded in the text used to control formatting, color, and other output options on video text terminals. Almost all terminal emulators designed to show text output from a remote computer, and (except for Microsoft Windows) to show text output from local software, interpret at least some of the ANSI escape sequences.

Contents  [hide
[edit]History

Almost all manufacturers of video terminals added vendor-specific escape sequences to do operations such as placing the cursor at arbitrary positions on the screen. As these sequences were all different, elaborate libraries such as termcap had to be created so programs could use the same API for all of them. In addition, most designs required sending numbers (such as row & column) as the binary values of the characters; for some programming languages and for systems that did not use ASCII internally it was often difficult or impossible to turn a number into the correct character.

The first standard for ANSI escape sequences was ECMA-48, adopted in 1976. It was a continuation of a series of character coding standards, the first one being ECMA-6 from 1961, a 7-bit standard from which ASCII originates. ECMA-48 has been updated several times and the current edition is the 5th from 1991. It is also adopted by ISO and IEC as standard ISO/IEC 6429. The name "ANSI escape sequence" dates from 1981 when ANSI adopted ECMA-48 as the standard ANSI X3.64 (and later, in 1997, withdrew it).[1]

The first popular video terminal to support these sequences was the Digital VT100 introduced in 1978,[2] which sparked a variety of "clones", among the earliest and most popular of which was the much more affordable Zenith Z-19 in 1979.[3] The popularity of these gradually led to more and more software (especially bulletin board systems) assuming the escape sequences worked, leading to almost all new terminals and emulator programs supporting them.

[edit]Support

Most terminal emulators running on Unix-like systems (such as xterm and the OS X Terminal) interpret ANSI escape sequences. The Linux console (the text seen when X is not running) also interprets them. Terminal programs for Microsoft Windows designed to show text from an outside source (a serial port, modem, or socket) also interpret them. Some support for text from local programs on Windows is offered through alternate command processors such as JP Software's TCC (formerly 4NT), Michael J. Mefford's ANSI.COM, and Jason Hood'sansicon.

Many Unix console applications (e.g., lsgrepVim, and Emacs) can generate them. Utility programs such as tput output them, as well as in low-level programming libraries, such as termcap or terminfo, or a higher-level library such as curses.

[edit]Windows and DOS

MS-DOS 1.0 did not support the ANSI or any other escape sequences. Only a few control characters (CRLFBS) were interpreted, making it impossible to do any kind of full-screen application. Any display effects had to be done with BIOS calls (or far more often by directly manipulating the IBM PC hardware).

MS-DOS 2.0 introduced the ability to add a device driver for the ANSI escape sequences – the de facto standard being ANSI.SYS, but others like ANSI.COM,[4] and NANSI.SYS[5] are used as well. Extreme slowness and the fact that it was not installed by default made usage by software almost non-existent; software continued to directly manipulate the hardware to get the text display needed.[citation needed]ANSI.SYS and similar drivers continued to work in through Windows 98, and even in Windows Me, when they were set to native DOS mode (the characters taking the whole screen). ANSI.SYS also worked in NT-derived systems for 16-bit legacy programs executing under theNTVDM.

The Win32 console does not support ANSI escape sequences at all. Software can manipulate the console with the ioctl-like Console APIinterlaced with the text output. Some software internally interprets ANSI escape sequences in text being printing and translates them to these calls[citation needed].

[edit]Sequence elements

Escape sequences start with the character ESC (ASCII decimal 27/hex 0x1B/octal 033). For two character sequences, the second character is in the range ASCII 64 to 95 (@ to _). Most of the sequences are however more than two characters, and start with the characters ESCand [ (left bracket). This sequence is called CSI for Control Sequence Introducer (or Control Sequence Initiator). The final character of these sequences is in the range ASCII 64 to 126 (@ to ~).

There is a single-character CSI (155/0x9B/0233) as well. The ESC+[ two-character sequence is more often used than the single-character alternative, for details see C0 and C1 control codes. Only the two-character sequence is recognized by devices that support just ASCII (7-bit bytes) or devices that support 8-bit bytes but use the 0x80–0x9F control character range for other purposes. On terminals that use UTF-8encoding, both forms take 2 bytes,[citation needed] but the two-character sequence is clearer.

Though some encodings use multiple bytes per character, the following discussion is restricted to ASCII characters, and so assumes each character is directly represented by a single byte.

[edit]Non-CSI codes

Note: other C0 codes besides ESC — commonly BEL, BS, CR, LF, FF, TAB, VT, SO, and SI — may produce similar or identical effects to some control sequences when output.

ESC N = SS2, ESC O = SS3 — select a single character from one of the alternate character sets.

ESC ^ = PM, ESC _ = APC — these each take a single string of text, terminated by ST (ESC \ ). They are ignored by xterm.

ESC P = DCS: Device control string, ESC ] = OSC: Operating system command — these are similar to CSI, but not limited to integer arguments. Because they are frequently used,[citation needed] in many cases BEL is an acceptable alternative to ST. E.g., in xterm, the window title can be set by: "OSC0;this is the window titleBEL"

Note: pressing special keys on the keyboard, as well as outputting many xterm CSI, DCS, or OSC sequences, often produces a CSI, DCS, or OSC sequence.

[edit]CSI codes

The general structure of most ANSI escape sequences is CSI [private mode characters(s?)n1 ; n2... [trailing intermediate character(s?)letter. The final byte, modified by private mode characters and trailing intermediate characters, specifies the command. The numbers are optional parameters. The default value used for omitted parameters varies with the command, but is usually 1 or 0. If trailing parameters are omitted, the trailing semicolons may also be omitted.

The final byte is technically any character in the range 64 to 126 (hex 0x40 to 0x7e, ASCII @ to ~), and may be modified extended with leading intermediate bytes in the range 32 to 47 (hex 0x20 to 0x2f).

The colon (0x3a) is the only character not a part of the general sequence; it was left for future standardization, so any sequence containing it should be ignored. Although multiple private mode characters or trailing intermediates are permitted, there are no such known usages.[citation needed]

If there are any leading private mode characters, the main body of the sequence could theoretically contain any order of characters 0x30 – 0x3f instead of a well-formed semicolon-separated list of numbers,[citation needed] but all known terminals are nice and just use them as a flag.[citation needed] Sequences are also private if the final byte is in the range 112 to 126 (hex 0x70 to 0x7e, ASCII p to ~).

Examples of private escape codes include the DECTCEM (DEC text cursor enable mode) shown below. It was first introduced for the VT-300 series of video terminals.

The existence of a C0 control, DEL (0x7f), or a high characters is undefined.[clarification needed] Typically, implementations will either cancel the sequence or execute the control and then continue parsing the CSI sequence.[clarification needed]

Some ANSI escape sequences (not a complete list)
CodeNameEffect
CSI n ACUU – CUrsor UpMoves 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 BCUD – CUrsor Down
CSI n CCUF – CUrsor Forward
CSI n DCUB – CUrsor Back
CSI n ECNL – Cursor Next LineMoves cursor to beginning of the line n (default 1) lines down.
CSI n FCPL – Cursor Previous LineMoves cursor to beginning of the line n (default 1) lines up.
CSI n GCHA – Cursor Horizontal AbsoluteMoves the cursor to column n.
CSI n ; m HCUP – CUrsor PositionMoves 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 JED – Erase DataClears part of the screen. If n is zero (or missing), clear from cursor to end of screen. If n is one, clear from cursor to beginning of the screen. If n is two, clear entire screen (and moves cursor to upper left on MS-DOS ANSI.SYS).
CSI n KEL – Erase in LineErases part of the line. If n is zero (or missing), clear from cursor to the end of the line. If n is one, clear from cursor to beginning of the line. If n is two, clear entire line. Cursor position does not change.
CSI n SSU – Scroll UpScroll whole page up by n (default 1) lines. New lines are added at the bottom. (not ANSI.SYS)
CSI n TSD – Scroll DownScroll whole page down by n (default 1) lines. New lines are added at the top. (not ANSI.SYS)
CSI n ; m fHVP – Horizontal and Vertical PositionMoves the cursor to row n, column m. Both default to 1 if omitted. Same as CUP
CSI n [;k] mSGR – Select Graphic RenditionSets SGR parameters, including text color. After CSI can be zero or more parameters separated with ;. With no parameters, CSI m is treated as CSI 0 m (reset / normal), which is typical of most of the ANSI escape sequences.
CSI 6 nDSR – Device Status ReportReports the cursor position to the application as (as though typed at the keyboard) ESC[n;mR, where n is the row and m is the column. (May not work on MS-DOS.)
CSI sSCP – Save Cursor PositionSaves the cursor position.
CSI uRCP – Restore Cursor PositionRestores the cursor position.
CSI ?25lDECTCEMHides the cursor. (Note: the trailing character is lowercase L.)
CSI ?25hDECTCEMShows the cursor.
SGR (Select Graphic Rendition) parameters
CodeEffectNote
0Reset / Normalall attributes off
1Bright (increased intensity) or Bold
2Faint (decreased intensity)not widely supported
3Italic: onnot widely supported. Sometimes treated as inverse.
4Underline: Single
5Blink: Slowless than 150 per minute
6Blink: RapidMS-DOS ANSI.SYS; 150 per minute or more; not widely supported
7Image: Negativeinverse or reverse; swap foreground and background
8Concealnot widely supported
9Crossed-outCharacters legible, but marked for deletion. Not widely supported.
10Primary(default) font
11–19n-th alternate fontSelect the n-th alternate font. 14 being the fourth alternate font, up to 19 being the 9th alternate font.
20Frakturhardly ever supported
21Bright/Bold: off or Underline: Doublebold off not widely supported, double underline hardly ever
22Normal color or intensityneither bright, bold nor faint
23Not italic, not Fraktur
24Underline: Nonenot singly or doubly underlined
25Blink: off
26Reserved
27Image: Positive
28Revealconceal off
29Not crossed out
30–37Set text color30 + x, where x is from the color table below
38Set xterm-256 text color[dubious ]next arguments are 5;x where x is color index (0..255)
39Default text colorimplementation defined (according to standard)
40–47Set background color40 + x, where x is from the color table below
48Set xterm-256 background colornext arguments are 5;x where x is color index (0..255)
49Default background colorimplementation defined (according to standard)
50Reserved
51Framed
52Encircled
53Overlined
54Not framed or encircled
55Not overlined
56–59Reserved
60ideogram underline or right side linehardly ever supported
61ideogram double underline or double line on the right sidehardly ever supported
62ideogram overline or left side linehardly ever supported
63ideogram double overline or double line on the left sidehardly ever supported
64ideogram stress markinghardly ever supported
90–99Set foreground color, high intensityaixterm (not in standard)
100–109Set background color, high intensityaixterm (not in standard)
[edit]Colors

Text color is manipulated using CSI n [;k] m sequence.

Color table[6]
Intensity01234567
NormalBlackRedGreenYellow[7]BlueMagentaCyanWhite
BrightBlackRedGreenYellowBlueMagentaCyanWhite

There are two other color standards CSS/HTML standard colors and X Window colors which standardize both the color names and associated RGB color values, but the escape sequence standard only specifies the color names, not RGB values. The chart below shows default RGB assignments for some common terminal programs, together with the CSS and the X-Window colors for these color names.[citation needed]

Standard VGA colorsWindows XP
command prompt
Terminal.appPuTTYxtermCSS/HTMLX Window
normalblack0, 0, 00, 0, 00, 0, 00, 0, 00, 0, 00, 0, 00, 0, 0
red170, 0, 0128, 0, 0194, 54, 33187, 0, 0205, 0, 0255, 0, 0255, 0, 0
green0, 170, 00, 128, 037, 188, 360, 187, 00, 205, 00, 255, 00, 128, 0
brown/yellow170, 85, 0128, 128, 0173, 173, 39187, 187, 0205, 205, 0255, 255, 0255, 255, 0
blue0, 0, 1700, 0, 12873, 46, 2250, 0, 1870, 0, 2380, 0, 2550, 0, 255
magenta170, 0, 170128, 0, 128211, 56, 211187, 0, 187205, 0, 205255, 0, 255255, 0, 255
cyan0, 170, 1700, 128, 12851, 187, 2000, 187, 1870, 205, 2050, 255, 2550, 255, 255
gray170, 170, 170192, 192, 192203, 204, 205187, 187, 187229, 229, 229255, 255, 255255, 255, 255
bright/lightdarkgray85, 85, 85128, 128, 128129, 131, 13185, 85, 85127, 127, 127
red255, 85, 85255, 0, 0252,57,31255, 85, 85255, 0, 0
green85, 255, 850, 255, 049, 231, 3485, 255, 850, 255, 0144, 238, 144144, 238, 144
yellow255, 255, 85255, 255, 0234, 236, 35255, 255, 85255, 255, 0255, 255, 224225, 255, 224
blue85, 85, 2550, 0, 25588, 51, 25585, 85, 25592, 92, 255173, 216, 230173, 216, 230
magenta255, 85, 255255, 0, 255249, 53, 248255, 85, 255255, 0, 255
cyan85, 255, 2550, 255, 25520, 240, 24085, 255, 2550, 255, 255224, 255, 255224, 255, 255
white255, 255, 255255, 255, 255233, 235, 235255, 255, 255255, 255, 255

The VGA column denotes the typical colors that are used when booting PCs and leaving them in their classical 80×25 text mode. The colors are different in the EGA/VGA graphic modes.

In July 2004, the blue colors of xterm changed,[8] RGB (0,0,205) --> (0,0,238) for normal and (0,0,255) --> (92,92,255) for bright. As of 2010 old xterm versions still linger on many computers though.

[edit]Examples

CSI 2 J — This clears the screen and, on some devices, locates the cursor to the y,x position 1,1 (upper left corner).

CSI 32 m — This makes text green. On MS-DOS, normally the green would be dark, dull green, so you may wish to enable Bold with the sequence CSI 1 m which would make it bright green, or combined as CSI 32 ; 1 m. MS-DOS ANSI.SYS uses the Bold state to make the character Bright; also the Blink state can be set (via INT 10, AX 1003h, BL 00h) to render the Background in the Bright mode. MS-DOS ANSI.SYS does not support SGR codes 90–97 and 100–107 directly.

CSI 0 ; 6 8 ; "DIR" ; 13 p — This re-assigns the key F10 to send to the keyboard buffer the string "DIR" and ENTER, which in the DOS command line would display the contents of the current directory. (MS-DOS ANSI.SYS only) This is a private-use code (as indicated by the letter p), using a non-standard extension to include a string-valued parameter. Following the letter of the standard would consider the sequence to end at the letter D.

CSI s — This saves the cursor position. Using the sequence CSI u will restore it to the position. Say the current cursor position is 7(y) and 10(x). The sequence CSI s will save those two numbers. Now you can move to a different cursor position, such as 20(y) and 3(x), using the sequence CSI 20 ; 3 H or CSI 20 ; 3 f. Now if you use the sequence CSI u the cursor position will return to 7(y) and 10(x). Some terminals require the DEC sequences ESC 7 / ESC 8 instead which is more widely supported.

[edit]Example of use in shell scripting

ANSI escape codes are often used in UNIX and UNIX-like terminals to provide syntax highlighting. For example, on compatible terminals, the following list command color-codes file and directory names by type.

ls --color

Users can employ escape codes in their scripts by including them as part of standard output or standard error. For example, the followingsed command embellishes the output of the make command by displaying lines containing words starting with "ERR" in reverse video, and words starting with "WARN" in bold (letter case is ignored).

make 2>&1 | sed -e 's/.*\bERR.*/\x1b[7m&\x1b[0m/i' -e 's/.*\bWARN.*/\x1b[1m&\x1b[0m/i'

The representations of the codes are highlighted.[9]

[edit]Illegal and ambiguous sequences in use
  • The Linux console uses OSC P n rr gg bb to change the palette, which, if hard-coded into an application, may hang other terminals. However, appending ST will be ignored by Linux and form a proper, ignorable sequence for other terminals.
  • On the Linux console, certain function keys generate sequences of the form CSI [ char. The CSI sequence should terminate on the [.
  • Old versions of Terminator generates SS3 1; modifiers char when F1–F4 are pressed with modifiers. The faulty behavior was copied from GNOME Terminal.[citation needed]
  • xterm replies CSI row ; column R if asked for cursor position and CSI 1 ; modifiers R if the F3 key is pressed with modifiers, which collide in the case of row == 1. This can be avoided by using the ? private modifier, which will be reflected in the response.[clarification needed]
  • many terminals prepend ESC to any character that is typed with the alt key down. This creates ambiguity for uppercase letters and symbols @[\]^_, which would form C1 codes.[clarification needed]
  • Konsole generates SS3 modifiers char when F1–F4 are pressed with modifiers.[clarification needed]
[edit]See also[edit]Notes
  1. ^ See this NIST list of withdrawn standards
  2. ^ Paul Williams (2006). "Digital's Video Terminals". VT100.net. Retrieved August 17, 2011.
  3. ^ Heathit Company (1979). "Heathkit Catalog 1979". Heathit Company. Retrieved 4 November 2011.
  4. ^ Michael Mefford (1989-02-07). "ANSI.com: Download It Here". PC Magazine. Retrieved 2011-08-10.
  5. ^ Dan Kegel, Eric Auer (1999-02-28). "Nansi and NNansi - ANSI Drivers for MS-DOS". Dan Kegel's Web Hostel. Retrieved 2011-08-10.
  6. ^ The names are standard, however the exact shade/hue/value of colors are not standardized and will depend on the device used to display them.
  7. ^ On terminals based on CGA compatible hardware, such as ANSI.SYS running on DOS, this normal intensity foreground color is rendered as Orange. CGA RGBI monitors contained hardware to modify the dark yellow color to an orange/brown color by reducing the green component. See this ansi art as an example.
  8. ^ "Patch #192 - 2004/7/12 - XFree86 4.4.99.9".
  9. ^ Colorized shell echo
[edit]External links===============================================================================
image.logo.gifANSI color codes

Pueblo/UE supports a wide range of ANSI color codes. This is a list of those codes currently supported by Pueblo/UE, together with which client version they were introduced in. If the version is shown as "--", then it was introduced in version 2.01 or earlier (exact information is not available).

 

An ANSI code is sent by the world by sending an escape character (ESC, ASCII value 27) followed by the appropriate code sequence:

Code:

Client:

Meaning:

[0m

--

reset; clears all colors and styles (to white on black)

[1m

--

bold on (see below)

[3m

--

italics on

[4m

--

underline on

[7m

2.50

inverse on; reverses foreground & background colors

[9m

2.50

strikethrough on

[22m

2.50

bold off (see below)

[23m

2.50

italics off

[24m

2.50

underline off

[27m

2.50

inverse off

[29m

2.50

strikethrough off

[30m

--

set foreground color to black

[31m

--

set foreground color to red

[32m

--

set foreground color to green

[33m

--

set foreground color to yellow

[34m

--

set foreground color to blue

[35m

--

set foreground color to magenta (purple)

[36m

--

set foreground color to cyan

[37m

--

set foreground color to white

[39m

2.53

set foreground color to default (white)

[40m

--

set background color to black

[41m

--

set background color to red

[42m

--

set background color to green

[43m

--

set background color to yellow

[44m

--

set background color to blue

[45m

--

set background color to magenta (purple)

[46m

--

set background color to cyan

[47m

--

set background color to white

[49m

2.53

set background color to default (black)

 

The bold codes have been given two different interpretations:

image.arrowyel.gif In client version 2.01 and earlier, bold was interpreted as the physical bold style – equivalent to the HTML  tag. In addition, the code [2m was mistakenly interpreted as bold off and the correct code was not handled.

image.arrowyel.gif In version 2.50 and later, bold is interpreted as ‘high-intensity’, which was the original ANSI definition. (High-intensity colors are brighter than low-intensity ones, which are the default.) If you want the bold font style, you will have to use the  tag instead. The incorrect [2m code for bold off is supported for backwards compatibility, along with the correct [22m code.

 

In addition, the ASCII BEL character (value 7) is internally translated to the HTML sequence . This will cause an audible alert sound, if configured to do so in the Preferences. While this is not actually an ANSI code sequence, worlds usually group this with them.

 

Also see

阅读(1017) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~