Advertisement
Guest User

cons.c - windows console helper

a guest
Nov 13th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.06 KB | None | 0 0
  1. /********************************************************************
  2.  ** cons.h - functions to handle console positioning and colors  
  3.  ********************************************************************/
  4.  
  5. #ifndef _CONS_H_
  6. #define _CONS_H_    // avoid multiple inclusions
  7.  
  8. #ifndef BOOL
  9. #include <windows.h>
  10. #endif
  11.  
  12. // macro to calculate the high intensity color
  13. #define HI_COLOR(x)         (8 + (x))
  14.  
  15. // default color (current)
  16. #define COLOR_DEFAULT       -1
  17.  
  18. // base color codes
  19. #define COLOR_BLACK          0
  20. #define COLOR_BLUE           1
  21. #define COLOR_GREEN          2
  22. #define COLOR_CYAN           3
  23. #define COLOR_RED            4
  24. #define COLOR_MAGENTA        5
  25. #define COLOR_BROWN          6
  26. #define COLOR_LIGHTGRAY      7
  27.  
  28. // high intensity
  29. #define COLOR_DARKGRAY       HI_COLOR(COLOR_BLACK)
  30. #define COLOR_LIGHTBLUE      HI_COLOR(COLOR_BLUE)
  31. #define COLOR_LIGHTGREEN     HI_COLOR(COLOR_GREEN)
  32. #define COLOR_LIGHTCYAN      HI_COLOR(COLOR_CYAN)
  33. #define COLOR_LIGHTRED       HI_COLOR(COLOR_RED)
  34. #define COLOR_LIGHTMAGENTA   HI_COLOR(COLOR_MAGENTA)
  35. #define COLOR_YELLOW         HI_COLOR(COLOR_BROWN)
  36. #define COLOR_WHITE          HI_COLOR(COLOR_LIGHTGRAY)
  37.  
  38. // default FG/BG colors
  39. #define COLOR_DEF_FG         COLOR_LIGHTGRAY
  40. #define COLOR_DEF_BG         COLOR_BLACK
  41.  
  42.  
  43. // prototypes
  44. BOOL CON_init(BOOL bEnableANSI);                // initialize (opt: enable ANSI escape processing)
  45. void CON_reset(BOOL bRestore);                  // reset (opt: restore color and position)
  46. void CON_clear();                               // clear and position to 0/0
  47. void CON_printat(int r, int c, char *fmt, ...); // formatted print at given row/col
  48. void CON_showcursor(BOOL bYesNo);               // show or hide the cursor
  49. void CON_getsize(int *nr, int *nc);             // get rows and columns
  50. void CON_getpos(int *row, int *col);            // get cursor position
  51. void CON_setpos(int row, int col);              // set cursor position  
  52. void CON_getcolor(int *fg, int *bg);            // get colors
  53. void CON_setcolor(int fg, int bg);              // set colors
  54. void CON_resetcolor();                          // reset colors to initial default
  55.  
  56. #endif
  57.  
  58.  
  59. /********************************************************************
  60.  ** cons.c - functions to handle console positioning and colors  
  61.  ********************************************************************/
  62.  
  63. // standard headers
  64. #include <stdlib.h>
  65. #include <stdio.h>
  66. #include <ctype.h>
  67. #include <memory.h>
  68. #include <malloc.h>
  69. #include <string.h>
  70. #include <stdarg.h>
  71.  
  72. // windows stuff
  73. #include <windows.h>
  74.  
  75. // our header
  76. #include "cons.h"
  77.  
  78. // misc macros (see code)
  79. #define MKCOLOR(fg, bg) (((bg & 0x0F) << 4) + (fg & 0x0F))
  80. #define FGCOLOR(c)      (c % 0x10)
  81. #define BGCOLOR(c)      (c / 0x10)
  82.  
  83. // internal functions
  84. static void  CON_getinfo();
  85.  
  86. // internal workareas
  87. static HANDLE                      hConsole = INVALID_HANDLE_VALUE;
  88. static DWORD                       dwModeFlags;
  89. static CONSOLE_SCREEN_BUFFER_INFO  csbi, csbi_bak;
  90. static int                         rows, cols;
  91. static int                         nrow, ncol;
  92. static int                         fgcol, bgcol;
  93.  
  94. // initializes console
  95. BOOL CON_init(BOOL bEnableANSI)
  96. {
  97.   DWORD   dwMode;
  98.   size_t  bufsize;
  99.  
  100.   // setup
  101.   hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  102.   if (INVALID_HANDLE_VALUE == hConsole) return(FALSE);
  103.   if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) return(FALSE);
  104.   memcpy(&csbi_bak, &csbi, sizeof(CONSOLE_SCREEN_BUFFER_INFO));
  105.   if (!GetConsoleMode(hConsole, &dwModeFlags)) return(FALSE);
  106.  
  107.   // initialize values (rows, cols, ...)
  108.   CON_getinfo();
  109.  
  110.   if (bEnableANSI)
  111.   {
  112.     // enable ANSI (VT) escape sequences support
  113.     dwMode = (dwModeFlags | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
  114.     if (!SetConsoleMode(hConsole, dwMode)) return(FALSE);
  115.   }
  116.  
  117.   // all Ok
  118.   return(TRUE);
  119. }
  120.  
  121. // reset console, optionally reset color and position
  122. void CON_reset(BOOL bRestore)
  123. {
  124.   if (INVALID_HANDLE_VALUE == hConsole) return;
  125.   if (bRestore)
  126.   {
  127.     // restore colors and cursor position
  128.     SetConsoleTextAttribute(hConsole, csbi_bak.wAttributes);
  129.     //if (csbi_bak.dwCursorPosition.Y > 0) csbi_bak.dwCursorPosition.Y--;
  130.     SetConsoleCursorPosition(hConsole, csbi_bak.dwCursorPosition);
  131.   }
  132.   // set back mode to original one (e.g. disable ANSI)
  133.   SetConsoleMode(hConsole, dwModeFlags);
  134.   hConsole = NULL;
  135. }
  136.  
  137. // clear the console and position to upper left corner
  138. void CON_clear()
  139. {
  140.   COORD coordScreen = {0, 0};
  141.   DWORD cCharsWritten;
  142.   DWORD dwConSize;
  143.  
  144.   if (INVALID_HANDLE_VALUE == hConsole) return;
  145.   // update console infos (size color), wipe and position to 0/0
  146.   CON_getinfo();
  147.   dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  148.   FillConsoleOutputCharacter(hConsole, (TCHAR)' ', dwConSize, coordScreen, &cCharsWritten);
  149.   FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
  150.   SetConsoleCursorPosition(hConsole, coordScreen);
  151. }
  152.  
  153. // print formatted data at given position
  154. void CON_printat(int r, int c, char *fmt, ...)
  155. {
  156.   va_list ap;
  157.  
  158.   CON_setpos(r, c);
  159.   va_start(ap, fmt);
  160.   vfprintf(stdout, fmt, ap);
  161.   va_end(ap);
  162.   fflush(stdout);
  163. }
  164.  
  165. // number of rows and columns
  166. void CON_getsize(int *nr, int *nc)
  167. {
  168.   CON_getinfo();
  169.   *nr = rows;
  170.   *nc = cols;
  171. }
  172.  
  173. // set cursor position
  174. void CON_setpos(int row, int col)
  175. {
  176.   COORD pos = {col, row};
  177.   if (INVALID_HANDLE_VALUE == hConsole) return;
  178.   SetConsoleCursorPosition(hConsole, pos);
  179. }
  180.  
  181. // get current position
  182. void CON_getpos(int *row, int *col)
  183. {
  184.   CON_getinfo();
  185.   *row = nrow;
  186.   *col = ncol;
  187. }
  188.  
  189. // show/hide the cursor
  190. void CON_showcursor(BOOL bYesNo)
  191. {
  192.   CONSOLE_CURSOR_INFO curs;
  193.  
  194.   if (INVALID_HANDLE_VALUE == hConsole) return;
  195.   GetConsoleCursorInfo(hConsole, &curs);
  196.   if (bYesNo) curs.bVisible = TRUE;
  197.     else curs.bVisible = FALSE;
  198.   SetConsoleCursorInfo(hConsole, &curs);
  199. }
  200.  
  201. // get current colors
  202. void CON_getcolor(int *fg, int *bg)
  203. {
  204.   CON_getinfo();
  205.   *fg = fgcol;
  206.   *bg = bgcol;
  207. }
  208.  
  209. // set foreground and background colors
  210. void CON_setcolor(int fg, int bg)
  211. {
  212.   int fcol=fg, bcol=bg;
  213.  
  214.   if (INVALID_HANDLE_VALUE == hConsole) return;
  215.   if ((COLOR_DEFAULT == fg)||(COLOR_DEFAULT == bg)) CON_getinfo();
  216.   if (COLOR_DEFAULT == fg) fcol=fgcol;
  217.   if (COLOR_DEFAULT == bg) bcol=bgcol;
  218.   SetConsoleTextAttribute(hConsole, MKCOLOR(fcol, bcol));
  219. }
  220.  
  221. // set color back to initial (at "CON_init" time) color
  222. void CON_resetcolor()
  223. {
  224.   if (INVALID_HANDLE_VALUE == hConsole) return;
  225.   SetConsoleTextAttribute(hConsole, csbi_bak.wAttributes);
  226.   CON_getinfo();
  227. }
  228.  
  229. // refresh console informations this is needed in case the infos
  230. // are changed extenally or true ANSI escapes or if the console
  231. // window is resized by the user
  232. static void CON_getinfo()
  233. {
  234.   if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) return;
  235.   rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
  236.   cols = csbi.srWindow.Right - csbi.srWindow.Left + 1;
  237.   nrow = csbi.dwCursorPosition.Y;
  238.   ncol = csbi.dwCursorPosition.X;
  239.   fgcol = FGCOLOR(csbi.wAttributes);
  240.   bgcol = BGCOLOR(csbi.wAttributes);
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement