| 1 |
#pragma once |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#if 0 |
| 22 |
#define RLUTIL_USE_ANSI |
| 23 |
#endif |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#if 0 |
| 30 |
#define RLUTIL_STRING_T char* |
| 31 |
#endif |
| 32 |
|
| 33 |
#ifndef RLUTIL_INLINE |
| 34 |
#ifdef _MSC_VER |
| 35 |
#define RLUTIL_INLINE __inline |
| 36 |
#else |
| 37 |
#define RLUTIL_INLINE static __inline__ |
| 38 |
#endif |
| 39 |
#endif |
| 40 |
|
| 41 |
#ifdef __cplusplus |
| 42 |
|
| 43 |
#include <iostream> |
| 44 |
#include <string> |
| 45 |
#include <cstdio> |
| 46 |
|
| 47 |
namespace rlutil { |
| 48 |
RLUTIL_INLINE void locate(int x, int y); |
| 49 |
} |
| 50 |
#else |
| 51 |
#include <stdio.h> |
| 52 |
#include <string.h> |
| 53 |
RLUTIL_INLINE void locate(int x, int y); |
| 54 |
#endif |
| 55 |
|
| 56 |
#ifdef _WIN32 |
| 57 |
#include <windows.h> |
| 58 |
#define _NO_OLDNAMES |
| 59 |
#include <conio.h> |
| 60 |
#define getch _getch |
| 61 |
#define kbhit _kbhit |
| 62 |
#else |
| 63 |
#include <termios.h> |
| 64 |
#include <unistd.h> |
| 65 |
#include <sys/ioctl.h> |
| 66 |
#include <sys/types.h> |
| 67 |
#include <sys/time.h> |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
RLUTIL_INLINE int getch(void) { |
| 73 |
|
| 74 |
struct termios oldt, newt; |
| 75 |
int ch; |
| 76 |
tcgetattr(STDIN_FILENO, &oldt); |
| 77 |
newt = oldt; |
| 78 |
newt.c_lflag &= ~(ICANON | ECHO); |
| 79 |
tcsetattr(STDIN_FILENO, TCSANOW, &newt); |
| 80 |
ch = getchar(); |
| 81 |
tcsetattr(STDIN_FILENO, TCSANOW, &oldt); |
| 82 |
return ch; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
RLUTIL_INLINE int kbhit(void) { |
| 89 |
|
| 90 |
static struct termios oldt, newt; |
| 91 |
int cnt = 0; |
| 92 |
tcgetattr(STDIN_FILENO, &oldt); |
| 93 |
newt = oldt; |
| 94 |
newt.c_lflag &= ~(ICANON | ECHO); |
| 95 |
newt.c_iflag = 0; |
| 96 |
newt.c_oflag = 0; |
| 97 |
newt.c_cc[VMIN] = 1; |
| 98 |
newt.c_cc[VTIME] = 1; |
| 99 |
tcsetattr(STDIN_FILENO, TCSANOW, &newt); |
| 100 |
ioctl(0, FIONREAD, &cnt); |
| 101 |
struct timeval tv; |
| 102 |
tv.tv_sec = 0; |
| 103 |
tv.tv_usec = 100; |
| 104 |
select(STDIN_FILENO+1, NULL, NULL, NULL, &tv); |
| 105 |
tcsetattr(STDIN_FILENO, TCSANOW, &oldt); |
| 106 |
return cnt; |
| 107 |
} |
| 108 |
#endif |
| 109 |
|
| 110 |
#ifndef gotoxy |
| 111 |
|
| 112 |
|
| 113 |
RLUTIL_INLINE void gotoxy(int x, int y) { |
| 114 |
#ifdef __cplusplus |
| 115 |
rlutil:: |
| 116 |
#endif |
| 117 |
locate(x,y); |
| 118 |
} |
| 119 |
#endif |
| 120 |
|
| 121 |
#ifdef __cplusplus |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
namespace rlutil { |
| 127 |
#endif |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
#ifdef __cplusplus |
| 136 |
#ifndef RLUTIL_STRING_T |
| 137 |
typedef std::string RLUTIL_STRING_T; |
| 138 |
#endif |
| 139 |
|
| 140 |
#define RLUTIL_PRINT(st) do { std::cout << st; } while(false) |
| 141 |
#else |
| 142 |
#ifndef RLUTIL_STRING_T |
| 143 |
typedef const char* RLUTIL_STRING_T; |
| 144 |
#endif |
| 145 |
|
| 146 |
#define RLUTIL_PRINT(st) printf("%s", st) |
| 147 |
#endif |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
enum { |
| 170 |
BLACK, |
| 171 |
BLUE, |
| 172 |
GREEN, |
| 173 |
CYAN, |
| 174 |
RED, |
| 175 |
MAGENTA, |
| 176 |
BROWN, |
| 177 |
GREY, |
| 178 |
DARKGREY, |
| 179 |
LIGHTBLUE, |
| 180 |
LIGHTGREEN, |
| 181 |
LIGHTCYAN, |
| 182 |
LIGHTRED, |
| 183 |
LIGHTMAGENTA, |
| 184 |
YELLOW, |
| 185 |
WHITE |
| 186 |
}; |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
static const RLUTIL_STRING_T ANSI_CLS = "\033[2J\033[3J"; |
| 224 |
static const RLUTIL_STRING_T ANSI_CONSOLE_TITLE_PRE = "\033]0;"; |
| 225 |
static const RLUTIL_STRING_T ANSI_CONSOLE_TITLE_POST = "\007"; |
| 226 |
static const RLUTIL_STRING_T ANSI_ATTRIBUTE_RESET = "\033[0m"; |
| 227 |
static const RLUTIL_STRING_T ANSI_CURSOR_HIDE = "\033[?25l"; |
| 228 |
static const RLUTIL_STRING_T ANSI_CURSOR_SHOW = "\033[?25h"; |
| 229 |
static const RLUTIL_STRING_T ANSI_CURSOR_HOME = "\033[H"; |
| 230 |
static const RLUTIL_STRING_T ANSI_BLACK = "\033[22;30m"; |
| 231 |
static const RLUTIL_STRING_T ANSI_RED = "\033[22;31m"; |
| 232 |
static const RLUTIL_STRING_T ANSI_GREEN = "\033[22;32m"; |
| 233 |
static const RLUTIL_STRING_T ANSI_BROWN = "\033[22;33m"; |
| 234 |
static const RLUTIL_STRING_T ANSI_BLUE = "\033[22;34m"; |
| 235 |
static const RLUTIL_STRING_T ANSI_MAGENTA = "\033[22;35m"; |
| 236 |
static const RLUTIL_STRING_T ANSI_CYAN = "\033[22;36m"; |
| 237 |
static const RLUTIL_STRING_T ANSI_GREY = "\033[22;37m"; |
| 238 |
static const RLUTIL_STRING_T ANSI_DARKGREY = "\033[01;30m"; |
| 239 |
static const RLUTIL_STRING_T ANSI_LIGHTRED = "\033[01;31m"; |
| 240 |
static const RLUTIL_STRING_T ANSI_LIGHTGREEN = "\033[01;32m"; |
| 241 |
static const RLUTIL_STRING_T ANSI_YELLOW = "\033[01;33m"; |
| 242 |
static const RLUTIL_STRING_T ANSI_LIGHTBLUE = "\033[01;34m"; |
| 243 |
static const RLUTIL_STRING_T ANSI_LIGHTMAGENTA = "\033[01;35m"; |
| 244 |
static const RLUTIL_STRING_T ANSI_LIGHTCYAN = "\033[01;36m"; |
| 245 |
static const RLUTIL_STRING_T ANSI_WHITE = "\033[01;37m"; |
| 246 |
static const RLUTIL_STRING_T ANSI_BACKGROUND_BLACK = "\033[40m"; |
| 247 |
static const RLUTIL_STRING_T ANSI_BACKGROUND_RED = "\033[41m"; |
| 248 |
static const RLUTIL_STRING_T ANSI_BACKGROUND_GREEN = "\033[42m"; |
| 249 |
static const RLUTIL_STRING_T ANSI_BACKGROUND_YELLOW = "\033[43m"; |
| 250 |
static const RLUTIL_STRING_T ANSI_BACKGROUND_BLUE = "\033[44m"; |
| 251 |
static const RLUTIL_STRING_T ANSI_BACKGROUND_MAGENTA = "\033[45m"; |
| 252 |
static const RLUTIL_STRING_T ANSI_BACKGROUND_CYAN = "\033[46m"; |
| 253 |
static const RLUTIL_STRING_T ANSI_BACKGROUND_WHITE = "\033[47m"; |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
enum { |
| 297 |
KEY_ESCAPE = 0, |
| 298 |
KEY_ENTER = 1, |
| 299 |
KEY_SPACE = 32, |
| 300 |
|
| 301 |
KEY_INSERT = 2, |
| 302 |
KEY_HOME = 3, |
| 303 |
KEY_PGUP = 4, |
| 304 |
KEY_DELETE = 5, |
| 305 |
KEY_END = 6, |
| 306 |
KEY_PGDOWN = 7, |
| 307 |
|
| 308 |
KEY_UP = 14, |
| 309 |
KEY_DOWN = 15, |
| 310 |
KEY_LEFT = 16, |
| 311 |
KEY_RIGHT = 17, |
| 312 |
|
| 313 |
KEY_F1 = 18, |
| 314 |
KEY_F2 = 19, |
| 315 |
KEY_F3 = 20, |
| 316 |
KEY_F4 = 21, |
| 317 |
KEY_F5 = 22, |
| 318 |
KEY_F6 = 23, |
| 319 |
KEY_F7 = 24, |
| 320 |
KEY_F8 = 25, |
| 321 |
KEY_F9 = 26, |
| 322 |
KEY_F10 = 27, |
| 323 |
KEY_F11 = 28, |
| 324 |
KEY_F12 = 29, |
| 325 |
|
| 326 |
KEY_NUMDEL = 30, |
| 327 |
KEY_NUMPAD0 = 31, |
| 328 |
KEY_NUMPAD1 = 127, |
| 329 |
KEY_NUMPAD2 = 128, |
| 330 |
KEY_NUMPAD3 = 129, |
| 331 |
KEY_NUMPAD4 = 130, |
| 332 |
KEY_NUMPAD5 = 131, |
| 333 |
KEY_NUMPAD6 = 132, |
| 334 |
KEY_NUMPAD7 = 133, |
| 335 |
KEY_NUMPAD8 = 134, |
| 336 |
KEY_NUMPAD9 = 135 |
| 337 |
}; |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
RLUTIL_INLINE int getkey(void) { |
| 347 |
#ifndef _WIN32 |
| 348 |
int cnt = kbhit(); |
| 349 |
#endif |
| 350 |
int k = getch(); |
| 351 |
switch(k) { |
| 352 |
case 0: { |
| 353 |
int kk; |
| 354 |
switch (kk = getch()) { |
| 355 |
case 71: return KEY_NUMPAD7; |
| 356 |
case 72: return KEY_NUMPAD8; |
| 357 |
case 73: return KEY_NUMPAD9; |
| 358 |
case 75: return KEY_NUMPAD4; |
| 359 |
case 77: return KEY_NUMPAD6; |
| 360 |
case 79: return KEY_NUMPAD1; |
| 361 |
case 80: return KEY_NUMPAD2; |
| 362 |
case 81: return KEY_NUMPAD3; |
| 363 |
case 82: return KEY_NUMPAD0; |
| 364 |
case 83: return KEY_NUMDEL; |
| 365 |
default: return kk-59+KEY_F1; |
| 366 |
}} |
| 367 |
case 224: { |
| 368 |
int kk; |
| 369 |
switch (kk = getch()) { |
| 370 |
case 71: return KEY_HOME; |
| 371 |
case 72: return KEY_UP; |
| 372 |
case 73: return KEY_PGUP; |
| 373 |
case 75: return KEY_LEFT; |
| 374 |
case 77: return KEY_RIGHT; |
| 375 |
case 79: return KEY_END; |
| 376 |
case 80: return KEY_DOWN; |
| 377 |
case 81: return KEY_PGDOWN; |
| 378 |
case 82: return KEY_INSERT; |
| 379 |
case 83: return KEY_DELETE; |
| 380 |
default: return kk-123+KEY_F1; |
| 381 |
}} |
| 382 |
case 13: return KEY_ENTER; |
| 383 |
#ifdef _WIN32 |
| 384 |
case 27: return KEY_ESCAPE; |
| 385 |
#else |
| 386 |
case 155: |
| 387 |
case 27: { |
| 388 |
|
| 389 |
if (cnt >= 3 && getch() == '[') { |
| 390 |
switch (k = getch()) { |
| 391 |
case 'A': return KEY_UP; |
| 392 |
case 'B': return KEY_DOWN; |
| 393 |
case 'C': return KEY_RIGHT; |
| 394 |
case 'D': return KEY_LEFT; |
| 395 |
} |
| 396 |
} else return KEY_ESCAPE; |
| 397 |
} |
| 398 |
#endif |
| 399 |
default: return k; |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
RLUTIL_INLINE int nb_getch(void) { |
| 406 |
if (kbhit()) return getch(); |
| 407 |
else return 0; |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
RLUTIL_INLINE RLUTIL_STRING_T getANSIColor(const int c) { |
| 415 |
switch (c) { |
| 416 |
case BLACK : return ANSI_BLACK; |
| 417 |
case BLUE : return ANSI_BLUE; |
| 418 |
case GREEN : return ANSI_GREEN; |
| 419 |
case CYAN : return ANSI_CYAN; |
| 420 |
case RED : return ANSI_RED; |
| 421 |
case MAGENTA : return ANSI_MAGENTA; |
| 422 |
case BROWN : return ANSI_BROWN; |
| 423 |
case GREY : return ANSI_GREY; |
| 424 |
case DARKGREY : return ANSI_DARKGREY; |
| 425 |
case LIGHTBLUE : return ANSI_LIGHTBLUE; |
| 426 |
case LIGHTGREEN : return ANSI_LIGHTGREEN; |
| 427 |
case LIGHTCYAN : return ANSI_LIGHTCYAN; |
| 428 |
case LIGHTRED : return ANSI_LIGHTRED; |
| 429 |
case LIGHTMAGENTA: return ANSI_LIGHTMAGENTA; |
| 430 |
case YELLOW : return ANSI_YELLOW; |
| 431 |
case WHITE : return ANSI_WHITE; |
| 432 |
default: return ""; |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
RLUTIL_INLINE RLUTIL_STRING_T getANSIBackgroundColor(const int c) { |
| 441 |
switch (c) { |
| 442 |
case BLACK : return ANSI_BACKGROUND_BLACK; |
| 443 |
case BLUE : return ANSI_BACKGROUND_BLUE; |
| 444 |
case GREEN : return ANSI_BACKGROUND_GREEN; |
| 445 |
case CYAN : return ANSI_BACKGROUND_CYAN; |
| 446 |
case RED : return ANSI_BACKGROUND_RED; |
| 447 |
case MAGENTA: return ANSI_BACKGROUND_MAGENTA; |
| 448 |
case BROWN : return ANSI_BACKGROUND_YELLOW; |
| 449 |
case GREY : return ANSI_BACKGROUND_WHITE; |
| 450 |
default: return ""; |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
RLUTIL_INLINE void setColor(int c) { |
| 460 |
#if defined(_WIN32) && !defined(RLUTIL_USE_ANSI) |
| 461 |
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); |
| 462 |
CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 463 |
|
| 464 |
GetConsoleScreenBufferInfo(hConsole, &csbi); |
| 465 |
|
| 466 |
SetConsoleTextAttribute(hConsole, (csbi.wAttributes & 0xFFF0) | (WORD)c); |
| 467 |
#else |
| 468 |
RLUTIL_PRINT(getANSIColor(c)); |
| 469 |
#endif |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
RLUTIL_INLINE void setBackgroundColor(int c) { |
| 478 |
#if defined(_WIN32) && !defined(RLUTIL_USE_ANSI) |
| 479 |
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); |
| 480 |
CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 481 |
|
| 482 |
GetConsoleScreenBufferInfo(hConsole, &csbi); |
| 483 |
|
| 484 |
SetConsoleTextAttribute(hConsole, (csbi.wAttributes & 0xFF0F) | (((WORD)c) << 4)); |
| 485 |
#else |
| 486 |
RLUTIL_PRINT(getANSIBackgroundColor(c)); |
| 487 |
#endif |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
RLUTIL_INLINE int saveDefaultColor(void) { |
| 497 |
#if defined(_WIN32) && !defined(RLUTIL_USE_ANSI) |
| 498 |
static char initialized = 0; |
| 499 |
static WORD attributes; |
| 500 |
|
| 501 |
if (!initialized) { |
| 502 |
CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 503 |
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); |
| 504 |
attributes = csbi.wAttributes; |
| 505 |
initialized = 1; |
| 506 |
} |
| 507 |
return (int)attributes; |
| 508 |
#else |
| 509 |
return -1; |
| 510 |
#endif |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
RLUTIL_INLINE void resetColor(void) { |
| 521 |
#if defined(_WIN32) && !defined(RLUTIL_USE_ANSI) |
| 522 |
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (WORD)saveDefaultColor()); |
| 523 |
#else |
| 524 |
RLUTIL_PRINT(ANSI_ATTRIBUTE_RESET); |
| 525 |
#endif |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
RLUTIL_INLINE void cls(void) { |
| 531 |
#if defined(_WIN32) && !defined(RLUTIL_USE_ANSI) |
| 532 |
|
| 533 |
const HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); |
| 534 |
const COORD coordScreen = {0, 0}; |
| 535 |
DWORD cCharsWritten; |
| 536 |
CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 537 |
|
| 538 |
GetConsoleScreenBufferInfo(hConsole, &csbi); |
| 539 |
const DWORD dwConSize = csbi.dwSize.X * csbi.dwSize.Y; |
| 540 |
FillConsoleOutputCharacter(hConsole, (TCHAR)' ', dwConSize, coordScreen, &cCharsWritten); |
| 541 |
|
| 542 |
GetConsoleScreenBufferInfo(hConsole, &csbi); |
| 543 |
FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten); |
| 544 |
|
| 545 |
SetConsoleCursorPosition(hConsole, coordScreen); |
| 546 |
#else |
| 547 |
RLUTIL_PRINT(ANSI_CLS); |
| 548 |
RLUTIL_PRINT(ANSI_CURSOR_HOME); |
| 549 |
#endif |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
RLUTIL_INLINE void locate(int x, int y) { |
| 555 |
#if defined(_WIN32) && !defined(RLUTIL_USE_ANSI) |
| 556 |
COORD coord; |
| 557 |
|
| 558 |
coord.X = (SHORT)(x - 1); |
| 559 |
coord.Y = (SHORT)(y - 1); |
| 560 |
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); |
| 561 |
#else |
| 562 |
#ifdef __cplusplus |
| 563 |
RLUTIL_PRINT("\033[" << y << ";" << x << "H"); |
| 564 |
#else |
| 565 |
char buf[32]; |
| 566 |
sprintf(buf, "\033[%d;%df", y, x); |
| 567 |
RLUTIL_PRINT(buf); |
| 568 |
#endif |
| 569 |
#endif |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
#ifdef __cplusplus |
| 575 |
RLUTIL_INLINE void setString(const RLUTIL_STRING_T & str_) { |
| 576 |
const char * const str = str_.data(); |
| 577 |
unsigned int len = str_.size(); |
| 578 |
#else |
| 579 |
RLUTIL_INLINE void setString(RLUTIL_STRING_T str) { |
| 580 |
unsigned int len = strlen(str); |
| 581 |
#endif |
| 582 |
#if defined(_WIN32) && !defined(RLUTIL_USE_ANSI) |
| 583 |
HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); |
| 584 |
DWORD numberOfCharsWritten; |
| 585 |
CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 586 |
|
| 587 |
GetConsoleScreenBufferInfo(hConsoleOutput, &csbi); |
| 588 |
WriteConsoleOutputCharacter(hConsoleOutput, str, len, csbi.dwCursorPosition, &numberOfCharsWritten); |
| 589 |
#else |
| 590 |
RLUTIL_PRINT(str); |
| 591 |
#ifdef __cplusplus |
| 592 |
RLUTIL_PRINT("\033[" << len << 'D'); |
| 593 |
#else |
| 594 |
char buf[3 + 20 + 1]; |
| 595 |
sprintf(buf, "\033[%uD", len); |
| 596 |
RLUTIL_PRINT(buf); |
| 597 |
#endif |
| 598 |
#endif |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
RLUTIL_INLINE void setChar(char ch) { |
| 604 |
const char buf[] = {ch, 0}; |
| 605 |
setString(buf); |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
RLUTIL_INLINE void setCursorVisibility(char visible) { |
| 611 |
#if defined(_WIN32) && !defined(RLUTIL_USE_ANSI) |
| 612 |
HANDLE hConsoleOutput = GetStdHandle( STD_OUTPUT_HANDLE ); |
| 613 |
CONSOLE_CURSOR_INFO structCursorInfo; |
| 614 |
GetConsoleCursorInfo( hConsoleOutput, &structCursorInfo ); |
| 615 |
structCursorInfo.bVisible = (visible ? TRUE : FALSE); |
| 616 |
SetConsoleCursorInfo( hConsoleOutput, &structCursorInfo ); |
| 617 |
#else |
| 618 |
RLUTIL_PRINT((visible ? ANSI_CURSOR_SHOW : ANSI_CURSOR_HIDE)); |
| 619 |
#endif |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
RLUTIL_INLINE void hidecursor(void) { |
| 625 |
setCursorVisibility(0); |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
RLUTIL_INLINE void showcursor(void) { |
| 631 |
setCursorVisibility(1); |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
RLUTIL_INLINE void msleep(unsigned int ms) { |
| 637 |
#ifdef _WIN32 |
| 638 |
Sleep(ms); |
| 639 |
#else |
| 640 |
|
| 641 |
if (ms > 1000) sleep(ms/1000000); |
| 642 |
usleep((ms % 1000000) * 1000); |
| 643 |
#endif |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
RLUTIL_INLINE int trows(void) { |
| 649 |
#ifdef _WIN32 |
| 650 |
CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 651 |
if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) |
| 652 |
return -1; |
| 653 |
else |
| 654 |
return csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 655 |
|
| 656 |
#else |
| 657 |
#ifdef TIOCGSIZE |
| 658 |
struct ttysize ts; |
| 659 |
ioctl(STDIN_FILENO, TIOCGSIZE, &ts); |
| 660 |
return ts.ts_lines; |
| 661 |
#elif defined(TIOCGWINSZ) |
| 662 |
struct winsize ts; |
| 663 |
ioctl(STDIN_FILENO, TIOCGWINSZ, &ts); |
| 664 |
return ts.ws_row; |
| 665 |
#else |
| 666 |
return -1; |
| 667 |
#endif |
| 668 |
#endif |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
RLUTIL_INLINE int tcols(void) { |
| 674 |
#ifdef _WIN32 |
| 675 |
CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 676 |
if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) |
| 677 |
return -1; |
| 678 |
else |
| 679 |
return csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 680 |
|
| 681 |
#else |
| 682 |
#ifdef TIOCGSIZE |
| 683 |
struct ttysize ts; |
| 684 |
ioctl(STDIN_FILENO, TIOCGSIZE, &ts); |
| 685 |
return ts.ts_cols; |
| 686 |
#elif defined(TIOCGWINSZ) |
| 687 |
struct winsize ts; |
| 688 |
ioctl(STDIN_FILENO, TIOCGWINSZ, &ts); |
| 689 |
return ts.ws_col; |
| 690 |
#else |
| 691 |
return -1; |
| 692 |
#endif |
| 693 |
#endif |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
#ifdef __cplusplus |
| 705 |
RLUTIL_INLINE void anykey() { |
| 706 |
getch(); |
| 707 |
} |
| 708 |
|
| 709 |
template <class T> void anykey(const T& msg) { |
| 710 |
RLUTIL_PRINT(msg); |
| 711 |
#else |
| 712 |
RLUTIL_INLINE void anykey(RLUTIL_STRING_T msg) { |
| 713 |
if (msg) |
| 714 |
RLUTIL_PRINT(msg); |
| 715 |
#endif |
| 716 |
getch(); |
| 717 |
} |
| 718 |
|
| 719 |
RLUTIL_INLINE void setConsoleTitle(RLUTIL_STRING_T title) { |
| 720 |
const char * true_title = |
| 721 |
#ifdef __cplusplus |
| 722 |
title.c_str(); |
| 723 |
#else |
| 724 |
title; |
| 725 |
#endif |
| 726 |
#if defined(_WIN32) && !defined(RLUTIL_USE_ANSI) |
| 727 |
SetConsoleTitleA(true_title); |
| 728 |
#else |
| 729 |
RLUTIL_PRINT(ANSI_CONSOLE_TITLE_PRE); |
| 730 |
RLUTIL_PRINT(true_title); |
| 731 |
RLUTIL_PRINT(ANSI_CONSOLE_TITLE_POST); |
| 732 |
#endif |
| 733 |
} |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
#ifdef __cplusplus |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
struct CursorHider { |
| 743 |
CursorHider() { hidecursor(); } |
| 744 |
~CursorHider() { showcursor(); } |
| 745 |
}; |
| 746 |
|
| 747 |
} |
| 748 |
#endif |