26 #define RLUTIL_USE_ANSI
34 #define RLUTIL_STRING_T char*
44 static void locate(
int x,
int y);
47 static void locate(
int x,
int y);
52 #define RLUTIL_INLINE __inline
54 #define RLUTIL_INLINE __inline__
59 #include "sys/socket.h"
63 #define _NO_OLDNAMES // for MinGW compatibility
75 #include <sys/ioctl.h>
76 #include <sys/types.h>
84 struct termios oldt, newt;
86 tcgetattr(STDIN_FILENO, &oldt);
88 newt.c_lflag &= ~(ICANON |
ECHO);
89 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
91 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
100 static struct termios oldt, newt;
102 tcgetattr(STDIN_FILENO, &oldt);
104 newt.c_lflag &= ~(ICANON |
ECHO);
108 newt.c_cc[VTIME] = 1;
109 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
110 ioctl(0, FIONREAD, &cnt);
114 select(STDIN_FILENO+1, NULL, NULL, NULL, &tv);
115 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
146 #ifndef RLUTIL_STRING_T
148 #endif // RLUTIL_STRING_T
153 #ifndef RLUTIL_STRING_T
155 #endif // RLUTIL_STRING_T
157 #define RLUTIL_PRINT(st) printf("%s", st)
158 #endif // __cplusplus
200 #define _ANSI_NONE "\033[0m"
201 #define _ANSI_CLS "\033[2J"
202 #define _ANSI_BLACK "\033[22;30m"
203 #define _ANSI_RED "\033[22;31m"
204 #define _ANSI_GREEN "\033[22;32m"
205 #define _ANSI_BROWN "\033[22;33m"
206 #define _ANSI_BLUE "\033[22;34m"
207 #define _ANSI_MAGENTA "\033[22;35m"
208 #define _ANSI_CYAN "\033[22;36m"
209 #define _ANSI_GREY "\033[22;37m"
210 #define _ANSI_DARKGREY "\033[01;30m"
211 #define _ANSI_LIGHTRED "\033[01;31m"
212 #define _ANSI_LIGHTGREEN "\033[01;32m"
213 #define _ANSI_YELLOW "\033[01;33m"
214 #define _ANSI_LIGHTBLUE "\033[01;34m"
215 #define _ANSI_LIGHTMAGENTA "\033[01;35m"
216 #define _ANSI_LIGHTCYAN "\033[01;36m"
217 #define _ANSI_WHITE "\033[01;37m"
299 static const int KEY_ESCAPE = 0;
300 static const int KEY_ENTER = 1;
301 static const int KEY_SPACE = 32;
303 static const int KEY_INSERT = 2;
304 static const int KEY_HOME = 3;
305 static const int KEY_PGUP = 4;
306 static const int KEY_DELETE = 5;
307 static const int KEY_END = 6;
308 static const int KEY_PGDOWN = 7;
310 static const int KEY_UP = 14;
311 static const int KEY_DOWN = 15;
312 static const int KEY_LEFT = 16;
313 static const int KEY_RIGHT = 17;
315 static const int KEY_F1 = 18;
316 static const int KEY_F2 = 19;
317 static const int KEY_F3 = 20;
318 static const int KEY_F4 = 21;
319 static const int KEY_F5 = 22;
320 static const int KEY_F6 = 23;
321 static const int KEY_F7 = 24;
322 static const int KEY_F8 = 25;
323 static const int KEY_F9 = 26;
324 static const int KEY_F10 = 27;
325 static const int KEY_F11 = 28;
326 static const int KEY_F12 = 29;
328 static const int KEY_NUMDEL = 30;
329 static const int KEY_NUMPAD0 = 31;
330 static const int KEY_NUMPAD1 = 127;
331 static const int KEY_NUMPAD2 = 128;
332 static const int KEY_NUMPAD3 = 129;
333 static const int KEY_NUMPAD4 = 130;
334 static const int KEY_NUMPAD5 = 131;
335 static const int KEY_NUMPAD6 = 132;
336 static const int KEY_NUMPAD7 = 133;
337 static const int KEY_NUMPAD8 = 134;
338 static const int KEY_NUMPAD9 = 135;
355 switch (kk = getch()) {
356 case 71:
return KEY_NUMPAD7;
357 case 72:
return KEY_NUMPAD8;
358 case 73:
return KEY_NUMPAD9;
359 case 75:
return KEY_NUMPAD4;
360 case 77:
return KEY_NUMPAD6;
361 case 79:
return KEY_NUMPAD1;
362 case 80:
return KEY_NUMPAD4;
363 case 81:
return KEY_NUMPAD3;
364 case 82:
return KEY_NUMPAD0;
365 case 83:
return KEY_NUMDEL;
366 default:
return kk-59+KEY_F1;
370 switch (kk = getch()) {
371 case 71:
return KEY_HOME;
372 case 72:
return KEY_UP;
373 case 73:
return KEY_PGUP;
374 case 75:
return KEY_LEFT;
375 case 77:
return KEY_RIGHT;
376 case 79:
return KEY_END;
377 case 80:
return KEY_DOWN;
378 case 81:
return KEY_PGDOWN;
379 case 82:
return KEY_INSERT;
380 case 83:
return KEY_DELETE;
381 default:
return kk-123+KEY_F1;
383 case 13:
return KEY_ENTER;
385 case 27:
return KEY_ESCAPE;
390 if (cnt >= 3 && getch() ==
'[') {
391 switch (k = getch()) {
392 case 'A':
return KEY_UP;
393 case 'B':
return KEY_DOWN;
394 case 'C':
return KEY_RIGHT;
395 case 'D':
return KEY_LEFT;
397 }
else return KEY_ESCAPE;
407 if (kbhit())
return getch();
417 case BLACK :
return ANSI_BLACK;
418 case BLUE :
return ANSI_BLUE;
419 case GREEN :
return ANSI_GREEN;
420 case CYAN :
return ANSI_CYAN;
421 case RED :
return ANSI_RED;
422 case MAGENTA :
return ANSI_MAGENTA;
423 case BROWN :
return ANSI_BROWN;
424 case GREY :
return ANSI_GREY;
425 case DARKGREY :
return ANSI_DARKGREY;
429 case LIGHTRED:
return ANSI_LIGHTRED;
431 case YELLOW:
return ANSI_YELLOW;
432 case WHITE:
return ANSI_WHITE;
433 case NONE:
return ANSI_NONE;
443 #if defined(_WIN32) && !defined(RLUTIL_USE_ANSI)
444 HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
445 SetConsoleTextAttribute(hConsole, (
WORD)c);
454 #if defined(_WIN32) && !defined(RLUTIL_USE_ANSI)
465 #if defined(_WIN32) && !defined(RLUTIL_USE_ANSI)
467 coord.X = (SHORT)x-1;
468 coord.Y = (SHORT)y-1;
469 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
470 #else // _WIN32 || USE_ANSI
472 std::ostringstream oss;
473 oss <<
"\033[" << y <<
";" << x <<
"H";
477 sprintf(buf,
"\033[%d;%df", y, x);
479 #endif // __cplusplus
480 #endif // _WIN32 || USE_ANSI
486 #if defined(_WIN32) && !defined(RLUTIL_USE_ANSI)
487 HANDLE hConsoleOutput;
488 CONSOLE_CURSOR_INFO structCursorInfo;
489 hConsoleOutput = GetStdHandle( STD_OUTPUT_HANDLE );
490 GetConsoleCursorInfo( hConsoleOutput, &structCursorInfo );
491 structCursorInfo.bVisible =
FALSE;
492 SetConsoleCursorInfo( hConsoleOutput, &structCursorInfo );
493 #else // _WIN32 || USE_ANSI
495 #endif // _WIN32 || USE_ANSI
501 #if defined(_WIN32) && !defined(RLUTIL_USE_ANSI)
502 HANDLE hConsoleOutput;
503 CONSOLE_CURSOR_INFO structCursorInfo;
504 hConsoleOutput = GetStdHandle( STD_OUTPUT_HANDLE );
505 GetConsoleCursorInfo( hConsoleOutput, &structCursorInfo );
506 structCursorInfo.bVisible =
TRUE;
507 SetConsoleCursorInfo( hConsoleOutput, &structCursorInfo );
508 #else // _WIN32 || USE_ANSI
510 #endif // _WIN32 || USE_ANSI
513 extern int usleep (__useconds_t __useconds);
522 if (ms > 1000) sleep(ms/1000000);
523 #pragma GCC diagnostic push
525 usleep((ms % 1000000) * 1000);
534 CONSOLE_SCREEN_BUFFER_INFO csbi;
535 if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
538 return csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
543 ioctl(STDIN_FILENO, TIOCGSIZE, &ts);
545 #elif defined(TIOCGWINSZ)
547 ioctl(STDIN_FILENO, TIOCGWINSZ, &ts);
559 CONSOLE_SCREEN_BUFFER_INFO csbi;
560 if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
563 return csbi.srWindow.Right - csbi.srWindow.Left + 1;
568 ioctl(STDIN_FILENO, TIOCGSIZE, &ts);
570 #elif defined(TIOCGWINSZ)
572 ioctl(STDIN_FILENO, TIOCGWINSZ, &ts);
592 template <
class T>
const T&
min (
const T& a,
const T& b ) {
return (a<b)?a:b; }
594 #define min(a,b) (((a)<(b))?(a):(b))
595 #endif // __cplusplus
602 template <
class T>
const T&
max (
const T& a,
const T& b ) {
return (b<a)?a:b; }
604 #define max(a,b) (((b)<(a))?(a):(b))
605 #endif // __cplusplus
616 CursorHider() { hidecursor(); }
617 ~CursorHider() { showcursor(); }
#define ECHO
Definition: lexer-filter.c:560
char * RLUTIL_STRING_T
Defs: Internal typedefs and macros RLUTIL_STRING_T - String type depending on which one of C or C++ i...
Definition: rlutil.h:154
#define RLUTIL_INLINE
Definition: rlutil.h:54
#define FALSE
Definition: conf.h:49
#define RLUTIL_PRINT(st)
Definition: rlutil.h:157
#define WORD
Definition: grammar-filter.c:140
int usleep(__useconds_t __useconds)
#define max(a, b)
Function: max Returns the greater of the two arguments.
Definition: rlutil.h:604
#define TRUE
Definition: conf.h:48
#define min(a, b)
Function: min Returns the lesser of the two arguments.
Definition: rlutil.h:594