import type { PadroneActionContext } from '../types/index.ts';
import { type ColorConfig, type ColorTheme, createColorizer } from './colorizer.ts';
export const DEFAULT_TERMINAL_WIDTH = 80;
export function wrapText(text: string, maxWidth: number): string[] {
if (maxWidth <= 0 || text.length <= maxWidth) return [text];
const words = text.split(' ');
const lines: string[] = [];
let current = '';
for (const word of words) {
if (current && current.length + 1 + word.length > maxWidth) {
lines.push(current);
current = word;
} else {
current = current ? `${current} ${word}` : word;
}
}
if (current) lines.push(current);
return lines.length > 0 ? lines : [text];
}
export function escapeHtml(text: string): string {
return text.replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"').replace(/'/g, ''');
}
// ── Styler ──────────────────────────────────────────────────────────────
/**
* Styling functions for semantic text roles.
* Used by formatters to apply visual styles (ANSI, HTML, Markdown, etc.)
* to different types of content.
*/
export type Styler = {
command: (text: string) => string;
arg: (text: string) => string;
type: (text: string) => string;
description: (text: string) => string;
label: (text: string) => string;
section: (text: string) => string;
meta: (text: string) => string;
example: (text: string) => string;
exampleValue: (text: string) => string;
deprecated: (text: string) => string;
};
/**
* Layout configuration for formatters.
*/
export type LayoutConfig = {
newline: string;
indent: (level: number) => string;
join: (parts: string[]) => string;
wrapDocument?: (content: string) => string;
};
// ── Styler Factories ────────────────────────────────────────────────────
export function createTextStyler(): Styler {
return {
command: (text) => text,
arg: (text) => text,
type: (text) => text,
description: (text) => text,
label: (text) => text,
section: (text) => text,
meta: (text) => text,
example: (text) => text,
exampleValue: (text) => text,
deprecated: (text) => text,
};
}
export function createAnsiStyler(theme?: ColorTheme | ColorConfig): Styler {
const colorizer = createColorizer(theme);
return {
command: colorizer.command,
arg: colorizer.arg,
type: colorizer.type,
description: colorizer.description,
label: colorizer.label,
section: colorizer.label,
meta: colorizer.meta,
example: colorizer.example,
exampleValue: colorizer.exampleValue,
deprecated: colorizer.deprecated,
};
}
export function createConsoleStyler(theme?: ColorTheme | ColorConfig): Styler {
return createAnsiStyler(theme);
}
export function createMarkdownStyler(): Styler {
return {
command: (text) => `**${text}**`,
arg: (text) => `\`${text}\``,
type: (text) => `\`${text}\``,
description: (text) => text,
label: (text) => `**${text}**`,
section: (text) => `### ${text}`,
meta: (text) => `*${text}*`,
example: (text) => `**${text}**`,
exampleValue: (text) => `\`${text}\``,
deprecated: (text) => `~~${text}~~`,
};
}
export function createHtmlStyler(): Styler {
return {
command: (text) => `${escapeHtml(text)}`,
arg: (text) => `${escapeHtml(text)}`,
type: (text) => `${escapeHtml(text)}`,
description: (text) => `${escapeHtml(text)}`,
label: (text) => `${escapeHtml(text)}`,
section: (text) => `