fix: improve config loading flow and table column width

This commit is contained in:
mitchell 2026-05-04 22:02:07 -04:00
parent decfd2bde0
commit 7f0498fc84
3 changed files with 18 additions and 11 deletions

View file

@ -30,15 +30,15 @@ async function main() {
const modelFlag = args.model ?? args.m;
const configFlag = args.config ?? args.c;
const config = await loadConfig();
const config = await loadConfig(configFlag);
const model = modelFlag ?? config.model;
if (configFlag) {
if (configFlag || !config) {
await initConfig(config);
return;
}
const model = modelFlag ?? config.model;
const renderer = new Renderer(plainOut, plainErr);
const provider = createOpenAICompatible({

View file

@ -10,13 +10,14 @@ export interface AppConfig {
model: string;
}
export async function initConfig(old?: AppConfig): Promise<AppConfig> {
export async function initConfig(old: AppConfig | null): Promise<AppConfig> {
console.log();
intro(chalk.blue("Welcome to LMC. Let's make a new config!"));
const baseURL = await text({
message: "What's your base URL:",
initialValue: old?.baseURL,
defaultValue: "http://127.0.0.1:8080/v1",
placeholder: "http://127.0.0.1:8080/v1",
defaultValue: old?.baseURL ?? "http://127.0.0.1:8080/v1",
placeholder: old?.baseURL ?? "http://127.0.0.1:8080/v1",
});
if (isCancel(baseURL)) process.exit(1);
@ -32,6 +33,7 @@ export async function initConfig(old?: AppConfig): Promise<AppConfig> {
const config = { baseURL, model } satisfies AppConfig;
await configFile.write(JSON.stringify(config, null, " "));
outro(chalk.green("Configuration saved!"));
return config;
@ -52,12 +54,17 @@ async function getModels(url: string) {
return models.data.map((model) => ({ value: model.id }));
}
export async function loadConfig(): Promise<AppConfig> {
export async function loadConfig(
configFlag: boolean | undefined,
): Promise<AppConfig | null> {
let config: AppConfig;
try {
config = await configFile.json();
} catch {
config = await initConfig();
if (configFlag) return null;
console.error(`Config not found at ${configFile.name}`);
return null;
}
return config;
}

View file

@ -110,7 +110,7 @@ export async function renderMarkdown(
.header(header)
.body(body)
.border()
.maxColWidth(Math.floor(columns / header.length) - 3)
.maxColWidth(Math.floor(columns / header.length) - header.length)
.toString();
header = [];
body = [];