Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********************************************************************
- ** cons.h - functions to handle console positioning and colors
- ********************************************************************/
- #ifndef _CONS_H_
- #define _CONS_H_ // avoid multiple inclusions
- #ifndef BOOL
- #include <windows.h>
- #endif
- // macro to calculate the high intensity color
- #define HI_COLOR(x) (8 + (x))
- // default color (current)
- #define COLOR_DEFAULT -1
- // base color codes
- #define COLOR_BLACK 0
- #define COLOR_BLUE 1
- #define COLOR_GREEN 2
- #define COLOR_CYAN 3
- #define COLOR_RED 4
- #define COLOR_MAGENTA 5
- #define COLOR_BROWN 6
- #define COLOR_LIGHTGRAY 7
- // high intensity
- #define COLOR_DARKGRAY HI_COLOR(COLOR_BLACK)
- #define COLOR_LIGHTBLUE HI_COLOR(COLOR_BLUE)
- #define COLOR_LIGHTGREEN HI_COLOR(COLOR_GREEN)
- #define COLOR_LIGHTCYAN HI_COLOR(COLOR_CYAN)
- #define COLOR_LIGHTRED HI_COLOR(COLOR_RED)
- #define COLOR_LIGHTMAGENTA HI_COLOR(COLOR_MAGENTA)
- #define COLOR_YELLOW HI_COLOR(COLOR_BROWN)
- #define COLOR_WHITE HI_COLOR(COLOR_LIGHTGRAY)
- // default FG/BG colors
- #define COLOR_DEF_FG COLOR_LIGHTGRAY
- #define COLOR_DEF_BG COLOR_BLACK
- // prototypes
- BOOL CON_init(BOOL bEnableANSI); // initialize (opt: enable ANSI escape processing)
- void CON_reset(BOOL bRestore); // reset (opt: restore color and position)
- void CON_clear(); // clear and position to 0/0
- void CON_printat(int r, int c, char *fmt, ...); // formatted print at given row/col
- void CON_showcursor(BOOL bYesNo); // show or hide the cursor
- void CON_getsize(int *nr, int *nc); // get rows and columns
- void CON_getpos(int *row, int *col); // get cursor position
- void CON_setpos(int row, int col); // set cursor position
- void CON_getcolor(int *fg, int *bg); // get colors
- void CON_setcolor(int fg, int bg); // set colors
- void CON_resetcolor(); // reset colors to initial default
- #endif
- /********************************************************************
- ** cons.c - functions to handle console positioning and colors
- ********************************************************************/
- // standard headers
- #include <stdlib.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <memory.h>
- #include <malloc.h>
- #include <string.h>
- #include <stdarg.h>
- // windows stuff
- #include <windows.h>
- // our header
- #include "cons.h"
- // misc macros (see code)
- #define MKCOLOR(fg, bg) (((bg & 0x0F) << 4) + (fg & 0x0F))
- #define FGCOLOR(c) (c % 0x10)
- #define BGCOLOR(c) (c / 0x10)
- // internal functions
- static void CON_getinfo();
- // internal workareas
- static HANDLE hConsole = INVALID_HANDLE_VALUE;
- static DWORD dwModeFlags;
- static CONSOLE_SCREEN_BUFFER_INFO csbi, csbi_bak;
- static int rows, cols;
- static int nrow, ncol;
- static int fgcol, bgcol;
- // initializes console
- BOOL CON_init(BOOL bEnableANSI)
- {
- DWORD dwMode;
- size_t bufsize;
- // setup
- hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- if (INVALID_HANDLE_VALUE == hConsole) return(FALSE);
- if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) return(FALSE);
- memcpy(&csbi_bak, &csbi, sizeof(CONSOLE_SCREEN_BUFFER_INFO));
- if (!GetConsoleMode(hConsole, &dwModeFlags)) return(FALSE);
- // initialize values (rows, cols, ...)
- CON_getinfo();
- if (bEnableANSI)
- {
- // enable ANSI (VT) escape sequences support
- dwMode = (dwModeFlags | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
- if (!SetConsoleMode(hConsole, dwMode)) return(FALSE);
- }
- // all Ok
- return(TRUE);
- }
- // reset console, optionally reset color and position
- void CON_reset(BOOL bRestore)
- {
- if (INVALID_HANDLE_VALUE == hConsole) return;
- if (bRestore)
- {
- // restore colors and cursor position
- SetConsoleTextAttribute(hConsole, csbi_bak.wAttributes);
- //if (csbi_bak.dwCursorPosition.Y > 0) csbi_bak.dwCursorPosition.Y--;
- SetConsoleCursorPosition(hConsole, csbi_bak.dwCursorPosition);
- }
- // set back mode to original one (e.g. disable ANSI)
- SetConsoleMode(hConsole, dwModeFlags);
- hConsole = NULL;
- }
- // clear the console and position to upper left corner
- void CON_clear()
- {
- COORD coordScreen = {0, 0};
- DWORD cCharsWritten;
- DWORD dwConSize;
- if (INVALID_HANDLE_VALUE == hConsole) return;
- // update console infos (size color), wipe and position to 0/0
- CON_getinfo();
- dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
- FillConsoleOutputCharacter(hConsole, (TCHAR)' ', dwConSize, coordScreen, &cCharsWritten);
- FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
- SetConsoleCursorPosition(hConsole, coordScreen);
- }
- // print formatted data at given position
- void CON_printat(int r, int c, char *fmt, ...)
- {
- va_list ap;
- CON_setpos(r, c);
- va_start(ap, fmt);
- vfprintf(stdout, fmt, ap);
- va_end(ap);
- fflush(stdout);
- }
- // number of rows and columns
- void CON_getsize(int *nr, int *nc)
- {
- CON_getinfo();
- *nr = rows;
- *nc = cols;
- }
- // set cursor position
- void CON_setpos(int row, int col)
- {
- COORD pos = {col, row};
- if (INVALID_HANDLE_VALUE == hConsole) return;
- SetConsoleCursorPosition(hConsole, pos);
- }
- // get current position
- void CON_getpos(int *row, int *col)
- {
- CON_getinfo();
- *row = nrow;
- *col = ncol;
- }
- // show/hide the cursor
- void CON_showcursor(BOOL bYesNo)
- {
- CONSOLE_CURSOR_INFO curs;
- if (INVALID_HANDLE_VALUE == hConsole) return;
- GetConsoleCursorInfo(hConsole, &curs);
- if (bYesNo) curs.bVisible = TRUE;
- else curs.bVisible = FALSE;
- SetConsoleCursorInfo(hConsole, &curs);
- }
- // get current colors
- void CON_getcolor(int *fg, int *bg)
- {
- CON_getinfo();
- *fg = fgcol;
- *bg = bgcol;
- }
- // set foreground and background colors
- void CON_setcolor(int fg, int bg)
- {
- int fcol=fg, bcol=bg;
- if (INVALID_HANDLE_VALUE == hConsole) return;
- if ((COLOR_DEFAULT == fg)||(COLOR_DEFAULT == bg)) CON_getinfo();
- if (COLOR_DEFAULT == fg) fcol=fgcol;
- if (COLOR_DEFAULT == bg) bcol=bgcol;
- SetConsoleTextAttribute(hConsole, MKCOLOR(fcol, bcol));
- }
- // set color back to initial (at "CON_init" time) color
- void CON_resetcolor()
- {
- if (INVALID_HANDLE_VALUE == hConsole) return;
- SetConsoleTextAttribute(hConsole, csbi_bak.wAttributes);
- CON_getinfo();
- }
- // refresh console informations this is needed in case the infos
- // are changed extenally or true ANSI escapes or if the console
- // window is resized by the user
- static void CON_getinfo()
- {
- if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) return;
- rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
- cols = csbi.srWindow.Right - csbi.srWindow.Left + 1;
- nrow = csbi.dwCursorPosition.Y;
- ncol = csbi.dwCursorPosition.X;
- fgcol = FGCOLOR(csbi.wAttributes);
- bgcol = BGCOLOR(csbi.wAttributes);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement