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 modelFlag = args.model ?? args.m;
const configFlag = args.config ?? args.c; const configFlag = args.config ?? args.c;
const config = await loadConfig(); const config = await loadConfig(configFlag);
const model = modelFlag ?? config.model; if (configFlag || !config) {
if (configFlag) {
await initConfig(config); await initConfig(config);
return; return;
} }
const model = modelFlag ?? config.model;
const renderer = new Renderer(plainOut, plainErr); const renderer = new Renderer(plainOut, plainErr);
const provider = createOpenAICompatible({ const provider = createOpenAICompatible({

View file

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

View file

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