rainbow-editor.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * Rainbow Editor - highlights "ultrathink" with animated shine effect
  3. *
  4. * Usage: pi --extension ./examples/extensions/rainbow-editor.ts
  5. */
  6. import { CustomEditor, type ExtensionAPI } from "@earendil-works/pi-coding-agent";
  7. // Base colors (coral → yellow → green → teal → blue → purple → pink)
  8. const COLORS: [number, number, number][] = [
  9. [233, 137, 115], // coral
  10. [228, 186, 103], // yellow
  11. [141, 192, 122], // green
  12. [102, 194, 179], // teal
  13. [121, 157, 207], // blue
  14. [157, 134, 195], // purple
  15. [206, 130, 172], // pink
  16. ];
  17. const RESET = "\x1b[0m";
  18. function brighten(rgb: [number, number, number], factor: number): string {
  19. const [r, g, b] = rgb.map((c) => Math.round(c + (255 - c) * factor));
  20. return `\x1b[38;2;${r};${g};${b}m`;
  21. }
  22. function colorize(text: string, shinePos: number): string {
  23. return (
  24. [...text]
  25. .map((c, i) => {
  26. const baseColor = COLORS[i % COLORS.length]!;
  27. // 3-letter shine: center bright, adjacent dimmer
  28. let factor = 0;
  29. if (shinePos >= 0) {
  30. const dist = Math.abs(i - shinePos);
  31. if (dist === 0) factor = 0.7;
  32. else if (dist === 1) factor = 0.35;
  33. }
  34. return `${brighten(baseColor, factor)}${c}`;
  35. })
  36. .join("") + RESET
  37. );
  38. }
  39. class RainbowEditor extends CustomEditor {
  40. private animationTimer?: ReturnType<typeof setInterval>;
  41. private frame = 0;
  42. private hasUltrathink(): boolean {
  43. return /ultrathink/i.test(this.getText());
  44. }
  45. private startAnimation(): void {
  46. if (this.animationTimer) return;
  47. this.animationTimer = setInterval(() => {
  48. this.frame++;
  49. this.tui.requestRender();
  50. }, 60);
  51. }
  52. private stopAnimation(): void {
  53. if (this.animationTimer) {
  54. clearInterval(this.animationTimer);
  55. this.animationTimer = undefined;
  56. }
  57. }
  58. handleInput(data: string): void {
  59. super.handleInput(data);
  60. if (this.hasUltrathink()) {
  61. this.startAnimation();
  62. } else {
  63. this.stopAnimation();
  64. }
  65. }
  66. render(width: number): string[] {
  67. // Cycle: 10 shine positions + 10 pause frames
  68. const cycle = this.frame % 20;
  69. const shinePos = cycle < 10 ? cycle : -1; // -1 means no shine (pause)
  70. return super.render(width).map((line) => line.replace(/ultrathink/gi, (m) => colorize(m, shinePos)));
  71. }
  72. }
  73. export default function (pi: ExtensionAPI) {
  74. pi.on("session_start", (_event, ctx) => {
  75. ctx.ui.setEditorComponent((tui, theme, kb) => new RainbowEditor(tui, theme, kb));
  76. });
  77. }