export interface BashResult {
	output: string;
	exitCode?: number;
	cancelled?: boolean;
	truncated?: boolean;
	fullOutputPath?: string;
}

export interface ParsedBashInput {
	command: string;
	display: string;
	excludeFromContext: boolean;
}

export function parseBashInput(text: string): ParsedBashInput | null {
	const trimmed = text.trim();
	if (!trimmed.startsWith("!")) return null;

	const excludeFromContext = trimmed.startsWith("!!");
	const command = (excludeFromContext ? trimmed.slice(2) : trimmed.slice(1)).trim();
	if (!command) return null;

	return {
		command,
		display: trimmed,
		excludeFromContext,
	};
}

export function formatBashOutput(result: BashResult): string {
	if (result.cancelled) return "(已取消)";
	if (result.output?.trim()) {
		let output = result.output;
		if (result.truncated) output += "\n...(输出已截断)";
		return output;
	}
	if (result.exitCode === 0) return "(无输出)";
	return "(无输出)";
}
