rogueutil
Cross-platform C/C++ utility for creating text-based user interfaces.
rogueutil.h
Go to the documentation of this file.
1 
30 #ifndef RUTIL_H
31 #define RUTIL_H
32 
33 #ifdef _DOXYGEN_
34 
35  #define RUTIL_USE_ANSI
36 
41  #define RUTIL_STRING char*
42 #endif /* _DOXYGEN */
43 
44 #ifdef __cplusplus
45  /* Common C++ headers */
46  #include <iostream>
47  #include <string>
48  #include <sstream>
49  #include <cstdio> /* for getch() */
50  #include <cstdarg> /* for colorPrint() */
51 
52  /* Namespace forward declarations */
53  namespace rogueutil
54  {
55  void locate(int x, int y);
56  }
57 #else
58  #include <stdio.h> /* for getch() / printf() */
59  #include <string.h> /* for strlen() */
60  #include <stdarg.h> /* for colorPrint() */
61 
62  void locate(int x, int y); /* Forward declare for C to avoid warnings */
63 #endif
64 
65 #ifdef _WIN32
66  #include <windows.h> /* for WinAPI and Sleep() */
67  #define _NO_OLDNAMES /* for MinGW compatibility */
68  #include <conio.h> /* for getch() and kbhit() */
69  #include <lmcons.h> /* for getUsername() */
70  #define getch _getch
71  #define kbhit _kbhit
72 #else
73  #include <termios.h> /* for getch() and kbhit() */
74  #include <unistd.h> /* for getch() and kbhit() */
75  #include <time.h> /* for nanosleep() */
76  #include <sys/ioctl.h> /* for getkey() */
77  #include <sys/types.h> /* for kbhit() */
78  #include <sys/time.h> /* for kbhit() */
79 #endif
80 
81 /* Functions covered by Window's conio.h */
82 #ifndef _WIN32
83 
89 int
90 getch(void)
91 {
92  struct termios oldt, newt;
93  int ch;
94  tcgetattr(STDIN_FILENO, &oldt);
95  newt = oldt;
96  newt.c_lflag &= ~(ICANON | ECHO);
97  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
98  ch = getchar();
99  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
100  return ch;
101 }
102 
108 int
109 kbhit(void)
110 {
111  static struct termios oldt, newt;
112  int cnt = 0;
113  tcgetattr(STDIN_FILENO, &oldt);
114  newt = oldt;
115  newt.c_lflag &= ~(ICANON | ECHO);
116  newt.c_iflag = 0; /* input mode */
117  newt.c_oflag = 0; /* output mode */
118  newt.c_cc[VMIN] = 1; /* minimum time to wait */
119  newt.c_cc[VTIME] = 1; /* minimum characters to wait for */
120  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
121  ioctl(0, FIONREAD, &cnt); /* Read count */
122  struct timeval tv;
123  tv.tv_sec = 0;
124  tv.tv_usec = 100;
125  select(STDIN_FILENO+1, NULL, NULL, NULL, &tv); /* A small time delay */
126  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
127  return cnt; /* Return number of characters */
128 }
129 
130 #endif /* _WIN32 */
131 
132 
133 #ifndef gotoxy
134 
139 void
140 gotoxy(int x, int y)
141 {
142 #ifdef __cplusplus
143  rogueutil::
144 #endif
145  locate(x,y);
146 }
147 #endif /* gotoxy */
148 
149 #ifdef __cplusplus
150 
153 namespace rogueutil
154 {
155 #endif /* __cplusplus */
156 
157 #ifdef __cplusplus
158  #ifndef RUTIL_STRING
159  typedef std::string RUTIL_STRING;
160  #endif /* RUTIL_STRING */
161 
162 #else
163  #ifndef RUTIL_STRING
164  typedef const char* RUTIL_STRING;
165  #endif /* RUTIL_STRING */
166 
167 #endif /* __cplusplus */
168 
172 typedef enum color_code {
173  BLACK,
174  BLUE,
175  GREEN,
176  CYAN,
177  RED,
178  MAGENTA,
179  BROWN,
180  GREY,
181  DARKGREY,
182  LIGHTBLUE,
183  LIGHTGREEN,
184  LIGHTCYAN,
185  LIGHTRED,
186  LIGHTMAGENTA,
187  YELLOW,
188  WHITE
189 } color_code;
190 
191 /* Constant strings for ANSI colors ans seqiences */
192 static const RUTIL_STRING ANSI_CLS = "\033[2J\033[3J";
193 static const RUTIL_STRING ANSI_CONSOLE_TITLE_PRE = "\033]0;";
194 static const RUTIL_STRING ANSI_CONSOLE_TITLE_POST = "\007";
195 static const RUTIL_STRING ANSI_ATTRIBUTE_RESET = "\033[0m";
196 static const RUTIL_STRING ANSI_CURSOR_HIDE = "\033[?25l";
197 static const RUTIL_STRING ANSI_CURSOR_SHOW = "\033[?25h";
198 static const RUTIL_STRING ANSI_CURSOR_HOME = "\033[H";
199 static const RUTIL_STRING ANSI_BLACK = "\033[22;30m";
200 static const RUTIL_STRING ANSI_RED = "\033[22;31m";
201 static const RUTIL_STRING ANSI_GREEN = "\033[22;32m";
202 static const RUTIL_STRING ANSI_BROWN = "\033[22;33m";
203 static const RUTIL_STRING ANSI_BLUE = "\033[22;34m";
204 static const RUTIL_STRING ANSI_MAGENTA = "\033[22;35m";
205 static const RUTIL_STRING ANSI_CYAN = "\033[22;36m";
206 static const RUTIL_STRING ANSI_GREY = "\033[22;37m";
207 static const RUTIL_STRING ANSI_DARKGREY = "\033[01;30m";
208 static const RUTIL_STRING ANSI_LIGHTRED = "\033[01;31m";
209 static const RUTIL_STRING ANSI_LIGHTGREEN = "\033[01;32m";
210 static const RUTIL_STRING ANSI_YELLOW = "\033[01;33m";
211 static const RUTIL_STRING ANSI_LIGHTBLUE = "\033[01;34m";
212 static const RUTIL_STRING ANSI_LIGHTMAGENTA = "\033[01;35m";
213 static const RUTIL_STRING ANSI_LIGHTCYAN = "\033[01;36m";
214 static const RUTIL_STRING ANSI_WHITE = "\033[01;37m";
215 static const RUTIL_STRING ANSI_BACKGROUND_BLACK = "\033[40m";
216 static const RUTIL_STRING ANSI_BACKGROUND_RED = "\033[41m";
217 static const RUTIL_STRING ANSI_BACKGROUND_GREEN = "\033[42m";
218 static const RUTIL_STRING ANSI_BACKGROUND_YELLOW = "\033[43m";
219 static const RUTIL_STRING ANSI_BACKGROUND_BLUE = "\033[44m";
220 static const RUTIL_STRING ANSI_BACKGROUND_MAGENTA = "\033[45m";
221 static const RUTIL_STRING ANSI_BACKGROUND_CYAN = "\033[46m";
222 static const RUTIL_STRING ANSI_BACKGROUND_WHITE = "\033[47m";
223 /* Remaining colors not supported as background colors */
224 
228 typedef enum key_code {
229  KEY_ESCAPE = 0,
230  KEY_ENTER = 1,
231  KEY_SPACE = 32,
232 
233  KEY_INSERT = 2,
234  KEY_HOME = 3,
235  KEY_PGUP = 4,
236  KEY_DELETE = 5,
237  KEY_END = 6,
238  KEY_PGDOWN = 7,
239 
240  KEY_UP = 14,
241  KEY_DOWN = 15,
242  KEY_LEFT = 16,
243  KEY_RIGHT = 17,
244 
245  KEY_F1 = 18,
246  KEY_F2 = 19,
247  KEY_F3 = 20,
248  KEY_F4 = 21,
249  KEY_F5 = 22,
250  KEY_F6 = 23,
251  KEY_F7 = 24,
252  KEY_F8 = 25,
253  KEY_F9 = 26,
254  KEY_F10 = 27,
255  KEY_F11 = 28,
256  KEY_F12 = 29,
257 
258  KEY_NUMDEL = 30,
259  KEY_NUMPAD0 = 31,
260  KEY_NUMPAD1 = 127,
261  KEY_NUMPAD2 = 128,
262  KEY_NUMPAD3 = 129,
263  KEY_NUMPAD4 = 130,
264  KEY_NUMPAD5 = 131,
265  KEY_NUMPAD6 = 132,
266  KEY_NUMPAD7 = 133,
267  KEY_NUMPAD8 = 134,
268  KEY_NUMPAD9 = 135,
269 } key_code;
270 
277 static void
279 {
280 #ifdef __cplusplus
281  std::cout << st;
282 #else
283  printf("%s", st);
284 #endif
285 }
286 
292 int
293 getkey(void)
294 {
295 #ifndef _WIN32
296  int cnt = kbhit(); /* for ANSI escapes processing */
297 #endif
298  int k = getch();
299  switch(k) {
300  case 0: {
301  int kk;
302  switch (kk = getch()) {
303  case 71:
304  return KEY_NUMPAD7;
305  case 72:
306  return KEY_NUMPAD8;
307  case 73:
308  return KEY_NUMPAD9;
309  case 75:
310  return KEY_NUMPAD4;
311  case 77:
312  return KEY_NUMPAD6;
313  case 79:
314  return KEY_NUMPAD1;
315  case 80:
316  return KEY_NUMPAD2;
317  case 81:
318  return KEY_NUMPAD3;
319  case 82:
320  return KEY_NUMPAD0;
321  case 83:
322  return KEY_NUMDEL;
323  default:
324  return kk-59+KEY_F1; /* Function keys */
325  }
326  }
327  case 224: {
328  int kk;
329  switch (kk = getch()) {
330  case 71:
331  return KEY_HOME;
332  case 72:
333  return KEY_UP;
334  case 73:
335  return KEY_PGUP;
336  case 75:
337  return KEY_LEFT;
338  case 77:
339  return KEY_RIGHT;
340  case 79:
341  return KEY_END;
342  case 80:
343  return KEY_DOWN;
344  case 81:
345  return KEY_PGDOWN;
346  case 82:
347  return KEY_INSERT;
348  case 83:
349  return KEY_DELETE;
350  default:
351  return kk-123+KEY_F1; /* Function keys */
352  }
353  }
354  case 13:
355  return KEY_ENTER;
356 #ifdef _WIN32
357  case 27:
358  return KEY_ESCAPE;
359 #else /* _WIN32 */
360  case 155: /* single-character CSI */
361  case 27: {
362  /* Process ANSI escape sequences */
363  if (cnt >= 3 && getch() == '[') {
364  switch (k = getch()) {
365  case 'A':
366  return KEY_UP;
367  case 'B':
368  return KEY_DOWN;
369  case 'C':
370  return KEY_RIGHT;
371  case 'D':
372  return KEY_LEFT;
373  default:
374  return KEY_ESCAPE;
375  }
376  } else return KEY_ESCAPE;
377  }
378 #endif /* _WIN32 */
379  default:
380  return k;
381  }
382 }
383 
389 int
390 nb_getch(void)
391 {
392  if (kbhit()) return getch();
393  else return 0;
394 }
395 
402 getANSIColor(const int c)
403 {
404  switch (c) {
405  case BLACK :
406  return ANSI_BLACK;
407  case BLUE :
408  return ANSI_BLUE; /* non-ANSI */
409  case GREEN :
410  return ANSI_GREEN;
411  case CYAN :
412  return ANSI_CYAN; /* non-ANSI */
413  case RED :
414  return ANSI_RED; /* non-ANSI */
415  case MAGENTA :
416  return ANSI_MAGENTA;
417  case BROWN :
418  return ANSI_BROWN;
419  case GREY :
420  return ANSI_GREY;
421  case DARKGREY :
422  return ANSI_DARKGREY;
423  case LIGHTBLUE :
424  return ANSI_LIGHTBLUE; /* non-ANSI */
425  case LIGHTGREEN :
426  return ANSI_LIGHTGREEN;
427  case LIGHTCYAN :
428  return ANSI_LIGHTCYAN; /* non-ANSI; */
429  case LIGHTRED :
430  return ANSI_LIGHTRED; /* non-ANSI; */
431  case LIGHTMAGENTA:
432  return ANSI_LIGHTMAGENTA;
433  case YELLOW :
434  return ANSI_YELLOW; /* non-ANSI */
435  case WHITE :
436  return ANSI_WHITE;
437  default:
438  return "";
439  }
440 }
441 
448 getANSIBgColor(const int c)
449 {
450  switch (c) {
451  case BLACK :
452  return ANSI_BACKGROUND_BLACK;
453  case BLUE :
454  return ANSI_BACKGROUND_BLUE;
455  case GREEN :
456  return ANSI_BACKGROUND_GREEN;
457  case CYAN :
458  return ANSI_BACKGROUND_CYAN;
459  case RED :
460  return ANSI_BACKGROUND_RED;
461  case MAGENTA:
462  return ANSI_BACKGROUND_MAGENTA;
463  case BROWN :
464  return ANSI_BACKGROUND_YELLOW;
465  case GREY :
466  return ANSI_BACKGROUND_WHITE;
467  default:
468  return "";
469  }
470 }
471 
477 void
478 setColor(int c)
479 {
480 #if defined(_WIN32) && !defined(RUTIL_USE_ANSI)
481  HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
482  CONSOLE_SCREEN_BUFFER_INFO csbi;
483 
484  GetConsoleScreenBufferInfo(hConsole, &csbi);
485 
486  SetConsoleTextAttribute(hConsole, (csbi.wAttributes & 0xFFF0) | (WORD)c); // Foreground colors take up the least significant byte
487 #else
489 #endif
490 }
491 
497 void
499 {
500 #if defined(_WIN32) && !defined(RUTIL_USE_ANSI)
501  HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
502  CONSOLE_SCREEN_BUFFER_INFO csbi;
503 
504  GetConsoleScreenBufferInfo(hConsole, &csbi);
505 
506  SetConsoleTextAttribute(hConsole, (csbi.wAttributes & 0xFF0F) | (((WORD)c) << 4)); // Background colors take up the second-least significant byte
507 #else
509 #endif
510 }
511 
516 int
518 {
519 #if defined(_WIN32) && !defined(RUTIL_USE_ANSI)
520  static char initialized = 0; /* bool */
521  static WORD attributes;
522 
523  if (!initialized) {
524  CONSOLE_SCREEN_BUFFER_INFO csbi;
525  GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
526  attributes = csbi.wAttributes;
527  initialized = 1;
528  }
529  return (int)attributes;
530 #else
531  return -1;
532 #endif
533 }
534 
541 void
543 {
544 #if defined(_WIN32) && !defined(RUTIL_USE_ANSI)
545  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (WORD)saveDefaultColor());
546 #else
547  rutil_print(ANSI_ATTRIBUTE_RESET);
548 #endif
549 }
550 
554 void
555 cls(void)
556 {
557 #if defined(_WIN32) && !defined(RUTIL_USE_ANSI)
558  /* Based on https://msdn.microsoft.com/en-us/library/windows/desktop/ms682022%28v=vs.85%29.aspx */
559  const HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
560  const COORD coordScreen = {0, 0};
561  DWORD cCharsWritten;
562  CONSOLE_SCREEN_BUFFER_INFO csbi;
563 
564  GetConsoleScreenBufferInfo(hConsole, &csbi);
565  const DWORD dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
566  FillConsoleOutputCharacter(hConsole, (TCHAR)' ', dwConSize, coordScreen, &cCharsWritten);
567 
568  GetConsoleScreenBufferInfo(hConsole, &csbi);
569  FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
570 
571  SetConsoleCursorPosition(hConsole, coordScreen);
572 #else
573  rutil_print(ANSI_CLS);
574  rutil_print(ANSI_CURSOR_HOME);
575 #endif
576 }
577 
581 void
582 locate(int x, int y)
583 {
584 #if defined(_WIN32) && !defined(RUTIL_USE_ANSI)
585  COORD coord;
586  coord.X = (SHORT)(x - 1);
587  coord.Y = (SHORT)(y - 1); /* Windows uses 0-based coordinates */
588  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
589 #else /* _WIN32 || USE_ANSI */
590 #ifdef __cplusplus
591  std::stringstream ss;
592  ss << "\033[" << y << ";" << x << "H";
593  rutil_print(ss.str());
594 #else
595  char buf[32];
596  sprintf(buf, "\033[%d;%df", y, x);
597  rutil_print(buf);
598 #endif /* __cplusplus */
599 #endif /* _WIN32 || USE_ANSI */
600 }
601 
605 #ifdef __cplusplus
606 void
607 setString(const RUTIL_STRING & str_)
608 {
609  const char * const str = str_.data();
610  unsigned int len = static_cast<unsigned int>(str_.size());
611 #else /* __cplusplus */
612 void
614 {
615  unsigned int len = (unsigned int)strlen(str);
616 #endif /* __cplusplus */
617 #if defined(_WIN32) && !defined(RUTIL_USE_ANSI)
618  HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
619  DWORD numberOfCharsWritten;
620  CONSOLE_SCREEN_BUFFER_INFO csbi;
621 
622  GetConsoleScreenBufferInfo(hConsoleOutput, &csbi);
623 
624 #ifdef UNICODE
625  WriteConsoleOutputCharacterA(hConsoleOutput, str,
626  len, csbi.dwCursorPosition,
627  &numberOfCharsWritten);
628 #else
629  WriteConsoleOutputCharacter(hConsoleOutput, str,
630  len, csbi.dwCursorPosition,
631  &numberOfCharsWritten);
632 #endif /* UNICODE */
633 
634 #else /* _WIN32 || USE_ANSI */
635  rutil_print(str);
636 #ifdef __cplusplus
637  std::stringstream ss;
638  ss << "\033[" << len << 'D';
639  rutil_print(ss.str());
640 #else
641  char buf[3 + 20 + 1]; /* 20 = max length of 64-bit
642  * unsigned int when printed as dec */
643  sprintf(buf, "\033[%uD", len);
644  rutil_print(buf);
645 #endif /* __cplusplus */
646 #endif /* _WIN32 || USE_ANSI */
647 }
648 
652 void
653 setChar(char ch)
654 {
655  const char buf[] = {ch, 0};
656  setString(buf);
657 }
658 
663 void
664 setCursorVisibility(char visible)
665 {
666 #if defined(_WIN32) && !defined(RUTIL_USE_ANSI)
667  HANDLE hConsoleOutput = GetStdHandle( STD_OUTPUT_HANDLE );
668  CONSOLE_CURSOR_INFO structCursorInfo;
669  GetConsoleCursorInfo( hConsoleOutput, &structCursorInfo ); // Get current cursor size
670  structCursorInfo.bVisible = (visible ? TRUE : FALSE);
671  SetConsoleCursorInfo( hConsoleOutput, &structCursorInfo );
672 #else /* _WIN32 || USE_ANSI */
673  rutil_print((visible ? ANSI_CURSOR_SHOW : ANSI_CURSOR_HIDE));
674 #endif /* _WIN32 || USE_ANSI */
675 }
676 
681 void
683 {
685 }
686 
691 void
693 {
695 }
696 
700 void
701 msleep(unsigned int ms)
702 {
703 #ifdef _WIN32
704  Sleep(ms);
705 #else
706  struct timespec ts;
707 
708  ts.tv_sec = ms / 1000;
709  ts.tv_nsec = (ms % 1000) * 1000000L;
710 
711  if(nanosleep(&ts, NULL) < 0) {
712  perror("sleep failed");
713  }
714 #endif /* _WIN32 */
715 }
716 
720 int
721 trows(void)
722 {
723 #ifdef _WIN32
724  CONSOLE_SCREEN_BUFFER_INFO csbi;
725  if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
726  return -1;
727  else
728  return csbi.srWindow.Bottom - csbi.srWindow.Top + 1; // Window height
729 #else
730 #ifdef TIOCGSIZE
731  struct ttysize ts;
732  ioctl(STDIN_FILENO, TIOCGSIZE, &ts);
733  return ts.ts_lines;
734 #elif defined(TIOCGWINSZ)
735  struct winsize ts;
736  ioctl(STDIN_FILENO, TIOCGWINSZ, &ts);
737  return ts.ws_row;
738 #else /* TIOCGSIZE */
739  return -1;
740 #endif /* TIOCGSIZE */
741 #endif /* _WIN32 */
742 }
743 
747 int
748 tcols(void)
749 {
750 #ifdef _WIN32
751  CONSOLE_SCREEN_BUFFER_INFO csbi;
752  if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
753  return -1;
754  else
755  return csbi.srWindow.Right - csbi.srWindow.Left + 1; // Window width
756 #else
757 #ifdef TIOCGSIZE
758  struct ttysize ts;
759  ioctl(STDIN_FILENO, TIOCGSIZE, &ts);
760  return ts.ts_cols;
761 #elif defined(TIOCGWINSZ)
762  struct winsize ts;
763  ioctl(STDIN_FILENO, TIOCGWINSZ, &ts);
764  return ts.ws_col;
765 #else /* TIOCGSIZE */
766  return -1;
767 #endif /* TIOCGSIZE */
768 #endif /* _WIN32 */
769 }
770 
775 #ifdef __cplusplus
776 void
777 anykey()
778 {
779  getch();
780 }
781 
782 template <class T>
783 void
784 anykey(const T& msg)
785 {
786  rutil_print(msg);
787 #else
788 void
790 {
791  if (msg)
792  rutil_print(msg);
793 #endif /* __cplusplus */
794  getch();
795 }
796 
800 void
802 {
803  const char * true_title =
804 #ifdef __cplusplus
805  title.c_str();
806 #else /* __cplusplus */
807  title;
808 #endif /* __cplusplus */
809 #if defined(_WIN32) && !defined(RUTIL_USE_ANSI)
810  SetConsoleTitleA(true_title);
811 #else
812  rutil_print(ANSI_CONSOLE_TITLE_PRE);
813  rutil_print(true_title);
814  rutil_print(ANSI_CONSOLE_TITLE_POST);
815 #endif /* defined(_WIN32) && !defined(RUTIL_USE_ANSI) */
816 }
817 
825 #ifdef __cplusplus
826 void
828 
829  resetColor();
830 }
831 
832 template <typename T, typename... F>
833 void
834 colorPrint(color_code color, color_code bgcolor, T arg, F... fmt)
835 {
836  if (color >= 0)
837  setColor(color);
838 
839  if (bgcolor >= 0)
840  setBackgroundColor(bgcolor);
841 
842  std::cout << arg << " "; /* Let me know if I should remove the space */
843  colorPrint(color, fmt...);
844 }
845 #else
846 void
847 colorPrint(color_code color, color_code bgcolor, const char *fmt, ...)
848 {
849  va_list args;
850  va_start(args, fmt);
851 
852  if (color >= 0)
853  setColor(color);
854 
855  if (bgcolor >= 0)
856  setBackgroundColor(bgcolor);
857 
858  vprintf(fmt, args);
859  va_end(args);
860 
861  resetColor();
862 }
863 #endif /* __cplusplus */
864 
868 char*
870 {
871 #ifdef _WIN32
872  /* TODO: Find a better way that doesn't use a static var. */
873  static char ret[UNLEN + 1];
874  DWORD len = UNLEN + 1;
875  if (GetUserNameA(ret, &len))
876  return ret;
877  return NULL;
878 #else /* _WIN32 */
879  return getlogin();
880 #endif
881 }
882 
888 void
889 printXY(int x, int y, RUTIL_STRING msg)
890 {
891  locate(x, y);
892  rutil_print(msg);
893 }
894 
895 #ifdef __cplusplus
896 
901 struct CursorHider {
902  CursorHider()
903  {
904  hidecursor();
905  }
906  ~CursorHider()
907  {
908  showcursor();
909  }
910 };
911 
912 } /* namespace rogueutil */
913 #endif
914 #endif /* RUTIL_H */
void setConsoleTitle(char *title)
Sets the console title given a string.
Definition: rogueutil.h:801
int nb_getch(void)
Non-blocking version of getch()
Definition: rogueutil.h:390
static void rutil_print(char *st)
Printing wrapper independent of C/C++.
Definition: rogueutil.h:278
char * getANSIColor(const int c)
Returns ANSI color escape sequence for specified number.
Definition: rogueutil.h:402
void setCursorVisibility(char visible)
Shows/hides the cursor.
Definition: rogueutil.h:664
void setString(char *str)
Prints the supplied string without advancing the cursor.
Definition: rogueutil.h:613
void printXY(int x, int y, char *msg)
Print a message at a position given by x and y.
Definition: rogueutil.h:889
void anykey(char *msg)
Waits until a key is pressed.
Definition: rogueutil.h:789
color_code
Provides easy color codes with similar numbers to QBasic.
Definition: rogueutil.h:172
char * getANSIBgColor(const int c)
Returns the ANSI background color escape sequence.
Definition: rogueutil.h:448
int tcols(void)
Returns the number of columns in the terminal or -1 on error.
Definition: rogueutil.h:748
#define RUTIL_STRING
Define to override rogueutil's string type.
Definition: rogueutil.h:41
void hidecursor(void)
Hides the cursor.
Definition: rogueutil.h:682
void setChar(char ch)
Sets the character at the cursor without advancing the cursor.
Definition: rogueutil.h:653
void setBackgroundColor(int c)
Changes the background color as specified by a number.
Definition: rogueutil.h:498
int trows(void)
Returns the number of rows in the terminal window or -1 on error.
Definition: rogueutil.h:721
void cls(void)
Clears screen, resets all attributes and moves cursor home.
Definition: rogueutil.h:555
int kbhit(void)
Determines if a button was pressed.
Definition: rogueutil.h:109
void gotoxy(int x, int y)
Sets the cursor to the specified x and y position.
Definition: rogueutil.h:140
char * getUsername(void)
Returns the username of the user running the program.
Definition: rogueutil.h:869
void msleep(unsigned int ms)
Pauses the program for a given number of milliseconds.
Definition: rogueutil.h:701
void showcursor(void)
Shows the cursor.
Definition: rogueutil.h:692
int getch(void)
Get a charater without waiting on a Return.
Definition: rogueutil.h:90
key_code
Provides keycodes for special keys.
Definition: rogueutil.h:228
void colorPrint(color_code color, color_code bgcolor, const char *fmt,...)
Prints a message in a given color.
Definition: rogueutil.h:847
int getkey(void)
Reads a key press (blocking)
Definition: rogueutil.h:293
void setColor(int c)
Changes color as specified by a number.
Definition: rogueutil.h:478
void resetColor(void)
Resets the color to one set by saveDefaultColor()
Definition: rogueutil.h:542
int saveDefaultColor(void)
Saves the color to use in resetColor() on Windows @detail Returns -1 if not on Windows or if RUTIL_US...
Definition: rogueutil.h:517
void locate(int x, int y)
Sets the cursor position to one defined by x and y.
Definition: rogueutil.h:582