|
| 1 | +import type { SourceConfig } from "../types/config.js"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Information about a source and its tools for display |
| 5 | + */ |
| 6 | +export interface SourceDisplayInfo { |
| 7 | + id: string; |
| 8 | + type: string; |
| 9 | + host: string; |
| 10 | + database: string; |
| 11 | + readonly: boolean; |
| 12 | + isDemo: boolean; |
| 13 | + tools: string[]; |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Unicode box drawing characters |
| 18 | + */ |
| 19 | +const BOX = { |
| 20 | + topLeft: "┌", |
| 21 | + topRight: "┐", |
| 22 | + bottomLeft: "└", |
| 23 | + bottomRight: "┘", |
| 24 | + horizontal: "─", |
| 25 | + vertical: "│", |
| 26 | + leftT: "├", |
| 27 | + rightT: "┤", |
| 28 | + bullet: "•", |
| 29 | +}; |
| 30 | + |
| 31 | +/** |
| 32 | + * Parse host and database from source config |
| 33 | + */ |
| 34 | +function parseHostAndDatabase(source: SourceConfig): { host: string; database: string } { |
| 35 | + // If DSN is provided, parse it |
| 36 | + if (source.dsn) { |
| 37 | + try { |
| 38 | + const url = new URL(source.dsn); |
| 39 | + const host = url.port ? `${url.hostname}:${url.port}` : url.hostname; |
| 40 | + const database = url.pathname.replace(/^\//, "") || ""; |
| 41 | + return { host, database }; |
| 42 | + } catch { |
| 43 | + return { host: "unknown", database: "" }; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // Otherwise use individual connection params |
| 48 | + const host = source.host |
| 49 | + ? source.port |
| 50 | + ? `${source.host}:${source.port}` |
| 51 | + : source.host |
| 52 | + : "localhost"; |
| 53 | + const database = source.database || ""; |
| 54 | + |
| 55 | + return { host, database }; |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Generate a horizontal line of specified width |
| 60 | + */ |
| 61 | +function horizontalLine(width: number, left: string, right: string): string { |
| 62 | + return left + BOX.horizontal.repeat(width - 2) + right; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Pad or truncate a string to fit a specific width |
| 67 | + */ |
| 68 | +function fitString(str: string, width: number): string { |
| 69 | + if (str.length > width) { |
| 70 | + return str.slice(0, width - 1) + "…"; |
| 71 | + } |
| 72 | + return str.padEnd(width); |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Generate the startup table showing sources and their tools |
| 77 | + */ |
| 78 | +export function generateStartupTable(sources: SourceDisplayInfo[]): string { |
| 79 | + if (sources.length === 0) { |
| 80 | + return ""; |
| 81 | + } |
| 82 | + |
| 83 | + // Calculate column widths based on content |
| 84 | + const idTypeWidth = Math.max( |
| 85 | + 20, |
| 86 | + ...sources.map((s) => `${s.id} (${s.type})`.length) |
| 87 | + ); |
| 88 | + const hostDbWidth = Math.max( |
| 89 | + 24, |
| 90 | + ...sources.map((s) => { |
| 91 | + const hostDb = s.database ? `${s.host}/${s.database}` : s.host; |
| 92 | + return hostDb.length; |
| 93 | + }) |
| 94 | + ); |
| 95 | + const modeWidth = 10; |
| 96 | + |
| 97 | + // Total width: 2 for borders + content + 2 spaces padding per column + 2 separators |
| 98 | + const totalWidth = 2 + idTypeWidth + 3 + hostDbWidth + 3 + modeWidth + 2; |
| 99 | + |
| 100 | + const lines: string[] = []; |
| 101 | + |
| 102 | + for (let i = 0; i < sources.length; i++) { |
| 103 | + const source = sources[i]; |
| 104 | + const isFirst = i === 0; |
| 105 | + const isLast = i === sources.length - 1; |
| 106 | + |
| 107 | + // Top border (only for first source) |
| 108 | + if (isFirst) { |
| 109 | + lines.push(horizontalLine(totalWidth, BOX.topLeft, BOX.topRight)); |
| 110 | + } |
| 111 | + |
| 112 | + // Source header row |
| 113 | + const idType = fitString(`${source.id} (${source.type})`, idTypeWidth); |
| 114 | + const hostDb = fitString( |
| 115 | + source.database ? `${source.host}/${source.database}` : source.host, |
| 116 | + hostDbWidth |
| 117 | + ); |
| 118 | + |
| 119 | + // Mode indicators |
| 120 | + const modes: string[] = []; |
| 121 | + if (source.isDemo) modes.push("DEMO"); |
| 122 | + if (source.readonly) modes.push("READ-ONLY"); |
| 123 | + const modeStr = fitString(modes.join(" "), modeWidth); |
| 124 | + |
| 125 | + lines.push( |
| 126 | + `${BOX.vertical} ${idType} ${BOX.vertical} ${hostDb} ${BOX.vertical} ${modeStr} ${BOX.vertical}` |
| 127 | + ); |
| 128 | + |
| 129 | + // Separator after header |
| 130 | + lines.push(horizontalLine(totalWidth, BOX.leftT, BOX.rightT)); |
| 131 | + |
| 132 | + // Tool rows |
| 133 | + for (const tool of source.tools) { |
| 134 | + const toolLine = ` ${BOX.bullet} ${tool}`; |
| 135 | + lines.push( |
| 136 | + `${BOX.vertical} ${fitString(toolLine, totalWidth - 4)} ${BOX.vertical}` |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + // Bottom border or separator |
| 141 | + if (isLast) { |
| 142 | + lines.push(horizontalLine(totalWidth, BOX.bottomLeft, BOX.bottomRight)); |
| 143 | + } else { |
| 144 | + lines.push(horizontalLine(totalWidth, BOX.leftT, BOX.rightT)); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + return lines.join("\n"); |
| 149 | +} |
| 150 | + |
| 151 | +/** |
| 152 | + * Build SourceDisplayInfo from source configs and tool names |
| 153 | + */ |
| 154 | +export function buildSourceDisplayInfo( |
| 155 | + sourceConfigs: SourceConfig[], |
| 156 | + getToolsForSource: (sourceId: string) => string[], |
| 157 | + isDemo: boolean |
| 158 | +): SourceDisplayInfo[] { |
| 159 | + return sourceConfigs.map((source) => { |
| 160 | + const { host, database } = parseHostAndDatabase(source); |
| 161 | + |
| 162 | + return { |
| 163 | + id: source.id, |
| 164 | + type: source.type || "sqlite", |
| 165 | + host, |
| 166 | + database, |
| 167 | + readonly: source.readonly || false, |
| 168 | + isDemo, |
| 169 | + tools: getToolsForSource(source.id), |
| 170 | + }; |
| 171 | + }); |
| 172 | +} |
0 commit comments