pi-tui-status-beautifier 1.5.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,8 +14,12 @@ const COMMON_NAMES: Record<string, string> = {
14
14
  "pi-control-chrome": "chrome",
15
15
  chrome: "chrome",
16
16
  toolflow: "toolflow",
17
+ anchor: "anchor",
18
+ "pi-anchor": "anchor",
17
19
  };
18
20
 
21
+ const SPINNERS = ["◐", "◓", "◑", "◒"] as const;
22
+
19
23
  export interface CustomStyleConfig {
20
24
  prefix?: string;
21
25
  suffix?: string;
@@ -292,9 +296,32 @@ export function beautifyStatus(
292
296
 
293
297
  // 4. Determine state glyph & indicator
294
298
  const glyphMap = preset.glyphs || {};
295
- let glyph =
296
- glyphMap[state] ||
297
- (state === "warning" ? "◌" : state === "error" ? "▲" : state === "accent" ? "◆" : "●");
299
+ let defaultGlyph =
300
+ name === "anchor"
301
+ ? "⌖"
302
+ : name === "toolflow"
303
+ ? "⌬"
304
+ : state === "warning"
305
+ ? "◌"
306
+ : state === "error"
307
+ ? "▲"
308
+ : state === "accent"
309
+ ? "◆"
310
+ : "●";
311
+
312
+ // Dynamic micro-spinner for active in-progress / running states (running, working, starting, waiting)
313
+ if (
314
+ valLower.includes("running") ||
315
+ valLower.includes("working") ||
316
+ valLower.includes("starting") ||
317
+ valLower.includes("waiting") ||
318
+ valLower.includes("...")
319
+ ) {
320
+ const spinnerChar = SPINNERS[Math.floor(Date.now() / 300) % SPINNERS.length];
321
+ defaultGlyph = spinnerChar;
322
+ }
323
+
324
+ let glyph = glyphMap[state] || defaultGlyph;
298
325
 
299
326
  const hasThemeFg = theme && typeof theme.fg === "function";
300
327
  const coloredGlyph = hasThemeFg ? theme.fg(state, glyph) : glyph;
@@ -440,6 +467,58 @@ export function padToVisualWidth(str: string, targetWidth: number, padChar = " "
440
467
  return str + padChar.repeat(targetWidth - currentWidth);
441
468
  }
442
469
 
470
+ /**
471
+ * Formats multiple status badges with adaptive folding for narrow terminals (< 80 columns)
472
+ */
473
+ export function formatFoldedBadges(
474
+ rawBadges: Array<{ key: string; val: string; rendered: string; isHighPriority: boolean }>,
475
+ maxWidth: number,
476
+ theme: any
477
+ ): string {
478
+ const fullStr = rawBadges.map(b => b.rendered).join(" ");
479
+ if (getStringWidth(fullStr) <= maxWidth || rawBadges.length <= 2) {
480
+ return fullStr;
481
+ }
482
+
483
+ // Narrow terminal overflow: partition high priority from idle/ready badges
484
+ const highPriority = rawBadges.filter(b => b.isHighPriority);
485
+ const lowPriority = rawBadges.filter(b => !b.isHighPriority);
486
+
487
+ const foldBadge = (count: number) => {
488
+ const text = `(+${count} idle)`;
489
+ return theme && typeof theme.fg === "function" ? theme.fg("dim", text) : text;
490
+ };
491
+
492
+ if (highPriority.length === 0) {
493
+ let current = "";
494
+ let shown = 0;
495
+ for (const b of lowPriority) {
496
+ const candidate = (current ? current + " " : "") + b.rendered;
497
+ const withFold = candidate + " " + foldBadge(lowPriority.length - shown - 1);
498
+ if (getStringWidth(withFold) <= maxWidth) {
499
+ current = candidate;
500
+ shown++;
501
+ } else {
502
+ break;
503
+ }
504
+ }
505
+ if (shown < lowPriority.length) {
506
+ const remaining = lowPriority.length - shown;
507
+ return current ? `${current} ${foldBadge(remaining)}` : foldBadge(remaining);
508
+ }
509
+ return current;
510
+ }
511
+
512
+ const highStr = highPriority.map(b => b.rendered).join(" ");
513
+ if (lowPriority.length > 0) {
514
+ const withFold = highStr + " " + foldBadge(lowPriority.length);
515
+ if (getStringWidth(withFold) <= maxWidth) {
516
+ return withFold;
517
+ }
518
+ }
519
+ return highStr;
520
+ }
521
+
443
522
  /**
444
523
  * Full-Footer Renderer (Hooked via ctx.ui.setFooter)
445
524
  * Formats working folder, Git branch, token metrics, and active model on Line 1,
@@ -564,11 +643,24 @@ export function renderBeautifiedFooter(
564
643
  }
565
644
 
566
645
  if (extStatuses.length > 0) {
567
- const badges = extStatuses
568
- .map(([k, v]) => beautifyStatus(k, v, theme, style, customCfg))
569
- .filter(Boolean) as string[];
570
- if (badges.length > 0) {
571
- lines.push(badges.join(" "));
646
+ const rawBadges = extStatuses
647
+ .map(([k, v]) => {
648
+ const rendered = beautifyStatus(k, v, theme, style, customCfg);
649
+ if (!rendered) return null;
650
+ const valLower = (v || "").toLowerCase();
651
+ const isHigh =
652
+ valLower.includes("error") ||
653
+ valLower.includes("fail") ||
654
+ valLower.includes("warn") ||
655
+ valLower.includes("running") ||
656
+ valLower.includes("active") ||
657
+ valLower.includes("starting");
658
+ return { key: k, val: v, rendered, isHighPriority: isHigh };
659
+ })
660
+ .filter(Boolean) as Array<{ key: string; val: string; rendered: string; isHighPriority: boolean }>;
661
+
662
+ if (rawBadges.length > 0) {
663
+ lines.push(formatFoldedBadges(rawBadges, width - 2, theme));
572
664
  }
573
665
  }
574
666
 
@@ -629,74 +721,42 @@ export default function (pi: ExtensionAPI) {
629
721
 
630
722
  // Dynamic status bar style controller registry
631
723
  pi.registerCommand("beautify", {
632
- description: "Choose TUI status bar beautification style or generate a custom style with AI",
724
+ description: "Choose TUI status bar style or customize with AI",
633
725
  handler: async (_args, ctx) => {
634
726
  if (!ctx.hasUI || !ctx.ui) {
635
727
  return;
636
728
  }
637
729
 
730
+ // Stage 1: Select Style / Theme
638
731
  const styleOptions = [
639
732
  "stream - Clean chevron stream (e.g. name ❯ ● ready)",
640
733
  "powerline - Angled brackets & diamonds (e.g. name ‹◆ ready›)",
641
734
  "matrix - Dot-matrix brackets & stars (e.g. name ⦗✦ ready⦘)",
642
735
  "glow - High-contrast inverse pill (e.g. name [ ● ready ])",
643
- "custom - [AI Prompt Recipe] Let your agent tailor your bespoke style",
644
- `footer - Toggle Full-Footer beautification (Git/Model/Tokens) [Currently: ${fullFooterEnabled ? "ON" : "OFF"}]`,
736
+ "custom - Describe your custom aesthetic to AI",
645
737
  "off - Revert to native Pi status bar & clean settings",
646
738
  ];
647
739
 
648
- const choice = await ctx.ui.select(`Choose status style (Current: ${currentStyle}):`, styleOptions);
740
+ const choice = await ctx.ui.select(`Choose status bar style (Current: ${currentStyle}):`, styleOptions);
649
741
 
650
742
  if (!choice) return;
651
743
 
652
744
  const styleKey = choice.split(" ")[0].trim();
653
745
 
654
- if (styleKey === "footer") {
655
- fullFooterEnabled = !fullFooterEnabled;
656
- const { targetSettingsPath } = loadConfig(ctx.cwd || process.cwd());
657
- try {
658
- if (fs.existsSync(targetSettingsPath)) {
659
- const data = fs.readFileSync(targetSettingsPath, "utf-8");
660
- const config = JSON.parse(data);
661
- if (!config.beautifier) config.beautifier = {};
662
- config.beautifier.fullFooter = fullFooterEnabled;
663
- const tempSettings = `${targetSettingsPath}.tmp.${process.pid}.${Date.now()}`;
664
- fs.writeFileSync(tempSettings, JSON.stringify(config, null, 2), "utf-8");
665
- fs.renameSync(tempSettings, targetSettingsPath);
666
- }
667
- } catch (_) {}
668
-
669
- if (fullFooterEnabled && currentStyle !== "off" && typeof (ctx.ui as any).setFooter === "function") {
670
- (ctx.ui as any).setFooter((tui: any, theme: any, footerData: any) => {
671
- const unsub = typeof footerData?.onBranchChange === "function" ? footerData.onBranchChange(() => tui.requestRender()) : undefined;
672
- return {
673
- dispose: unsub,
674
- invalidate() {},
675
- render(width: number): string[] {
676
- if (currentStyle === "off" || !fullFooterEnabled) return [];
677
- return renderBeautifiedFooter(width, tui, theme, footerData, ctx, currentStyle, customStyleConfig);
678
- }
679
- };
680
- });
681
- } else if (typeof (ctx.ui as any).setFooter === "function") {
682
- (ctx.ui as any).setFooter(undefined);
683
- }
684
-
685
- ctx.ui.notify(`Full-footer beautification is now ${fullFooterEnabled ? "ENABLED" : "DISABLED (Native Pi Footer)"}`, "info");
686
- return;
687
- }
688
-
746
+ // Zero-trace physical cleanup & revert
689
747
  if (styleKey === "off") {
690
748
  currentStyle = "off";
749
+ fullFooterEnabled = false;
691
750
  customStyleConfig = undefined;
692
751
  lastRenderedStatus.clear();
693
752
 
694
- // Restore all raw original statuses immediately
695
753
  for (const [key, val] of originalStatuses.entries()) {
696
754
  ctx.ui.setStatus(key, val);
697
755
  }
756
+ if (typeof (ctx.ui as any).setFooter === "function") {
757
+ (ctx.ui as any).setFooter(undefined);
758
+ }
698
759
 
699
- // Zero-trace physical cleanup: completely prune the `beautifier` key from settings.json!
700
760
  const { targetSettingsPath } = loadConfig(ctx.cwd || process.cwd());
701
761
  try {
702
762
  if (fs.existsSync(targetSettingsPath)) {
@@ -717,41 +777,113 @@ export default function (pi: ExtensionAPI) {
717
777
  return;
718
778
  }
719
779
 
780
+ // Form A: Interactive plain-language input dialog for Custom Style
720
781
  if (styleKey === "custom") {
721
- const promptRecipe = [
722
- "You are acting as my Pi TUI Status Bar designer for `pi-tui-status-beautifier`.",
723
- "1. In my primary language, call `ask_user_question` with ASCII visual preview mockups showing ONLY realistic extension statuses (e.g. `chrome`, `toolflow`, `plan`):",
724
- " - Powerline: chrome ‹◆ ready› | toolflow ‹◈ 2/5›",
725
- " - Matrix: chrome ⦗✦ ready⦘ | toolflow ⦗◍ 2/5⦘",
726
- " - Stream: chrome ❯ ● ready | toolflow ❯ ◐ 2/5",
727
- " - Glow: chrome [ ● ready ] | toolflow [ ◐ 2/5 ]",
728
- " (Do NOT include git branch or model name in mockups).",
729
- "2. If I describe or pick a style, write the configuration into `./.pi/settings.json` (or `~/.pi/agent/settings.json`) under `beautifier`:",
782
+ let userDesire: string | undefined;
783
+ if (typeof (ctx.ui as any).input === "function") {
784
+ userDesire = await (ctx.ui as any).input(
785
+ "Describe your custom status bar style:",
786
+ "e.g. 赛博复古点阵 / Minimal dots / Soft warm pastel / Powerline diamonds"
787
+ );
788
+ }
789
+
790
+ if (!userDesire || !userDesire.trim()) {
791
+ return;
792
+ }
793
+
794
+ const instruction = [
795
+ "Please configure a custom Pi TUI status bar style for `pi-tui-status-beautifier`.",
796
+ `User's requested aesthetic: "${userDesire.trim()}"`,
797
+ "",
798
+ "Requirements:",
799
+ `1. In the user's primary language, call \`ask_user_question\` with 3 distinct ASCII visual preview mockups tailored to "${userDesire.trim()}". Show ONLY realistic extension status items (e.g. \`chrome\`, \`toolflow\`, \`plan\`). Do NOT include git branch or model name in mockups.`,
800
+ "2. Once selected, save the configuration into `./.pi/settings.json` (or `~/.pi/agent/settings.json`) under `beautifier`:",
730
801
  " {",
731
802
  ' "beautifier": {',
732
803
  ' "style": "custom",',
733
804
  ' "custom": {',
734
- ' "separator": " » ",',
735
- ' "brackets": ["«", "»"],',
736
- ' "prefix": "",',
737
- ' "suffix": "",',
738
- ' "glyphs": { "success": "✦", "warning": "◇", "error": "✖", "accent": "✧" }',
805
+ ' "separator": " ... ",',
806
+ ' "brackets": ["...", "..."],',
807
+ ' "prefix": "...",',
808
+ ' "suffix": "...",',
809
+ ' "glyphs": { "success": "...", "warning": "...", "error": "...", "accent": "..." }',
739
810
  " }",
740
811
  " }",
741
812
  " }",
742
- "3. If I say revert, restore, default, off, or cancel, DELETE the `beautifier` key from `settings.json` and exit quietly without asking more questions.",
743
- "4. Do NOT search filesystem or edit extension source files."
813
+ "3. If the user ever asks to revert, cancel, or restore default, cleanly delete `beautifier` from `settings.json` without asking questions.",
814
+ "4. Do NOT search the filesystem or edit extension source files. Confirm when applied."
744
815
  ].join("\n");
745
816
 
746
- if (typeof (ctx.ui as any).setEditorText === "function") {
747
- (ctx.ui as any).setEditorText(promptRecipe);
748
- ctx.ui.notify("Press Enter to let your agent consult your style preference with visual previews!", "info");
749
- } else {
750
- ctx.ui.notify(promptRecipe, "info");
817
+ if (typeof (pi as any).sendUserMessage === "function") {
818
+ (pi as any).sendUserMessage(instruction, { deliverAs: "followUp" });
819
+ ctx.ui.notify("Consulting AI status bar designer...", "info");
820
+ } else if (typeof (ctx.ui as any).setEditorText === "function") {
821
+ (ctx.ui as any).setEditorText(instruction);
822
+ ctx.ui.notify("Press Enter to send custom style request to your agent!", "info");
751
823
  }
752
824
  return;
753
825
  }
754
826
 
827
+ // Stage 2: Scope Selection Wizard (Two-stage flow)
828
+ const scopeOptions = [
829
+ "status-only - Plugin status badges only (Recommended: keeps native Pi path, tokens & thinking level)",
830
+ "full-footer - Full-footer beautification (Transforms Git branch, model, and tokens too)",
831
+ ];
832
+
833
+ const scopeChoice = await ctx.ui.select("Choose beautification scope:", scopeOptions);
834
+ if (!scopeChoice) return;
835
+
836
+ const isFullFooter = scopeChoice.startsWith("full-footer");
837
+ fullFooterEnabled = isFullFooter;
838
+ currentStyle = styleKey;
839
+ lastRenderedStatus.clear();
840
+
841
+ // Write atomically to settings.json
842
+ const { targetSettingsPath } = loadConfig(ctx.cwd || process.cwd());
843
+ try {
844
+ if (fs.existsSync(targetSettingsPath)) {
845
+ const data = fs.readFileSync(targetSettingsPath, "utf-8");
846
+ const config = JSON.parse(data);
847
+ if (!config.beautifier) config.beautifier = {};
848
+ config.beautifier.style = styleKey;
849
+ config.beautifier.fullFooter = isFullFooter;
850
+ const tempSettings = `${targetSettingsPath}.tmp.${process.pid}.${Date.now()}`;
851
+ fs.writeFileSync(tempSettings, JSON.stringify(config, null, 2), "utf-8");
852
+ fs.renameSync(tempSettings, targetSettingsPath);
853
+ }
854
+ } catch (_e) {
855
+ // Silently ignore disk write errors
856
+ }
857
+
858
+ // Update footer rendering
859
+ if (isFullFooter && typeof (ctx.ui as any).setFooter === "function") {
860
+ (ctx.ui as any).setFooter((tui: any, theme: any, footerData: any) => {
861
+ const unsub =
862
+ typeof footerData?.onBranchChange === "function"
863
+ ? footerData.onBranchChange(() => tui.requestRender())
864
+ : undefined;
865
+ return {
866
+ dispose: unsub,
867
+ invalidate() {},
868
+ render(width: number): string[] {
869
+ if (currentStyle === "off" || !fullFooterEnabled) return [];
870
+ return renderBeautifiedFooter(width, tui, theme, footerData, ctx, currentStyle, customStyleConfig);
871
+ },
872
+ };
873
+ });
874
+ } else if (typeof (ctx.ui as any).setFooter === "function") {
875
+ (ctx.ui as any).setFooter(undefined);
876
+ }
877
+
878
+ for (const [key, val] of originalStatuses.entries()) {
879
+ ctx.ui.setStatus(key, val);
880
+ }
881
+
882
+ ctx.ui.notify(
883
+ `TUI status style changed to: ${styleKey} (${isFullFooter ? "Full-Footer" : "Status Only"})`,
884
+ "info"
885
+ );
886
+
755
887
  if ((VALID_STYLES as readonly string[]).includes(styleKey)) {
756
888
  currentStyle = styleKey;
757
889
  lastRenderedStatus.clear();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-tui-status-beautifier",
3
- "version": "1.5.0",
3
+ "version": "1.7.0",
4
4
  "description": "Minimalist status bar formatter & AI-customizable layout engine for Pi TUI.",
5
5
  "main": "extensions/tui-status-beautifier.ts",
6
6
  "keywords": [