Based on Nominal Animals posts, I've managed to put together some cheesey macros for ANSI control sequences. They're a bit rough. I plan on spending more time with it and fleshing them out a bit. But here's a first draft for anyone that might find them useful.
NOTE: I borrowed the listing of ANSI Color codes from a post by Nominal Animal. He should get most of the credit for the codes in this header I've created.
Code:
#ifndef __ANSI_H__
#define __ANSI_H__
/*
* Some ansi control character sequence macros by Twiki
*
* CREDITS: Special thanks to Nominal Animal and Wikipedia for explaining ANSI control codes
* and a special thanks again to Nominal Animal, without whom this collection of ANSI macros
* would not be possible.
*
* DATE: Thursday, November 15th 2012
* LICENSE: Public Domain (MMXII)
*/
/*
* Make sure we're compiling on a UNIX or workalike operating system
*
* One of these symbols should be defined somewhere either internally by the compiler
* or in the common includes typical of a c program... tested on OpenBSD and Debian Linux
* I'm not aware of a compiler system for any *NIX variant in which one of these aren't defined
*/
#if !(defined(unix) || defined(_unix) || defined(__unix) || defined(__unix__) || defined(_unix_) \
|| defined(UNIX) || defined(_UNIX) || defined(__UNIX) || defined(__UNIX__) || defined(_UNIX_))
#error "The macros in ansi.h require a unix or workalike operating system to function properly!"
#endif
/* NOTE: The following color values come from a listing posted on cboard by the user Nominal Animal
* I've copied them here to complete my ansi.h header which I intend to post on cboard for the purpose
* of providing others a simple set of macros to use in order to manipulate color ansi terminal displays
*
* All credit for the following color values goes to Nominal Animal without whom, this collection of macros
* would not be possible.
*/
/* Uses Web color names, see http://en.wikipedia.org/wiki/Web_colors */
#define ALL_NORMAL "\033[0m"
#define FG_BLACK "\033[22;30m"
#define FG_MAROON "\033[22;31m" /* Dark red */
#define FG_GREEN "\033[22;32m"
#define FG_OLIVE "\033[22;33m" /* Brown */
#define FG_NAVY "\033[22;34m" /* Dark blue */
#define FG_PURPLE "\033[22;35m"
#define FG_TEAL "\033[22;36m" /* Dark cyan */
#define FG_SILVER "\033[22;37m" /* Light gray */
#define FG_GRAY "\033[1;30m" /* Dark gray */
#define FG_RED "\033[1;31m" /* Light red */
#define FG_LIME "\033[1;32m" /* Light green */
#define FG_YELLOW "\033[1;33m"
#define FG_BLUE "\033[1;34m"
#define FG_FUCHSIA "\033[1;35m" /* Light purple */
#define FG_AQUA "\033[1;36m" /* Light cyan */
#define FG_WHITE "\033[1;37m"
#define BG_BLACK "\033[40m"
#define BG_MAROON "\033[41m"
#define BG_GREEN "\033[42m"
#define BG_OLIVE "\033[43m"
#define BG_NAVY "\033[44m"
#define BG_PURPLE "\033[45m"
#define BG_TEAL "\033[46m"
#define BG_SILVER "\033[47m"
/* NOTE: Credit for the following macros goes to Nominal Animal, without whom these macros would not
* be possible. Many of them come directly from Nominal Animal's posts on cboard
*/
/* SETCURPOS(x,y)
* GOTOXY(x,y)
* XY(x,y)
* SETXY(x,y)
*
* All of these macros do the same thing... They set the cursor position to the x and y coordinates
* Where x is the column, and y is the row
*/
#if defined(GOTOXY)
#undef GOTOXY
#endif
#if defined(XY)
#undef XY
#endif
#if defined(SETXY)
#undef SETXY
#endif
#if defined(SETCURPOS)
#undef SETCURPOS
#endif
#define SETCURPOS(x,y) printf("\033[%d;%dH",y,x)
#define SETXY(x,y) printf("\033[%d;%dH",y,x)
#define XY(x,y) printf("\033[%d;%dH",y,x)
#define GOTOXY(x,y) printf("\033[%d;%dH",y,x)
/* CURUP(count)
* Moves the cursor up count lines
*/
#if defined(CURUP)
#undef CURUP
#endif
#define CURUP(count) printf("\033[%dA",count)
/* CURDOWN(count)
* Moves the cursor down count lines
*/
#if defined(CURDOWN)
#undef CURDOWN
#endif
#define CURDOWN(count) printf("\033[%dB",count)
/* INCCUR(count)
* Move the cursor forward by count cells
*/
#if defined(INCCUR)
#undef INCCUR
#endif
#define INCCUR(count) printf("\033[%dC",count)
/* DECCUR(count)
* Move cursor backward by count cells
*/
#if defined(DECCUR)
#undef DECCUR
#endif
#define DECCUR(count) printf("\033[%dD", count)
/* DECLINE(n)
*
* Decrement cursor n lines up
*/
#if defined(DECLINE)
#undef DECLINE
#endif
#define DECLINE(n) printf("\033[%dE",n)
/* INCLINE(n)
*
* Increment cursor n lines down
*/
#if defined(INCLINE)
#undef INCLINE
#endif
#define INCLINE(n) printf("\033[%dF",n)
/* SETCOLUMN(n)
* SETX(n)
*
* Set the current cursor column ( x coordinate ) position to n
*/
#if defined(SETCOLUMN)
#undef SETCOLUMN
#endif
#if defined(SETX)
#undef SETX
#endif
#define SETCOLUMN(n) printf("\033[%dG")
#define SETX(n) printf("\033[%dG")
/* HIDECUR()
* Takes no arguments... Hides the cursor
*/
#if defined(HIDECUR)
#undef HIDECUR
#endif
#define HIDECUR() printf("\033[?25l")
/* SHOWCUR()
* Takes no arguments... Shows a previously hidden cursor
*/
#if defined(SHOWCUR)
#undef SHOWCUR
#endif
#define SHOWCUR() printf("\033[?25h")
/* SAVECURPOS()
* Takes no arguments... Saves the cursor position internally
*/
#if defined(SAVECURPOS)
#undef SAVECURPOS
#endif
#define SAVECURPOS() printf("\033[s")
/* RESTORECURPOS()
* Takes no arguments... Restores a previously saved cursor position
*/
#if defined(RESTORECURPOS)
#undef RESTORECURPOS
#endif
#define RESTORECURPOS() printf("\033[u")
/* RESETCUR()
* Takes no arguments... Resets the cursor position to the top left of the screen
*/
#if defined(RESETCUR)
#undef RESETCUR
#endif
#define RESETCUR() printf("\033[H")
/* CLEARSCR()
* CLS()
* CLEAR()
*
* All three of these macros do the same thing... They clear the screen
* They don't take any arguments
*
*/
#ifdef CLEARSCR
#undef CLEARSCR
#endif
#ifdef CLS
#undef CLS
#endif
#ifdef CLEAR
#undef CLEAR
#endif
#define CLEARSCR() printf("\033[H\033[2J"); fflush(stdout)
#define CLS() printf("\033[H\033[2J"); fflush(stdout)
#define CLEAR() printf("\033[H\033[2J"); fflush(stdout)
/* CLEARTOEOS()
* CLEOS()
*
* Clear from cursor position to end of screen
*/
#if defined(CLEARTOEOS)
#undef CLEARTOEOS
#endif
#if defined(CLEOS)
#undef CLEOS
#endif
#define CLEARTOEOS() printf("\033[0J")
#define CLEOS() printf("\033[0J")
/* CLEARTOBOS()
* CLBOS()
*
* Clear from cursor to beginning of screen
*/
#if defined(CLEARTOBOS)
#undef CLEARTOBOS
#endif
#if defined(CLBOS)
#undef CLBOS
#endif
#define CLEARTOBOS() printf("\033[1J")
#define CLBOS() printf("\033[1J")
/* CLEARLINE()
* CLN()
*
* Clear the entire contents of the current line
*/
#if defined(CLEARLINE)
#undef CLEARLINE
#endif
#if defined(CLN)
#undef CLN
#endif
#define CLEARLINE() printf("\033[2K")
#define CLN() printf("\033[2K")
/* CLEARTOEOL()
* CLEOL()
*
* Clear from cursor position to end of line
*/
#if defined(CLEARTOEOL)
#undef CLEARTOEOL
#endif
#if defined(CLEOL)
#undef CLEOL
#endif
#define CLEARTOEOL() printf("\033[0K")
#define CLEOL() printf("\033[0K")
/* CLEARTOBOL()
* CLBOL()
*
* Clear from cursor position to beginning of line
*/
#if defined(CLEARTOBOL)
#undef CLEARTOBOL
#endif
#if defined(CLBOL)
#undef CLBOL
#endif
#define CLEARTOBOL() printf("\033[1K")
#define CLBOL() printf("\033[1K")
/* SETFG(color)
* Sets the foreground text color to "color" specified. "color" must be a string literal containing the
* appropriate foreground ANSI color code for the desired color. Above are a listing of FG_XXXX color
* literals defined to make things easier on the user. e.g. SETFG(FG_GREEN) sets the text color to dark green.
*/
#if defined(SETFG)
#undef SETFG
#endif
#define SETFG(color) printf("%s",color)
/* SETBG(color)
* Sets the background text color to "color" specified. "color" must be a string literal containing the
* appropriate background ANSI color code for the desired color. Above are a listing of BG_XXXX color
* literals defined to make things easier on the user. e.g. SETBG(FG_NAVY) sets the background color to navy blue.
*/
#if defined(SETBG)
#undef SETBG
#endif
#define SETBG(color) printf("%s",color)
/* SETCOLOR(fg,bg)
*
* Sets the foreground and background color to fg (foreground) and bg (background) colors specified. fg and bg must
* be a string literal containing the appropriate foreground and background ANSI color codes for the desired colors.
* Above are a listing of FG_XXXX and BG_XXXX color literals defined to make things easier on the user.
* e.g. SETCOLOR(FG_WHITE,BG_NAVY) sets the foreground color to white and the background color to Navy Blue
*/
#if defined(SETCOLOR)
#undef SETCOLOR
#endif
#define SETCOLOR(fg,bg) printf("%s%s",fg,bg)
/* SCROLLUP(n)
* SCRUP(n)
*
* Scrolls the screen up by n lines.
*/
#if defined(SCROLLUP)
#undef SCROLLUP
#endif
#if defined(SCRUP)
#undef SCRUP
#endif
#define SCROLLUP(n) printf("\033[%dS",n)
/* SCROLLDOWN(n)
* SCRDOWN(n)
* SCRDWN(n)
*
* Scrolls the screen down by n lines
*/
#if defined(SCROLLDOWN)
#undef SCROLLDOWN
#endif
#if defined(SCRDOWN)
#undef SCRDOWN
#endif
#if defined(SCRDWN)
#undef SCRDWN
#endif
#define SCROLLDOWN(n) printf("\033[%dT",n)
#define SCRDOWN(n) printf("\033[%dT",n)
#define SCRDWN(n) printf("\033[%dT",n)
#endif
/* __ANSI_H__ */
If you find them useful, then great... if not, then perhaps some suggestions?
Twiki