image-test.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { readFileSync } from "fs";
  2. import { Image } from "../src/components/image.ts";
  3. import { Spacer } from "../src/components/spacer.ts";
  4. import { Text } from "../src/components/text.ts";
  5. import { ProcessTerminal } from "../src/terminal.ts";
  6. import { getCapabilities, getImageDimensions } from "../src/terminal-image.ts";
  7. import { TUI } from "../src/tui.ts";
  8. const testImagePath = process.argv[2] || "/tmp/test-image.png";
  9. console.log("Terminal capabilities:", getCapabilities());
  10. console.log("Loading image from:", testImagePath);
  11. let imageBuffer: Buffer;
  12. try {
  13. imageBuffer = readFileSync(testImagePath);
  14. } catch (_e) {
  15. console.error(`Failed to load image: ${testImagePath}`);
  16. console.error("Usage: npx tsx test/image-test.ts [path-to-image.png]");
  17. process.exit(1);
  18. }
  19. const base64Data = imageBuffer.toString("base64");
  20. const dims = getImageDimensions(base64Data, "image/png");
  21. console.log("Image dimensions:", dims);
  22. console.log("");
  23. const terminal = new ProcessTerminal();
  24. const tui = new TUI(terminal);
  25. tui.addChild(new Text("Image Rendering Test", 1, 1));
  26. tui.addChild(new Spacer(1));
  27. if (dims) {
  28. tui.addChild(
  29. new Image(base64Data, "image/png", { fallbackColor: (s) => `\x1b[33m${s}\x1b[0m` }, { maxWidthCells: 60 }, dims),
  30. );
  31. } else {
  32. tui.addChild(new Text("Could not parse image dimensions", 1, 0));
  33. }
  34. tui.addChild(new Spacer(1));
  35. tui.addChild(new Text("Press Ctrl+C to exit", 1, 0));
  36. const editor = {
  37. handleInput(data: string) {
  38. if (data.charCodeAt(0) === 3) {
  39. tui.stop();
  40. process.exit(0);
  41. }
  42. },
  43. };
  44. tui.setFocus(editor as any);
  45. tui.start();