INF: How to Use ANSI.SYS Escape Sequences from a Program

ID Number: Q34020

2.00 2.10 2.11 3.00 3.10 3.20 3.22 3.30 3.30a 4.00 4.01 5.0

MS-DOS

Summary:

Below is a C source file that includes macros for some of the ANSI

escape sequences described in the "Microsoft MS-DOS User's Reference."

These macros make it easy to position the cursor, clear text from the

screen, and change text color. To use these macros, the MS-DOS device

driver ANSI.SYS must be installed in the CONFIG.SYS file.

More Information:

The C source code below is to be used as an include file in a

program that will make these ANSI calls. If the symbol TEST_MAIN is

defined, a sample test program will be activated.

/* ----------------------------------------------------------------------- */

/* Sample C language macros for using ANSI.SYS. */

/* Uncomment the next line to enable a test program. */

#define TEST_MAIN

/* cursor positioning */

#define ANSI_CUP(row,col) printf("\x1B[%d;%dH",row,col) /* cursor position */

#define ANSI_CUU(rows) printf("\x1B[%dA",rows) /* cursor up */

#define ANSI_CUD(rows) printf("\x1B[%dB",rows) /* cursor down */

#define ANSI_CUF(col) printf("\x1B[%dC",col) /* cursor forward */

#define ANSI_CUB(col) printf("\x1B[%dD",col) /* cursor back */

#define ANSI_SCP() printf("\x1B[s") /* save cursor position */

#define ANSI_RCP() printf("\x1B[u") /* restore cursor position */

#define ANSI_ED() printf("\x1B[2J") /* erase display */

#define ANSI_EL() printf("\x1B[K") /* erase to end of line */

/* character attribute */

#define ANSI_INVERSE_ON() printf("\x1B[7m")

#define ANSI_BLINK_ON() printf("\x1B[5m")

#define ANSI_BOLD_ON() printf("\x1B[1m")

#define ANSI_CONCEALED_ON() printf("\x1B[8m")

#define ANSI_INVERSE_OFF() printf("\x1B[0m") /* all attributes off */

#define ANSI_BLINK_OFF() printf("\x1B[0m") /* all attributes off */

#define ANSI_BOLD_OFF() printf("\x1B[0m") /* all attributes off */

#define ANSI_CONCEALED_OFF() printf("\x1B[0m") /* all attributes off */

#define ANSI_ALL_ATTRIB_OFF() printf("\x1B[0m") /* all attributes off */

/* color */

#define ANSI_BLUE_BACK_ON() printf("\x1B[44m")

#define ANSI_RED_BACK_ON() printf("\x1B[41m")

#define ANSI_YELLOW_BACK_ON() printf("\x1B[41m")

#define ANSI_YELLOW_FORE_ON() printf("\x1B[33m")

#define ANSI_BLUE_BACK_OFF() printf("\x1B[0m") /* all attributes off */

#define ANSI_RED_BACK_OFF() printf("\x1B[0m") /* all attributes off */

#define ANSI_YELLOW_BACK_OFF() printf("\x1B[0m") /* all attributes off */

#define ANSI_YELLOW_FORE_OFF() printf("\x1B[0m") /* all attributes off */

/* For some monitors, you might have to turn on bold before turning on

yellow in order to get a yellow color. Otherwise, you may get a tan

or reddish color. */

#ifdef TEST_MAIN

/* Test program to demonstrate the use of these ANSI macros. */

#include <stdio.h>

int main(void);

int main()

{

int row = 10, col = 0;

ANSI_ED();

ANSI_CUP(row, col);

ANSI_BOLD_ON();

puts("Bold is on");

ANSI_BOLD_OFF();

ANSI_BLINK_ON();

puts("Blink is on");

ANSI_BLINK_OFF();

ANSI_BLUE_BACK_ON();

puts("Blue background on");

ANSI_BLUE_BACK_OFF();

ANSI_RED_BACK_ON();

puts("Red background on");

ANSI_RED_BACK_OFF();

ANSI_BOLD_ON();

ANSI_YELLOW_FORE_ON();

puts("Yellow foreground on");

ANSI_YELLOW_FORE_OFF();

return(0);

} /* main */

#endif /* TEST_MAIN */

/* ----------------------------------------------------------------------- */