|
| 1 | +import chalk from 'chalk'; |
| 2 | +import { colors } from './colors.js'; |
| 3 | + |
| 4 | +export interface SplitPaneOptions { |
| 5 | + splitRatio?: number; // default 0.45 (45% left, 55% right) |
| 6 | + chatTitle?: string; |
| 7 | + ptyTitle?: string; |
| 8 | +} |
| 9 | + |
| 10 | +export type PaneFocus = 'chat' | 'pty'; |
| 11 | + |
| 12 | +export class SplitTerminalRenderer { |
| 13 | + private splitRatio: number; |
| 14 | + private chatTitle: string; |
| 15 | + private ptyTitle: string; |
| 16 | + private focus: PaneFocus = 'chat'; |
| 17 | + |
| 18 | + private chatBuffer: string[] = []; |
| 19 | + private ptyBuffer: string[] = []; |
| 20 | + private currentInput = ''; |
| 21 | + |
| 22 | + private maxChatHistory = 100; |
| 23 | + private maxPtyHistory = 200; |
| 24 | + |
| 25 | + constructor(options: SplitPaneOptions = {}) { |
| 26 | + this.splitRatio = options.splitRatio ?? 0.45; |
| 27 | + this.chatTitle = options.chatTitle ?? '💬 CHAT & COLLABORATION'; |
| 28 | + this.ptyTitle = options.ptyTitle ?? '🤖 NATIVE AI VIEW (PTY)'; |
| 29 | + } |
| 30 | + |
| 31 | + public get currentFocus(): PaneFocus { |
| 32 | + return this.focus; |
| 33 | + } |
| 34 | + |
| 35 | + public setFocus(focus: PaneFocus): void { |
| 36 | + this.focus = focus; |
| 37 | + this.render(); |
| 38 | + } |
| 39 | + |
| 40 | + public toggleFocus(): PaneFocus { |
| 41 | + this.focus = this.focus === 'chat' ? 'pty' : 'chat'; |
| 42 | + this.render(); |
| 43 | + return this.focus; |
| 44 | + } |
| 45 | + |
| 46 | + public appendChat(line: string): void { |
| 47 | + const lines = line.split('\n'); |
| 48 | + for (const l of lines) { |
| 49 | + this.chatBuffer.push(l); |
| 50 | + } |
| 51 | + if (this.chatBuffer.length > this.maxChatHistory) { |
| 52 | + this.chatBuffer = this.chatBuffer.slice(this.chatBuffer.length - this.maxChatHistory); |
| 53 | + } |
| 54 | + this.render(); |
| 55 | + } |
| 56 | + |
| 57 | + public appendPtyData(data: string): void { |
| 58 | + const cleaned = data.replace(/\r\n/g, '\n').replace(/\r/g, '\n'); |
| 59 | + const lines = cleaned.split('\n'); |
| 60 | + for (const l of lines) { |
| 61 | + this.ptyBuffer.push(l); |
| 62 | + } |
| 63 | + if (this.ptyBuffer.length > this.maxPtyHistory) { |
| 64 | + this.ptyBuffer = this.ptyBuffer.slice(this.ptyBuffer.length - this.maxPtyHistory); |
| 65 | + } |
| 66 | + this.render(); |
| 67 | + } |
| 68 | + |
| 69 | + public setInput(input: string): void { |
| 70 | + this.currentInput = input; |
| 71 | + this.render(); |
| 72 | + } |
| 73 | + |
| 74 | + public clearPty(): void { |
| 75 | + this.ptyBuffer = []; |
| 76 | + this.render(); |
| 77 | + } |
| 78 | + |
| 79 | + public render(): void { |
| 80 | + if (!process.stdout.isTTY) return; |
| 81 | + |
| 82 | + const totalCols = process.stdout.columns || 120; |
| 83 | + const totalRows = process.stdout.rows || 30; |
| 84 | + |
| 85 | + const leftCols = Math.max(20, Math.floor(totalCols * this.splitRatio)); |
| 86 | + const rightCols = Math.max(20, totalCols - leftCols - 1); |
| 87 | + const contentRows = Math.max(5, totalRows - 4); // 1 header, 1 subheader, contentRows, 1 divider, 1 footer input |
| 88 | + |
| 89 | + // Header bar |
| 90 | + const leftHeaderStr = ` ${this.chatTitle} `.padEnd(leftCols).slice(0, leftCols); |
| 91 | + const rightHeaderStr = ` ${this.ptyTitle} `.padEnd(rightCols).slice(0, rightCols); |
| 92 | + |
| 93 | + const leftHeaderFormatted = |
| 94 | + this.focus === 'chat' |
| 95 | + ? chalk.bgCyan.black.bold(leftHeaderStr) |
| 96 | + : chalk.bgBlue.white(leftHeaderStr); |
| 97 | + |
| 98 | + const rightHeaderFormatted = |
| 99 | + this.focus === 'pty' |
| 100 | + ? chalk.bgCyan.black.bold(rightHeaderStr) |
| 101 | + : chalk.bgBlue.white(rightHeaderStr); |
| 102 | + |
| 103 | + const headerLine = `${leftHeaderFormatted}│${rightHeaderFormatted}`; |
| 104 | + |
| 105 | + // Subheader / Status Line |
| 106 | + const leftFocusTag = this.focus === 'chat' ? colors.green('[ACTIVE FOCUS]') : colors.dim('[Press TAB to focus]'); |
| 107 | + const rightFocusTag = this.focus === 'pty' ? colors.green('[ACTIVE FOCUS]') : colors.dim('[Press TAB to focus]'); |
| 108 | + |
| 109 | + const subLeft = ` ${leftFocusTag}`.padEnd(leftCols + 10).slice(0, leftCols + 10); |
| 110 | + const subRight = ` ${rightFocusTag}`.padEnd(rightCols + 10).slice(0, rightCols + 10); |
| 111 | + const subheaderLine = `${subLeft}│${subRight}`; |
| 112 | + |
| 113 | + // Prepare viewport slice |
| 114 | + const visibleChat = this.chatBuffer.slice(-contentRows); |
| 115 | + const visiblePty = this.ptyBuffer.slice(-contentRows); |
| 116 | + |
| 117 | + const bodyLines: string[] = []; |
| 118 | + |
| 119 | + for (let r = 0; r < contentRows; r++) { |
| 120 | + const chatLine = visibleChat[r] ?? ''; |
| 121 | + const ptyLine = visiblePty[r] ?? ''; |
| 122 | + |
| 123 | + const truncatedChat = this.stripAnsiAndPad(chatLine, leftCols); |
| 124 | + const truncatedPty = this.stripAnsiAndPad(ptyLine, rightCols); |
| 125 | + |
| 126 | + bodyLines.push(`${truncatedChat}│${truncatedPty}`); |
| 127 | + } |
| 128 | + |
| 129 | + // Horizontal divider above input |
| 130 | + const lineDivider = '─'.repeat(totalCols); |
| 131 | + |
| 132 | + // Footer input bar |
| 133 | + const focusHelp = |
| 134 | + this.focus === 'chat' |
| 135 | + ? colors.cyan('💬 Focus: Chat Input') + colors.dim(' (Press TAB for PTY)') |
| 136 | + : colors.magenta('🤖 Focus: Native PTY Keypresses') + colors.dim(' (Press TAB for Chat)'); |
| 137 | + |
| 138 | + const promptText = `> ${this.currentInput}`; |
| 139 | + const footerLine = `${promptText.padEnd(totalCols - 35).slice(0, totalCols - 35)} │ ${focusHelp}`; |
| 140 | + |
| 141 | + // Frame output assembly using ANSI screen home \x1b[H |
| 142 | + const frame = ['\x1b[H', headerLine, subheaderLine, ...bodyLines, lineDivider, footerLine].join('\n'); |
| 143 | + process.stdout.write(frame); |
| 144 | + } |
| 145 | + |
| 146 | + private stripAnsiAndPad(text: string, width: number): string { |
| 147 | + const plain = text.replace(/\x1b\[[0-9;]*[a-zA-Z]/g, ''); |
| 148 | + if (plain.length >= width) { |
| 149 | + return text.slice(0, width); |
| 150 | + } |
| 151 | + return text + ' '.repeat(width - plain.length); |
| 152 | + } |
| 153 | +} |
0 commit comments