mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 19:51:37 +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) {
|
export function registerDirectoryCli(program: Command) {
|
||||||
const directory = program
|
const directory = program
|
||||||
.command("directory")
|
.command("directory")
|
||||||
@@ -138,22 +162,7 @@ export function registerDirectoryCli(program: Command) {
|
|||||||
defaultRuntime.log(JSON.stringify(result, null, 2));
|
defaultRuntime.log(JSON.stringify(result, null, 2));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (result.length === 0) {
|
printDirectoryList({ title: "Peers", emptyMessage: "No peers found.", entries: result });
|
||||||
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(),
|
|
||||||
);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
defaultRuntime.error(danger(String(err)));
|
defaultRuntime.error(danger(String(err)));
|
||||||
defaultRuntime.exit(1);
|
defaultRuntime.exit(1);
|
||||||
@@ -185,22 +194,7 @@ export function registerDirectoryCli(program: Command) {
|
|||||||
defaultRuntime.log(JSON.stringify(result, null, 2));
|
defaultRuntime.log(JSON.stringify(result, null, 2));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (result.length === 0) {
|
printDirectoryList({ title: "Groups", emptyMessage: "No groups found.", entries: result });
|
||||||
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(),
|
|
||||||
);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
defaultRuntime.error(danger(String(err)));
|
defaultRuntime.error(danger(String(err)));
|
||||||
defaultRuntime.exit(1);
|
defaultRuntime.exit(1);
|
||||||
@@ -239,24 +233,11 @@ export function registerDirectoryCli(program: Command) {
|
|||||||
defaultRuntime.log(JSON.stringify(result, null, 2));
|
defaultRuntime.log(JSON.stringify(result, null, 2));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (result.length === 0) {
|
printDirectoryList({
|
||||||
defaultRuntime.log(theme.muted("No group members found."));
|
title: "Group Members",
|
||||||
return;
|
emptyMessage: "No group members found.",
|
||||||
}
|
entries: result,
|
||||||
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(),
|
|
||||||
);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
defaultRuntime.error(danger(String(err)));
|
defaultRuntime.error(danger(String(err)));
|
||||||
defaultRuntime.exit(1);
|
defaultRuntime.exit(1);
|
||||||
|
|||||||
Reference in New Issue
Block a user