TerminalTests.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. }
  54. switch(c) {
  55. KEY_CASE(TERMINAL_KEY_ARROW_LEFT);
  56. KEY_CASE(TERMINAL_KEY_ARROW_RIGHT);
  57. KEY_CASE(TERMINAL_KEY_ARROW_UP);
  58. KEY_CASE(TERMINAL_KEY_ARROW_DOWN);
  59. KEY_CASE(TERMINAL_KEY_DELETE);
  60. default: printf("%lu\n", c); break;
  61. }
  62. }
  63. leaveRawTerminal();
  64. leaveAlternativeTerminal();
  65. }