|
| 1 | +import * as p from "@clack/prompts"; |
| 2 | +import type { DetectedAgent } from "./detector"; |
| 3 | + |
| 4 | +function hexToRgb(hex: string): { r: number; g: number; b: number } { |
| 5 | + const clean = hex.replace(/^#/, ""); |
| 6 | + const fullHex = |
| 7 | + clean.length === 3 |
| 8 | + ? clean |
| 9 | + .split("") |
| 10 | + .map((c) => c + c) |
| 11 | + .join("") |
| 12 | + : clean; |
| 13 | + const num = parseInt(fullHex, 16); |
| 14 | + if (isNaN(num)) return { r: 255, g: 255, b: 255 }; |
| 15 | + return { |
| 16 | + r: (num >> 16) & 255, |
| 17 | + g: (num >> 8) & 255, |
| 18 | + b: num & 255, |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +function colorize(text: string, hex: string): string { |
| 23 | + const { r, g, b } = hexToRgb(hex); |
| 24 | + return `\x1b[38;2;${r};${g};${b}m${text}\x1b[0m`; |
| 25 | +} |
| 26 | + |
| 27 | +function dim(text: string): string { |
| 28 | + return `\x1b[2m${text}\x1b[0m`; |
| 29 | +} |
| 30 | + |
| 31 | +function reset(text: string): string { |
| 32 | + return `\x1b[0m${text}\x1b[0m`; |
| 33 | +} |
| 34 | + |
| 35 | +function hyperlink(text: string, url: string): string { |
| 36 | + return `\x1b]8;;${url}\x07${text}\x1b]8;;\x07`; |
| 37 | +} |
| 38 | + |
| 39 | +function underline(text: string): string { |
| 40 | + return `\x1b[4m${text}\x1b[0m`; |
| 41 | +} |
| 42 | + |
| 43 | +export function displayStyledSuggestions(agents: DetectedAgent[]): void { |
| 44 | + if (agents.length === 0) return; |
| 45 | + |
| 46 | + const maxNameLength = Math.max(...agents.map((a) => a.name.length)); |
| 47 | + const lines = agents.map((agent) => { |
| 48 | + const coloredName = colorize(agent.name, agent.brandColor); |
| 49 | + const linkedName = underline(hyperlink(coloredName, agent.repoUrl)); |
| 50 | + const padding = " ".repeat(maxNameLength - agent.name.length); |
| 51 | + |
| 52 | + return reset(` ${linkedName}${padding} ${dim("→")} ${dim(agent.wrappedCommand)}`); |
| 53 | + }); |
| 54 | + |
| 55 | + const content = [...lines, "", "Generate wrapped stats for those too!"].join("\n"); |
| 56 | + p.note(content, "Other AI agents detected on your system"); |
| 57 | +} |
| 58 | + |
| 59 | +export function displayAgentSuggestions(agents: DetectedAgent[]): void { |
| 60 | + if (agents.length === 0) return; |
| 61 | + displayStyledSuggestions(agents); |
| 62 | +} |
0 commit comments