File size: 3,955 Bytes
20f108c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | import blessed from "blessed";
import { phantomTheme, phantomBorders, phantomBordersFocus } from "./theme.js";
import { AgentIdentity } from "../agents/types.js";
export interface PhantomPanel {
box: blessed.Widgets.BoxElement;
log: blessed.Widgets.Log;
agentId?: string;
}
export class PanelManager {
private screen: blessed.Widgets.Screen;
private panels: PhantomPanel[] = [];
private focusedIndex: number = 0;
private container: blessed.Widgets.BoxElement;
constructor(screen: blessed.Widgets.Screen) {
this.screen = screen;
this.container = blessed.box({
parent: screen,
top: 1,
left: 0,
width: "100%",
height: "95%-1",
});
}
createPanel(agent?: AgentIdentity): PhantomPanel {
const cols = Math.ceil(Math.sqrt(this.panels.length + 1));
const rows = Math.ceil((this.panels.length + 1) / cols);
const idx = this.panels.length;
const box = blessed.box({
parent: this.container,
top: `${Math.floor(idx / cols) * (100 / rows)}%`,
left: `${(idx % cols) * (100 / cols)}%`,
width: `${100 / cols}%`,
height: `${100 / rows}%`,
border: phantomBorders,
style: {
bg: phantomTheme.panelBg,
fg: phantomTheme.foreground,
border: { fg: phantomTheme.border },
},
label: agent
? ` {bold}${agent.name}{/bold} [${agent.role}] `
: " {bold}~terminal{/bold} ",
tags: true,
});
const log = blessed.log({
parent: box,
top: 0,
left: 0,
width: "100%",
height: "100%-1",
scrollable: true,
scrollbar: {
ch: "░",
style: { fg: phantomTheme.dim },
},
style: {
bg: phantomTheme.panelBg,
fg: phantomTheme.foreground,
},
keys: true,
vi: true,
alwaysScroll: true,
scrollback: 500,
tags: true,
});
const footer = blessed.box({
parent: box,
bottom: 0,
left: 0,
width: "100%",
height: 1,
content: agent
? ` {${agent.color}-fg}●{/${agent.color}-fg} ${agent.id}`
: " {bold}←→{/bold} switch {bold}SPC{/bold} cmd {bold}ESC{/bold} menu ",
tags: true,
style: {
bg: phantomTheme.titleBar,
fg: phantomTheme.dim,
},
});
const panel: PhantomPanel = { box, log, agentId: agent?.id };
this.panels.push(panel);
box.on("click", () => this.focus(idx));
this.focus(idx);
this.screen.render();
return panel;
}
focus(index: number): void {
const prev = this.panels[this.focusedIndex];
if (prev && prev.box.style.border) {
(prev.box.style.border as Record<string, string>).fg = phantomTheme.border;
}
this.focusedIndex = Math.max(0, Math.min(index, this.panels.length - 1));
const current = this.panels[this.focusedIndex];
if (current && current.box.style.border) {
(current.box.style.border as Record<string, string>).fg = phantomTheme.borderFocus;
}
this.screen.render();
}
getFocused(): PhantomPanel | undefined {
return this.panels[this.focusedIndex];
}
getPanels(): PhantomPanel[] {
return this.panels;
}
removePanel(id: string): void {
const idx = this.panels.findIndex((p) => p.agentId === id);
if (idx === -1) return;
const panel = this.panels[idx];
panel.box.detach();
this.panels.splice(idx, 1);
this.screen.render();
}
writeToPanel(agentId: string, msg: string): void {
const panel = this.panels.find((p) => p.agentId === agentId);
if (panel) {
panel.log.add(msg);
this.screen.render();
}
}
writeToAll(msg: string): void {
this.panels.forEach((p) => {
p.log.add(msg);
});
this.screen.render();
}
nextPanel(): void {
this.focus((this.focusedIndex + 1) % this.panels.length);
}
prevPanel(): void {
this.focus((this.focusedIndex - 1 + this.panels.length) % this.panels.length);
}
}
|