(options?: { colors?: boolean })
| 296 | * @since 4.0.0 |
| 297 | */ |
| 298 | export const defaultFormatter = (options?: { colors?: boolean }): Formatter => { |
| 299 | const globalProcess = (globalThis as any).process |
| 300 | const hasProcess = typeof globalProcess === "object" && globalProcess !== null |
| 301 | |
| 302 | const useColor = options?.colors !== undefined |
| 303 | ? options.colors |
| 304 | // Auto-detect based on environment |
| 305 | : (hasProcess && |
| 306 | typeof globalProcess.stdout === "object" && |
| 307 | globalProcess.stdout !== null && |
| 308 | globalProcess.stdout.isTTY === true && |
| 309 | globalProcess.env?.NO_COLOR !== "1") |
| 310 | |
| 311 | // Color palette using ANSI escape codes |
| 312 | const colors = useColor |
| 313 | ? { |
| 314 | bold: (text: string): string => `\x1b[1m${text}\x1b[0m`, |
| 315 | dim: (text: string): string => `\x1b[2m${text}\x1b[0m`, |
| 316 | cyan: (text: string): string => `\x1b[36m${text}\x1b[0m`, |
| 317 | green: (text: string): string => `\x1b[32m${text}\x1b[0m`, |
| 318 | blue: (text: string): string => `\x1b[34m${text}\x1b[0m`, |
| 319 | yellow: (text: string): string => `\x1b[33m${text}\x1b[0m`, |
| 320 | magenta: (text: string): string => `\x1b[35m${text}\x1b[0m` |
| 321 | } |
| 322 | : { |
| 323 | bold: (text: string): string => text, |
| 324 | dim: (text: string): string => text, |
| 325 | cyan: (text: string): string => text, |
| 326 | green: (text: string): string => text, |
| 327 | blue: (text: string): string => text, |
| 328 | yellow: (text: string): string => text, |
| 329 | magenta: (text: string): string => text |
| 330 | } |
| 331 | |
| 332 | const reset = useColor ? "\x1b[0m" : "" |
| 333 | const red = useColor ? "\x1b[31m" : "" |
| 334 | const bold = useColor ? "\x1b[1m" : "" |
| 335 | |
| 336 | return { |
| 337 | formatHelpDoc: (doc: HelpDoc): string => formatHelpDocImpl(doc, colors), |
| 338 | formatCliError: (error): string => escapeControlCharacters(error.message), |
| 339 | formatError: (error): string => { |
| 340 | return `\n${bold}${red}ERROR${reset}\n ${escapeControlCharacters(error.message)}${reset}` |
| 341 | }, |
| 342 | formatErrors: (errors): string => { |
| 343 | if (errors.length === 0) return "" |
| 344 | if (errors.length === 1) { |
| 345 | return `\n${bold}${red}ERROR${reset}\n ${escapeControlCharacters(errors[0].message)}${reset}` |
| 346 | } |
| 347 | |
| 348 | // Group errors by _tag |
| 349 | const grouped = new Map<string, Array<CliError.CliError>>() |
| 350 | for (const error of errors) { |
| 351 | const tag = (error as any)._tag ?? "Error" |
| 352 | const group = grouped.get(tag) ?? [] |
| 353 | group.push(error) |
| 354 | grouped.set(tag, group) |
| 355 | } |
no test coverage detected