scai
Version:
> **AI-powered CLI for local code analysis, commit message suggestions, and natural-language queries.** 100% local, private, GDPR-friendly, made in Denmark/EU with ❤️.
54 lines (53 loc) • 1.73 kB
JavaScript
import columnify from "columnify";
/**
* Format structured rows for terminal output.
*/
export function styleRows(rows, options = {}) {
if (!rows || rows.length === 0)
return "";
const terminalWidth = process.stdout.columns || 80;
const { maxWidthFraction = 2 / 3 } = options;
return columnify(rows, {
columnSplitter: " ",
maxLineWidth: terminalWidth,
config: Object.fromEntries(Object.keys(rows[0]).map((key) => [
key,
{ maxWidth: Math.floor(terminalWidth * maxWidthFraction), align: "left" },
])),
});
}
/**
* Format a plain string for terminal output (wraps lines to terminal width).
*/
export function styleText(text, options = {}) {
const terminalWidth = process.stdout.columns || 80;
const { maxWidthFraction = 2 / 3 } = options;
const maxWidth = Math.floor(terminalWidth * maxWidthFraction);
// Wrap each line to maxWidth
const wrapped = text
.split("\n")
.map((line) => {
if (line.length <= maxWidth)
return line;
const chunks = [];
let start = 0;
while (start < line.length) {
chunks.push(line.slice(start, start + maxWidth));
start += maxWidth;
}
return chunks.join("\n");
})
.join("\n");
return wrapped;
}
/**
* Parse numbered suggestions or key-value text into rows for columnify.
*/
export function parseAndStyleSuggestions(text, options = {}) {
// Simple parser: each line becomes { No: n, Suggestion: line }
const rows = text
.trim()
.split("\n")
.map((line, idx) => ({ No: (idx + 1).toString(), Suggestion: line }));
return styleRows(rows, options);
}