35 #define RUTIL_USE_ANSI 41 #define RUTIL_STRING char* 76 #include <sys/ioctl.h> 77 #include <sys/types.h> 92 struct termios oldt, newt;
94 tcgetattr(STDIN_FILENO, &oldt);
96 newt.c_lflag &= ~(ICANON | ECHO);
97 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
99 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
111 static struct termios oldt, newt;
113 tcgetattr(STDIN_FILENO, &oldt);
115 newt.c_lflag &= ~(ICANON | ECHO);
119 newt.c_cc[VTIME] = 1;
120 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
121 ioctl(0, FIONREAD, &cnt);
125 select(STDIN_FILENO+1, NULL, NULL, NULL, &tv);
126 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
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";
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";
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";
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";
302 switch (kk =
getch()) {
329 switch (kk =
getch()) {
351 return kk-123+KEY_F1;
363 if (cnt >= 3 &&
getch() ==
'[') {
364 switch (k =
getch()) {
376 }
else return KEY_ESCAPE;
422 return ANSI_DARKGREY;
424 return ANSI_LIGHTBLUE;
426 return ANSI_LIGHTGREEN;
428 return ANSI_LIGHTCYAN;
430 return ANSI_LIGHTRED;
432 return ANSI_LIGHTMAGENTA;
452 return ANSI_BACKGROUND_BLACK;
454 return ANSI_BACKGROUND_BLUE;
456 return ANSI_BACKGROUND_GREEN;
458 return ANSI_BACKGROUND_CYAN;
460 return ANSI_BACKGROUND_RED;
462 return ANSI_BACKGROUND_MAGENTA;
464 return ANSI_BACKGROUND_YELLOW;
466 return ANSI_BACKGROUND_WHITE;
480 #if defined(_WIN32) && !defined(RUTIL_USE_ANSI) 481 HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
482 CONSOLE_SCREEN_BUFFER_INFO csbi;
484 GetConsoleScreenBufferInfo(hConsole, &csbi);
486 SetConsoleTextAttribute(hConsole, (csbi.wAttributes & 0xFFF0) | (WORD)c);
500 #if defined(_WIN32) && !defined(RUTIL_USE_ANSI) 501 HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
502 CONSOLE_SCREEN_BUFFER_INFO csbi;
504 GetConsoleScreenBufferInfo(hConsole, &csbi);
506 SetConsoleTextAttribute(hConsole, (csbi.wAttributes & 0xFF0F) | (((WORD)c) << 4));
519 #if defined(_WIN32) && !defined(RUTIL_USE_ANSI) 520 static char initialized = 0;
521 static WORD attributes;
524 CONSOLE_SCREEN_BUFFER_INFO csbi;
525 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
526 attributes = csbi.wAttributes;
529 return (
int)attributes;
544 #if defined(_WIN32) && !defined(RUTIL_USE_ANSI) 545 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (WORD)
saveDefaultColor());
557 #if defined(_WIN32) && !defined(RUTIL_USE_ANSI) 559 const HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
560 const COORD coordScreen = {0, 0};
562 CONSOLE_SCREEN_BUFFER_INFO csbi;
564 GetConsoleScreenBufferInfo(hConsole, &csbi);
565 const DWORD dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
566 FillConsoleOutputCharacter(hConsole, (TCHAR)
' ', dwConSize, coordScreen, &cCharsWritten);
568 GetConsoleScreenBufferInfo(hConsole, &csbi);
569 FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
571 SetConsoleCursorPosition(hConsole, coordScreen);
584 #if defined(_WIN32) && !defined(RUTIL_USE_ANSI) 586 coord.X = (SHORT)(x - 1);
587 coord.Y = (SHORT)(y - 1);
588 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
591 std::stringstream ss;
592 ss <<
"\033[" << y <<
";" << x <<
"H";
596 sprintf(buf,
"\033[%d;%df", y, x);
609 const char *
const str = str_.data();
610 unsigned int len = static_cast<unsigned int>(str_.size());
615 unsigned int len = (
unsigned int)strlen(str);
617 #if defined(_WIN32) && !defined(RUTIL_USE_ANSI) 618 HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
619 DWORD numberOfCharsWritten;
620 CONSOLE_SCREEN_BUFFER_INFO csbi;
622 GetConsoleScreenBufferInfo(hConsoleOutput, &csbi);
625 WriteConsoleOutputCharacterA(hConsoleOutput, str,
626 len, csbi.dwCursorPosition,
627 &numberOfCharsWritten);
629 WriteConsoleOutputCharacter(hConsoleOutput, str,
630 len, csbi.dwCursorPosition,
631 &numberOfCharsWritten);
637 std::stringstream ss;
638 ss <<
"\033[" << len <<
'D';
641 char buf[3 + 20 + 1];
643 sprintf(buf,
"\033[%uD", len);
655 const char buf[] = {ch, 0};
666 #if defined(_WIN32) && !defined(RUTIL_USE_ANSI) 667 HANDLE hConsoleOutput = GetStdHandle( STD_OUTPUT_HANDLE );
668 CONSOLE_CURSOR_INFO structCursorInfo;
669 GetConsoleCursorInfo( hConsoleOutput, &structCursorInfo );
670 structCursorInfo.bVisible = (visible ? TRUE : FALSE);
671 SetConsoleCursorInfo( hConsoleOutput, &structCursorInfo );
673 rutil_print((visible ? ANSI_CURSOR_SHOW : ANSI_CURSOR_HIDE));
708 ts.tv_sec = ms / 1000;
709 ts.tv_nsec = (ms % 1000) * 1000000L;
711 if(nanosleep(&ts, NULL) < 0) {
712 perror(
"sleep failed");
724 CONSOLE_SCREEN_BUFFER_INFO csbi;
725 if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
728 return csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
732 ioctl(STDIN_FILENO, TIOCGSIZE, &ts);
734 #elif defined(TIOCGWINSZ) 736 ioctl(STDIN_FILENO, TIOCGWINSZ, &ts);
751 CONSOLE_SCREEN_BUFFER_INFO csbi;
752 if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
755 return csbi.srWindow.Right - csbi.srWindow.Left + 1;
759 ioctl(STDIN_FILENO, TIOCGSIZE, &ts);
761 #elif defined(TIOCGWINSZ) 763 ioctl(STDIN_FILENO, TIOCGWINSZ, &ts);
803 const char * true_title =
809 #if defined(_WIN32) && !defined(RUTIL_USE_ANSI) 810 SetConsoleTitleA(true_title);
832 template <
typename T,
typename... F>
842 std::cout << arg <<
" ";
873 static char ret[UNLEN + 1];
874 DWORD len = UNLEN + 1;
875 if (GetUserNameA(ret, &len))
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