|
| 1 | +// Shared OpenClaw banner: the pixel lobster mascot beside the OPENCLAW |
| 2 | +// wordmark, with a short startup animation on rich interactive terminals. |
| 3 | +// Used by the wizard flows (doctor/onboard/configure) and the foreground |
| 4 | +// gateway run; non-TTY and CI paths always get the plain static banner. |
| 5 | +import { |
| 6 | + decorativeEmoji, |
| 7 | + supportsDecorativeEmoji, |
| 8 | +} from "../../packages/terminal-core/src/decorative-emoji.js"; |
| 9 | +import { restoreTerminalState } from "../../packages/terminal-core/src/restore.js"; |
| 10 | +import { isRich, theme } from "../../packages/terminal-core/src/theme.js"; |
| 11 | +import type { RuntimeEnv } from "../runtime.js"; |
| 12 | + |
| 13 | +// Art is pregenerated from pixel bitmaps (two pixel rows per terminal row via |
| 14 | +// ▀▄█). Mascot and wordmark are separate so they can be tinted independently; |
| 15 | +// the wordmark starts on mascot row 2, keeping the claws above the text line. |
| 16 | +const MASCOT_ART = [ |
| 17 | + "▄███▄ ▄███▄", |
| 18 | + "▀█▄█▀ ▀█▄█▀", |
| 19 | + " ▀▄ ▄▀", |
| 20 | + " ██ █ ██", |
| 21 | + " ▀█████▀", |
| 22 | + " ▄█▀ █ ▀█▄", |
| 23 | +] as const; |
| 24 | +// Claw tips with the pincer notch widened; swapping the top two rows in and |
| 25 | +// out produces the "snip". |
| 26 | +const MASCOT_OPEN_ROWS = ["▄█▀█▄ ▄█▀█▄", "▀█ █▀ ▀█ █▀"] as const; |
| 27 | +const MASCOT_WIDTH = 15; |
| 28 | +const WORDMARK_ROW_OFFSET = 2; |
| 29 | + |
| 30 | +const WORDMARK_ART = [ |
| 31 | + "█▀▀▀█ █▀▀▀█ █▀▀▀▀ █▄ █ █▀▀▀▀ █ █▀▀▀█ █ █", |
| 32 | + "█ █ █▀▀▀▀ █▀▀▀ █ ▀▄█ █ █ █▀▀▀█ █▄▀▄█", |
| 33 | + "▀▀▀▀▀ ▀ ▀▀▀▀▀ ▀ ▀ ▀▀▀▀▀ ▀▀▀▀▀ ▀ ▀ ▀ ▀", |
| 34 | +] as const; |
| 35 | +const GAP = 3; |
| 36 | +const BANNER_WIDTH = MASCOT_WIDTH + GAP + 48; |
| 37 | +const ROWS = MASCOT_ART.length; |
| 38 | + |
| 39 | +export type ClawBannerOptions = { |
| 40 | + columns?: number; |
| 41 | + isTty?: boolean; |
| 42 | + rich?: boolean; |
| 43 | + env?: NodeJS.ProcessEnv; |
| 44 | + /** Injectable randomness for the animation garnish (tests pin it). */ |
| 45 | + rng?: () => number; |
| 46 | + sleep?: (ms: number) => Promise<void>; |
| 47 | + write?: (chunk: string) => void; |
| 48 | +}; |
| 49 | + |
| 50 | +type CellTint = (col: number) => (text: string) => string; |
| 51 | + |
| 52 | +const identityTint: (text: string) => string = (text) => text; |
| 53 | + |
| 54 | +// Composes one banner frame. Tints run per glyph column so the wipe edge and |
| 55 | +// shimmer band can cut through individual letters. |
| 56 | +function composeFrame(params: { |
| 57 | + mascotRows?: readonly string[]; |
| 58 | + mascotTint?: CellTint; |
| 59 | + wordmarkTint?: CellTint; |
| 60 | +}): string[] { |
| 61 | + const mascotRows = params.mascotRows ?? MASCOT_ART; |
| 62 | + const lines: string[] = []; |
| 63 | + for (let row = 0; row < ROWS; row++) { |
| 64 | + const mascotRow = (mascotRows[row] ?? "").padEnd(MASCOT_WIDTH).slice(0, MASCOT_WIDTH); |
| 65 | + let out = ""; |
| 66 | + for (let col = 0; col < mascotRow.length; col++) { |
| 67 | + const ch = mascotRow[col] ?? " "; |
| 68 | + out += ch === " " ? " " : (params.mascotTint?.(col) ?? theme.accent)(ch); |
| 69 | + } |
| 70 | + const wordmarkRow = WORDMARK_ART[row - WORDMARK_ROW_OFFSET]; |
| 71 | + if (wordmarkRow) { |
| 72 | + out += " ".repeat(GAP); |
| 73 | + for (let col = 0; col < wordmarkRow.length; col++) { |
| 74 | + const ch = wordmarkRow[col] ?? " "; |
| 75 | + out += |
| 76 | + ch === " " ? " " : (params.wordmarkTint?.(MASCOT_WIDTH + GAP + col) ?? identityTint)(ch); |
| 77 | + } |
| 78 | + } |
| 79 | + lines.push(out.replace(/\s+$/, "")); |
| 80 | + } |
| 81 | + return lines; |
| 82 | +} |
| 83 | + |
| 84 | +function staticBannerLines(): string[] { |
| 85 | + return composeFrame({}); |
| 86 | +} |
| 87 | + |
| 88 | +function plainTitleLine(): string { |
| 89 | + const icon = decorativeEmoji("🦞"); |
| 90 | + return supportsDecorativeEmoji() && icon ? `${icon} OPENCLAW ${icon}` : "OPENCLAW"; |
| 91 | +} |
| 92 | + |
| 93 | +const defaultSleep = (ms: number) => |
| 94 | + new Promise<void>((resolve) => { |
| 95 | + setTimeout(resolve, ms); |
| 96 | + }); |
| 97 | + |
| 98 | +// One combined entrance: a left-to-right molt wipe reveals the color, a |
| 99 | +// shimmer band sweeps the wordmark, and the claws snip. The rng varies the |
| 100 | +// shimmer passes and snip count a little so back-to-back runs don't feel |
| 101 | +// canned; every sequence ends on the exact static banner. |
| 102 | +async function animateBanner(opts: { |
| 103 | + rng: () => number; |
| 104 | + sleep: (ms: number) => Promise<void>; |
| 105 | + write: (chunk: string) => void; |
| 106 | +}): Promise<void> { |
| 107 | + const { rng, sleep, write } = opts; |
| 108 | + let drewFrame = false; |
| 109 | + const draw = (lines: string[]) => { |
| 110 | + const prefix = drewFrame ? `\x1b[${ROWS}F` : ""; |
| 111 | + drewFrame = true; |
| 112 | + write(`${prefix}${lines.map((line) => `\x1b[K${line}`).join("\n")}\n`); |
| 113 | + }; |
| 114 | + // Ctrl-C during the ~1s sequence would otherwise kill the process with the |
| 115 | + // cursor still hidden: default signal death skips the finally block. The |
| 116 | + // banner runs before any other component installs signal handlers, so a |
| 117 | + // scoped restore-and-exit handler is safe here and removed right after. |
| 118 | + const onSignal = (signal: "SIGINT" | "SIGTERM") => { |
| 119 | + restoreTerminalState(`claw banner ${signal}`); |
| 120 | + process.exit(signal === "SIGINT" ? 130 : 143); |
| 121 | + }; |
| 122 | + const onSigint = () => onSignal("SIGINT"); |
| 123 | + const onSigterm = () => onSignal("SIGTERM"); |
| 124 | + process.once("SIGINT", onSigint); |
| 125 | + process.once("SIGTERM", onSigterm); |
| 126 | + write("\x1b[?25l"); |
| 127 | + try { |
| 128 | + // Molt wipe: dim shell ahead of a bright 2-column edge, color behind it. |
| 129 | + const wipeSteps = 9; |
| 130 | + for (let step = 0; step <= wipeSteps; step++) { |
| 131 | + const edge = Math.round((BANNER_WIDTH * step) / wipeSteps); |
| 132 | + const tintAt = |
| 133 | + (colored: (text: string) => string): CellTint => |
| 134 | + (col) => |
| 135 | + col < edge ? colored : col < edge + 2 ? theme.accentBright : theme.muted; |
| 136 | + draw( |
| 137 | + composeFrame({ |
| 138 | + mascotTint: tintAt(theme.accent), |
| 139 | + wordmarkTint: tintAt(identityTint), |
| 140 | + }), |
| 141 | + ); |
| 142 | + await sleep(45); |
| 143 | + } |
| 144 | + // Shimmer: a bright band sweeps the wordmark; rarely it runs twice. |
| 145 | + const shimmerPasses = rng() < 0.2 ? 2 : 1; |
| 146 | + for (let pass = 0; pass < shimmerPasses; pass++) { |
| 147 | + for (let x = MASCOT_WIDTH; x < BANNER_WIDTH + 6; x += 4) { |
| 148 | + const band: CellTint = (col) => |
| 149 | + col >= x && col < x + 6 ? theme.accentBright : identityTint; |
| 150 | + draw(composeFrame({ wordmarkTint: band })); |
| 151 | + await sleep(40); |
| 152 | + } |
| 153 | + } |
| 154 | + // Snip: claws open and close once, sometimes twice. |
| 155 | + const snips = rng() < 0.4 ? 2 : 1; |
| 156 | + for (let snip = 0; snip < snips; snip++) { |
| 157 | + draw(composeFrame({ mascotRows: [...MASCOT_OPEN_ROWS, ...MASCOT_ART.slice(2)] })); |
| 158 | + await sleep(95); |
| 159 | + draw(staticBannerLines()); |
| 160 | + await sleep(115); |
| 161 | + } |
| 162 | + draw(staticBannerLines()); |
| 163 | + } finally { |
| 164 | + process.off("SIGINT", onSigint); |
| 165 | + process.off("SIGTERM", onSigterm); |
| 166 | + write("\x1b[?25h"); |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +/** |
| 171 | + * Prints the OpenClaw banner: animated on rich interactive terminals, static |
| 172 | + * otherwise, plain title on terminals too narrow for the art. |
| 173 | + */ |
| 174 | +export async function printClawBanner( |
| 175 | + runtime: RuntimeEnv, |
| 176 | + options: ClawBannerOptions = {}, |
| 177 | +): Promise<void> { |
| 178 | + const columns = options.columns ?? process.stdout.columns ?? 80; |
| 179 | + if (columns < BANNER_WIDTH) { |
| 180 | + runtime.log(`${plainTitleLine()}\n`); |
| 181 | + return; |
| 182 | + } |
| 183 | + const env = options.env ?? process.env; |
| 184 | + const animate = |
| 185 | + (options.isTty ?? process.stdout.isTTY ?? false) && |
| 186 | + (options.rich ?? isRich()) && |
| 187 | + !env.CI && |
| 188 | + !env.VITEST; |
| 189 | + if (!animate) { |
| 190 | + runtime.log(`${staticBannerLines().join("\n")}\n`); |
| 191 | + return; |
| 192 | + } |
| 193 | + await animateBanner({ |
| 194 | + rng: options.rng ?? Math.random, |
| 195 | + sleep: options.sleep ?? defaultSleep, |
| 196 | + write: options.write ?? ((chunk) => process.stdout.write(chunk)), |
| 197 | + }); |
| 198 | + (options.write ?? ((chunk: string) => process.stdout.write(chunk)))("\n"); |
| 199 | +} |
| 200 | + |
| 201 | +export const testing = { |
| 202 | + staticBannerLines, |
| 203 | + BANNER_WIDTH, |
| 204 | +}; |
0 commit comments