TerminalTests.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <stdio.h>
  2. #include "../Tests.h"
  3. #include "core/Logger.h"
  4. #include "core/Terminal.h"
  5. #include "core/Utility.h"
  6. #define KEY_CASE(key) \
  7. case key: puts(#key); break
  8. void testTerminal(bool tty) {
  9. IntVector2 size = getTerminalSize();
  10. if(tty) {
  11. TEST_TRUE(size.x > 0);
  12. TEST_TRUE(size.y > 0);
  13. TEST_FALSE(enterRawTerminal());
  14. TEST_FALSE(leaveRawTerminal());
  15. } else {
  16. TEST_INT(0, size.x);
  17. TEST_INT(0, size.y);
  18. }
  19. enterAlternativeTerminal();
  20. clearTerminal();
  21. resetCursor();
  22. LOG_ERROR("Not visible!");
  23. leaveAlternativeTerminal();
  24. LOG_ERROR("Not visible!");
  25. moveCursorUp(2);
  26. moveCursorDown(1);
  27. moveCursorLeft(3);
  28. moveCursorRight(3);
  29. clearTerminalLine();
  30. hideCursor();
  31. showCursor();
  32. }
  33. void testInteractiveTerminal(void) {
  34. enterAlternativeTerminal();
  35. resetCursor();
  36. hideCursor();
  37. puts("Hi there");
  38. LOG_WARNING("This is a test");
  39. IntVector2 v = getTerminalSize();
  40. printf("%d %d\n", v.x, v.y);
  41. clearTerminal();
  42. resetCursor();
  43. showCursor();
  44. puts("the final!!!");
  45. sleepMillis(500);
  46. enterRawTerminal();
  47. while(true) {
  48. u64 c = getRawChar();
  49. if(c == 'q') {
  50. break;
  51. } else if(c == 0) {
  52. continue;
  53. } else if(isSpecialChar(c)) {
  54. SpecialChar sc = convertToSpecialChar(c);
  55. if(sc.shift) {
  56. printf("SHIFT + ");
  57. }
  58. if(sc.control) {
  59. printf("CTRL + ");
  60. }
  61. if(sc.alt) {
  62. printf("ALT + ");
  63. }
  64. switch(sc.key) {
  65. KEY_CASE(TERMINAL_KEY_ARROW_LEFT);
  66. KEY_CASE(TERMINAL_KEY_ARROW_RIGHT);
  67. KEY_CASE(TERMINAL_KEY_ARROW_UP);
  68. KEY_CASE(TERMINAL_KEY_ARROW_DOWN);
  69. KEY_CASE(TERMINAL_KEY_DELETE);
  70. KEY_CASE(TERMINAL_KEY_F1);
  71. KEY_CASE(TERMINAL_KEY_F2);
  72. KEY_CASE(TERMINAL_KEY_F3);
  73. KEY_CASE(TERMINAL_KEY_F4);
  74. KEY_CASE(TERMINAL_KEY_F5);
  75. KEY_CASE(TERMINAL_KEY_F6);
  76. KEY_CASE(TERMINAL_KEY_F7);
  77. KEY_CASE(TERMINAL_KEY_F8);
  78. KEY_CASE(TERMINAL_KEY_F9);
  79. KEY_CASE(TERMINAL_KEY_F10);
  80. KEY_CASE(TERMINAL_KEY_F11);
  81. KEY_CASE(TERMINAL_KEY_F12);
  82. KEY_CASE(TERMINAL_KEY_PAGE_UP);
  83. KEY_CASE(TERMINAL_KEY_PAGE_DOWN);
  84. KEY_CASE(TERMINAL_KEY_HOME);
  85. KEY_CASE(TERMINAL_KEY_END);
  86. default: printf("%lu\n", sc.key); break;
  87. }
  88. } else {
  89. printf("%lu\n", c);
  90. }
  91. }
  92. leaveRawTerminal();
  93. leaveAlternativeTerminal();
  94. }