border-status-editor.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import {
  2. CustomEditor,
  3. type ExtensionAPI,
  4. type ExtensionContext,
  5. type KeybindingsManager,
  6. } from "@earendil-works/pi-coding-agent";
  7. import type { Component, EditorTheme, TUI } from "@earendil-works/pi-tui";
  8. import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
  9. function fitBorder(
  10. left: string,
  11. right: string,
  12. width: number,
  13. border: (text: string) => string,
  14. fill: (text: string) => string = border,
  15. ): string {
  16. if (width <= 0) return "";
  17. if (width === 1) return border("─");
  18. let leftText = left;
  19. let rightText = right;
  20. const fixedWidth = 2;
  21. const minimumGap = 3;
  22. while (
  23. fixedWidth + visibleWidth(leftText) + visibleWidth(rightText) + minimumGap > width &&
  24. visibleWidth(rightText) > 0
  25. ) {
  26. rightText = truncateToWidth(rightText, Math.max(0, visibleWidth(rightText) - 1), "");
  27. }
  28. while (
  29. fixedWidth + visibleWidth(leftText) + visibleWidth(rightText) + minimumGap > width &&
  30. visibleWidth(leftText) > 0
  31. ) {
  32. leftText = truncateToWidth(leftText, Math.max(0, visibleWidth(leftText) - 1), "");
  33. }
  34. const gapWidth = Math.max(0, width - fixedWidth - visibleWidth(leftText) - visibleWidth(rightText));
  35. return `${border("─")}${leftText}${fill("─".repeat(gapWidth))}${rightText}${border("─")}`;
  36. }
  37. function formatCwd(cwd: string): string {
  38. const home = process.env.HOME;
  39. if (home && cwd.startsWith(home)) {
  40. return `~${cwd.slice(home.length)}`;
  41. }
  42. return cwd;
  43. }
  44. function formatContext(ctx: ExtensionContext): string {
  45. const usage = ctx.getContextUsage();
  46. const contextWindow = usage?.contextWindow ?? ctx.model?.contextWindow;
  47. if (!contextWindow || !usage || usage.percent === null) {
  48. return "ctx ?";
  49. }
  50. return `ctx ${Math.round(usage.percent)}%/${(contextWindow / 1000).toFixed(0)}k`;
  51. }
  52. function formatThinking(level: string): string {
  53. return level === "off" ? "off" : level;
  54. }
  55. class EmptyFooter implements Component {
  56. render(): string[] {
  57. return [];
  58. }
  59. invalidate(): void {}
  60. }
  61. export default function (pi: ExtensionAPI) {
  62. let isWorking = false;
  63. let spinnerIndex = 0;
  64. let spinnerTimer: ReturnType<typeof setInterval> | undefined;
  65. let activeTui: TUI | undefined;
  66. const spinnerFrames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
  67. const stopSpinner = () => {
  68. if (spinnerTimer) {
  69. clearInterval(spinnerTimer);
  70. spinnerTimer = undefined;
  71. }
  72. };
  73. pi.on("agent_start", () => {
  74. isWorking = true;
  75. stopSpinner();
  76. spinnerTimer = setInterval(() => {
  77. spinnerIndex = (spinnerIndex + 1) % spinnerFrames.length;
  78. activeTui?.requestRender();
  79. }, 80);
  80. activeTui?.requestRender();
  81. });
  82. pi.on("agent_end", () => {
  83. isWorking = false;
  84. stopSpinner();
  85. activeTui?.requestRender();
  86. });
  87. pi.on("session_shutdown", () => {
  88. stopSpinner();
  89. activeTui = undefined;
  90. });
  91. pi.on("session_start", (_event, ctx) => {
  92. ctx.ui.setWorkingVisible(false);
  93. ctx.ui.setFooter(() => new EmptyFooter());
  94. let branch: string | undefined;
  95. const refreshBranch = async () => {
  96. const result = await pi.exec("git", ["branch", "--show-current"], { cwd: ctx.cwd }).catch(() => undefined);
  97. const stdout = result?.stdout.trim();
  98. branch = stdout && stdout.length > 0 ? stdout : undefined;
  99. activeTui?.requestRender();
  100. };
  101. void refreshBranch();
  102. class BorderStatusEditor extends CustomEditor {
  103. constructor(tui: TUI, theme: EditorTheme, keybindings: KeybindingsManager) {
  104. super(tui, theme, keybindings, { paddingX: 0 });
  105. activeTui = tui;
  106. }
  107. render(width: number): string[] {
  108. const lines = super.render(width);
  109. if (lines.length < 2) return lines;
  110. const thm = ctx.ui.theme;
  111. const model = ctx.model ? `${ctx.model.provider}/${ctx.model.id}` : "no model";
  112. const thinking = pi.getThinkingLevel();
  113. const topLeft = isWorking ? thm.fg("accent", ` ${spinnerFrames[spinnerIndex]} `) : "";
  114. const topRight = "";
  115. const bottomLeft = thm.fg("muted", ` ${model} · ${formatThinking(thinking)} `);
  116. const bottomRight = thm.fg(
  117. "muted",
  118. ` ${formatContext(ctx)} · ${formatCwd(ctx.cwd)}${branch ? ` (${branch})` : ""} `,
  119. );
  120. const borderColor = (text: string) => this.borderColor(text);
  121. lines[0] = fitBorder(topLeft, topRight, width, borderColor);
  122. lines[lines.length - 1] = fitBorder(bottomLeft, bottomRight, width, borderColor);
  123. return lines;
  124. }
  125. }
  126. ctx.ui.setEditorComponent((tui, theme, keybindings) => new BorderStatusEditor(tui, theme, keybindings));
  127. });
  128. }