|
| 1 | +interface CowsayOptions { |
| 2 | + text: string |
| 3 | + mode?: "say" | "think" |
| 4 | + eyes?: string |
| 5 | + tongue?: string |
| 6 | +} |
| 7 | + |
| 8 | +export function cowsay(options: CowsayOptions | string): string { |
| 9 | + // Handle string argument |
| 10 | + const opts: CowsayOptions = |
| 11 | + typeof options === "string" ? { text: options } : options |
| 12 | + |
| 13 | + // Default options |
| 14 | + const { text = "", mode = "say", eyes = "oo", tongue = " " } = opts |
| 15 | + |
| 16 | + // Split text into lines |
| 17 | + const lines = formatText(text) |
| 18 | + |
| 19 | + // Create the speech bubble |
| 20 | + const bubble = createBubble(lines, mode) |
| 21 | + |
| 22 | + // Create the cow |
| 23 | + const cow = createCow(eyes, tongue, mode) |
| 24 | + |
| 25 | + // Combine the bubble and cow |
| 26 | + return bubble + cow |
| 27 | +} |
| 28 | + |
| 29 | +function formatText(text: string, maxWidth: number = 40): string[] { |
| 30 | + if (!text) return [""] |
| 31 | + |
| 32 | + const words = text.split(" ") |
| 33 | + const lines: string[] = [] |
| 34 | + let currentLine = "" |
| 35 | + |
| 36 | + for (const word of words) { |
| 37 | + if (currentLine.length + word.length + 1 <= maxWidth) { |
| 38 | + currentLine += (currentLine ? " " : "") + word |
| 39 | + } else { |
| 40 | + lines.push(currentLine) |
| 41 | + currentLine = word |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + if (currentLine) { |
| 46 | + lines.push(currentLine) |
| 47 | + } |
| 48 | + |
| 49 | + return lines |
| 50 | +} |
| 51 | + |
| 52 | +function createBubble(lines: string[], mode: "say" | "think"): string { |
| 53 | + if (lines.length === 0) return "" |
| 54 | + |
| 55 | + const maxLength = Math.max(...lines.map((line) => line.length)) |
| 56 | + let result = " " + "_".repeat(maxLength + 2) + "\n" |
| 57 | + |
| 58 | + if (lines.length === 1) { |
| 59 | + const line = lines[0] |
| 60 | + const padding = " ".repeat(maxLength - line.length) |
| 61 | + result += |
| 62 | + mode === "say" |
| 63 | + ? `< ${line}${padding} >\n` |
| 64 | + : `( ${line}${padding} )\n` |
| 65 | + } else { |
| 66 | + lines.forEach((line, i) => { |
| 67 | + const padding = " ".repeat(maxLength - line.length) |
| 68 | + let prefix, suffix |
| 69 | + |
| 70 | + if (i === 0) { |
| 71 | + prefix = mode === "say" ? "/ " : "( " |
| 72 | + suffix = mode === "say" ? " \\" : " )" |
| 73 | + } else if (i === lines.length - 1) { |
| 74 | + prefix = mode === "say" ? "\\ " : "( " |
| 75 | + suffix = mode === "say" ? " /" : " )" |
| 76 | + } else { |
| 77 | + prefix = mode === "say" ? "| " : "( " |
| 78 | + suffix = mode === "say" ? " |" : " )" |
| 79 | + } |
| 80 | + |
| 81 | + result += `${prefix}${line}${padding}${suffix}\n` |
| 82 | + }) |
| 83 | + } |
| 84 | + |
| 85 | + result += " " + "-".repeat(maxLength + 2) + "\n" |
| 86 | + return result |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Create the ASCII cow |
| 91 | + */ |
| 92 | +function createCow( |
| 93 | + eyes: string, |
| 94 | + tongue: string, |
| 95 | + mode: "say" | "think" |
| 96 | +): string { |
| 97 | + return ` \\ ^__^ |
| 98 | + \\ (${eyes})\\_______ |
| 99 | + (__)\\ )\\/\\ |
| 100 | + ${tongue}||----w | |
| 101 | + || || |
| 102 | + ` |
| 103 | +} |
| 104 | + |
| 105 | +export function cowthink(options: CowsayOptions | string): string { |
| 106 | + const opts = typeof options === "string" ? { text: options } : options |
| 107 | + return cowsay({ ...opts, mode: "think" }) |
| 108 | +} |
0 commit comments