key-tester.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env node
  2. import { matchesKey } from "../src/keys.ts";
  3. import { ProcessTerminal } from "../src/terminal.ts";
  4. import { type Component, TUI } from "../src/tui.ts";
  5. import { truncateToWidth } from "../src/utils.ts";
  6. /**
  7. * Simple key code logger component
  8. */
  9. class KeyLogger implements Component {
  10. private log: string[] = [];
  11. private maxLines = 20;
  12. private tui: TUI;
  13. private terminal: ProcessTerminal;
  14. constructor(tui: TUI, terminal: ProcessTerminal) {
  15. this.tui = tui;
  16. this.terminal = terminal;
  17. }
  18. handleInput(data: string): void {
  19. // Handle Ctrl+C (raw or Kitty protocol) for exit
  20. if (matchesKey(data, "ctrl+c")) {
  21. this.tui.stop();
  22. console.log("\nExiting...");
  23. process.exit(0);
  24. }
  25. // Convert to various representations
  26. const hex = Buffer.from(data).toString("hex");
  27. const charCodes = Array.from(data)
  28. .map((c) => c.charCodeAt(0))
  29. .join(", ");
  30. const repr = data
  31. .replace(/\x1b/g, "\\x1b")
  32. .replace(/\r/g, "\\r")
  33. .replace(/\n/g, "\\n")
  34. .replace(/\t/g, "\\t")
  35. .replace(/\x7f/g, "\\x7f");
  36. const logLine = `Hex: ${hex.padEnd(20)} | Chars: [${charCodes.padEnd(15)}] | Repr: "${repr}"`;
  37. this.log.push(logLine);
  38. // Keep only last N lines
  39. if (this.log.length > this.maxLines) {
  40. this.log.shift();
  41. }
  42. // Request re-render to show the new log entry
  43. this.tui.requestRender();
  44. }
  45. invalidate(): void {
  46. // No cached state to invalidate currently
  47. }
  48. private protocolName(): string {
  49. if (this.terminal.kittyProtocolActive) return "kitty";
  50. if (this.terminal.modifyOtherKeysActive) return "modifyOtherKeys";
  51. return "legacy";
  52. }
  53. private fit(line: string, width: number): string {
  54. return truncateToWidth(line, width).padEnd(width);
  55. }
  56. render(width: number): string[] {
  57. const lines: string[] = [];
  58. // Title
  59. lines.push("=".repeat(width));
  60. lines.push(this.fit("Key Code Tester - Press keys to see their codes (Ctrl+C to exit)", width));
  61. lines.push(this.fit(`Protocol: ${this.protocolName()}`, width));
  62. lines.push("=".repeat(width));
  63. lines.push("");
  64. // Log entries
  65. for (const entry of this.log) {
  66. lines.push(this.fit(entry, width));
  67. }
  68. // Fill remaining space
  69. const remaining = Math.max(0, 25 - lines.length);
  70. for (let i = 0; i < remaining; i++) {
  71. lines.push("".padEnd(width));
  72. }
  73. // Footer
  74. lines.push("=".repeat(width));
  75. lines.push(this.fit("Test these:", width));
  76. lines.push(this.fit(" - Shift + Enter (should show: \\x1b[13;2u with Kitty protocol)", width));
  77. lines.push(this.fit(" - Alt/Option + Enter", width));
  78. lines.push(this.fit(" - Option/Alt + Backspace", width));
  79. lines.push(this.fit(" - Cmd/Ctrl + Backspace", width));
  80. lines.push(this.fit(" - Regular Backspace", width));
  81. lines.push("=".repeat(width));
  82. return lines;
  83. }
  84. }
  85. // Set up TUI
  86. const terminal = new ProcessTerminal();
  87. const tui = new TUI(terminal);
  88. const logger = new KeyLogger(tui, terminal);
  89. tui.addChild(logger);
  90. tui.setFocus(logger);
  91. // Handle Ctrl+C for clean exit (SIGINT still works for raw mode)
  92. process.on("SIGINT", () => {
  93. tui.stop();
  94. console.log("\nExiting...");
  95. process.exit(0);
  96. });
  97. // Start the TUI
  98. tui.start();
  99. // Protocol negotiation completes asynchronously after the first render.
  100. // Refresh briefly/continuously so the displayed protocol state is not stale.
  101. setInterval(() => tui.requestRender(), 100);