Home | News (Page 1) | Star Dot Star

Using ANSI Escape Sequences

Adding color, control and terminal features to ASCII text.

by Randy Harris office@lexipixel.com

April 10, 2004  (12:53:21 EST)

An ANSI escape sequence is a series of characters (beginning with an escape character or keystroke) that you can use to define functions to MS-DOS. Specifically, you can reassign keys, change graphic functions, and affect cursor movement.

This short help file will explain how the ANSI escape sequences are defined for MS-DOS. Examples of how to use ANSI escape sequences are inlcuded at the end of this help file.

NOTE:
----
1. MS-DOS uses a default value when you do not specify a value or when you specify zero.

2. Pn represents "numeric parameter." This is not a decimal number specified with ASCII digits.

3. Ps respresents "selective parameter." This is any decimal number that is used to select a subfunction. Multiple subfunctions may be selected by seperating the parameters with semicolons.

4. Pl represents "line number." This is a decimal number specified with ASCII digits.

5. Pc respresents "column parameter." This is a decimal number specified with ASCII digits.


CURSOR FUNCTIONS:



The following escape sequences affect the cursor position on the screen.


CUP - Cursor Position

ESC [ Pl ; Pc H


HVP - Horizontal and Vertical Position

ESC [ Pl ; Pc f



CUP and HVP move the cursor to the position specifically by the paramters. The first parameter specifies the line number, and the second parameter specifies the column number. The default value is one, (1). When no parameter are specified, the cursor moves to the home position.



CUU - Cursor Up

ESC [ Pn A



This sequence moves the cursor up one line without changing columns. The value Pn sets the number of lines moved. The default value for Pn is 1. MS-DOS ingnores the CUU sequence if the cursor is already on the top line.


CUD - Cursor Down

ESC [ Pn B



This sequence moves the cursor down one line without changing columns. The value of Pn sets the number of lines moved. The default value for Pn is 1. MS-DOS ignores the CUD sequence if the cursor is already on the bottom line.


CUF - Cursor Forward

ESC [ Pn C



The CUF sequence moves the cursor forward one column without changing lines. The value of Pn sets the number of columns moved. The default value for Pn is 1. MS-DOS ignores the CUF sequence if the cursor is already in the far right hand column.


CUB - Cursor Backwards

ESC [ Pn D



This escape sequence moves the cursor back one column without changing lines. The value of Pn sets the number of columns moved. The default value for Pn is 1. MS-DOS ignores the CUB sequence if the cursor is already in the far right column.


DSR - Device Status Report

ESC [ 6 n



The console driver will output a CPR sequence on receipt of the DSR escape sequence.


CPR - Cursor Position Report
(from console driver to system)

ESC [ Pn ; Pn R


The CPR sequence reports current cursor position using standard input. The first character specifies the current line and the second parameter specifies the current column.


SCP - Save Cursor Position

ESC [ s



The current cursor position is saved. This cursor position can be restored with the RCP sequence.


RCP - Restore Cursor Position

ESC [ u



This sequence restores the cursor position to the value it had when the console driver received the SCP sequence.


ERASING:



The following escape sequences affect erase functions.


ED - Erase Display

ESC [ 2 J



This ED sequence erases the screen, and the cursor goes to the home position.


EL - Erase Line

ESC [ K



This sequence erases from the cursor to the end of the line (including the cursor position).


MODES OF OPERATION:



The following escape sequences affect screen graphics.


SGR - Set Graphics Rendition

ESC [ Ps ; ... ; Ps m



This SGR escape sequence calls the graphic functions specified by the parameters described below. The graphic functions remain until the next occurance of an SGR escape sequence.


s Function
--- -----------------
0 All attibutes off
1 Bold ON
2 Faint ON
3 Italic ON
5 Blink ON
7 Reverse Video ON
8 Concealed ON
30 Black Foreground
31 Red Foreground
32 Green Foreground
33 Yellow Foreground
34 Blue Foreground
35 Magneta Foreground
36 Cyan Foreground
37 White Foreground
40 Black Background
41 Red Background
42 Green Background
43 Yellow Background
44 Blue Background
45 Magneta Background
46 Cyan Background
47 White Background
48 Subscript
49 Superscript



SM - Set Mode

ESC [ = Ps h

-or-

ESC [ = h

-or-

ESC [ = 0 h

-or-

ESC [ ? 7 h


The SM escape sequence changes the screen width or type to one of the following:


s Function
--- ------------------------
0 40 x 25 Black and White
1 40 x 25 Color
2 80 x 25 Black and White
3 80 x 25 Color
4 320 x 200 Color
5 320 x 200 Black and White
6 640 x 200 Black and White
7 Wrap at end of line



RM - Reset Mode

ESC [ = Ps 1

-or-

ESC [ = 1

-or-

ESC [ = 0 1

-or-

ESC [ ? 7 1


Parameters for RM are the same as for SM (Set mode), except that paramter 7 will reset the wrap at the end of line mode.


KEYBOARD REASSIGNMENT:



Although not part of the ANSI 3.64- 1979 or ISO 6429 standard, the following keyboard reassignments are compatible with these standards.

The control sequence is:

ESC [ Pn ; Pn ; ... Pn p -or-
ESC [ "string" ; p

ESC [ Pn ; "string" ; Pn ; Pn ; "string" ; Pn p
or any other combination of strings and decimal numbers.

The final code in the control sequence (p) is one reserved for private used by the ANSI 3.64- 1979 standard.

The first ASCII code in the control sequence defines which code is being mapped. The remaining numbers define the sequence of the ASCII codes generated when this key is intercepted. Note that there is one exception; if the first code in the sequence is zero (null), then the first and second code make up an extended ASCII redefinition.

Examples:

1. Reassign the Q and q key to the A and a key (and vice versa):

ESC [ 65 ; 81 p A becomes Q
ESC [ 97 ; 113 p a becomes q
ESC [ 81 ; 65 p Q becomes A
ESC [ 113 ; 97 p q becomes a

2. Reassign the F10 key to a DIR command followed by a carriage return:

ESC [ 0 ; 68 ; "DIR" ; 13 p

The 0;68 is the extended ASCII code for the F10 key; 13 decimal is a carriage return.




###





Copyright ©2004-2008 RMH, all rights reserved.
powered by LexiNews v.01.12, from LEXIPIXEL