mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-21 13:44:03 +00:00
refactor(cli): dedupe directory table rendering
This commit is contained in:
@@ -37,6 +37,30 @@ function buildRows(entries: Array<{ id: string; name?: string | undefined }>) {
|
||||
}));
|
||||
}
|
||||
|
||||
function printDirectoryList(params: {
|
||||
title: string;
|
||||
emptyMessage: string;
|
||||
entries: Array<{ id: string; name?: string | undefined }>;
|
||||
}): void {
|
||||
if (params.entries.length === 0) {
|
||||
defaultRuntime.log(theme.muted(params.emptyMessage));
|
||||
return;
|
||||
}
|
||||
|
||||
const tableWidth = Math.max(60, (process.stdout.columns ?? 120) - 1);
|
||||
defaultRuntime.log(`${theme.heading(params.title)} ${theme.muted(`(${params.entries.length})`)}`);
|
||||
defaultRuntime.log(
|
||||
renderTable({
|
||||
width: tableWidth,
|
||||
columns: [
|
||||
{ key: "ID", header: "ID", minWidth: 16, flex: true },
|
||||
{ key: "Name", header: "Name", minWidth: 18, flex: true },
|
||||
],
|
||||
rows: buildRows(params.entries),
|
||||
}).trimEnd(),
|
||||
);
|
||||
}
|
||||
|
||||
export function registerDirectoryCli(program: Command) {
|
||||
const directory = program
|
||||
.command("directory")
|
||||
@@ -138,22 +162,7 @@ export function registerDirectoryCli(program: Command) {
|
||||
defaultRuntime.log(JSON.stringify(result, null, 2));
|
||||
return;
|
||||
}
|
||||
if (result.length === 0) {
|
||||
defaultRuntime.log(theme.muted("No peers found."));
|
||||
return;
|
||||
}
|
||||
const tableWidth = Math.max(60, (process.stdout.columns ?? 120) - 1);
|
||||
defaultRuntime.log(`${theme.heading("Peers")} ${theme.muted(`(${result.length})`)}`);
|
||||
defaultRuntime.log(
|
||||
renderTable({
|
||||
width: tableWidth,
|
||||
columns: [
|
||||
{ key: "ID", header: "ID", minWidth: 16, flex: true },
|
||||
{ key: "Name", header: "Name", minWidth: 18, flex: true },
|
||||
],
|
||||
rows: buildRows(result),
|
||||
}).trimEnd(),
|
||||
);
|
||||
printDirectoryList({ title: "Peers", emptyMessage: "No peers found.", entries: result });
|
||||
} catch (err) {
|
||||
defaultRuntime.error(danger(String(err)));
|
||||
defaultRuntime.exit(1);
|
||||
@@ -185,22 +194,7 @@ export function registerDirectoryCli(program: Command) {
|
||||
defaultRuntime.log(JSON.stringify(result, null, 2));
|
||||
return;
|
||||
}
|
||||
if (result.length === 0) {
|
||||
defaultRuntime.log(theme.muted("No groups found."));
|
||||
return;
|
||||
}
|
||||
const tableWidth = Math.max(60, (process.stdout.columns ?? 120) - 1);
|
||||
defaultRuntime.log(`${theme.heading("Groups")} ${theme.muted(`(${result.length})`)}`);
|
||||
defaultRuntime.log(
|
||||
renderTable({
|
||||
width: tableWidth,
|
||||
columns: [
|
||||
{ key: "ID", header: "ID", minWidth: 16, flex: true },
|
||||
{ key: "Name", header: "Name", minWidth: 18, flex: true },
|
||||
],
|
||||
rows: buildRows(result),
|
||||
}).trimEnd(),
|
||||
);
|
||||
printDirectoryList({ title: "Groups", emptyMessage: "No groups found.", entries: result });
|
||||
} catch (err) {
|
||||
defaultRuntime.error(danger(String(err)));
|
||||
defaultRuntime.exit(1);
|
||||
@@ -239,24 +233,11 @@ export function registerDirectoryCli(program: Command) {
|
||||
defaultRuntime.log(JSON.stringify(result, null, 2));
|
||||
return;
|
||||
}
|
||||
if (result.length === 0) {
|
||||
defaultRuntime.log(theme.muted("No group members found."));
|
||||
return;
|
||||
}
|
||||
const tableWidth = Math.max(60, (process.stdout.columns ?? 120) - 1);
|
||||
defaultRuntime.log(
|
||||
`${theme.heading("Group Members")} ${theme.muted(`(${result.length})`)}`,
|
||||
);
|
||||
defaultRuntime.log(
|
||||
renderTable({
|
||||
width: tableWidth,
|
||||
columns: [
|
||||
{ key: "ID", header: "ID", minWidth: 16, flex: true },
|
||||
{ key: "Name", header: "Name", minWidth: 18, flex: true },
|
||||
],
|
||||
rows: buildRows(result),
|
||||
}).trimEnd(),
|
||||
);
|
||||
printDirectoryList({
|
||||
title: "Group Members",
|
||||
emptyMessage: "No group members found.",
|
||||
entries: result,
|
||||
});
|
||||
} catch (err) {
|
||||
defaultRuntime.error(danger(String(err)));
|
||||
defaultRuntime.exit(1);
|
||||
|
||||
Reference in New Issue
Block a user