From b060afd3a5ee7885031ccd87c31fdf6854181fff Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 15 Feb 2026 14:17:07 +0000 Subject: [PATCH] refactor(cli): dedupe directory table rendering --- src/cli/directory-cli.ts | 81 +++++++++++++++------------------------- 1 file changed, 31 insertions(+), 50 deletions(-) diff --git a/src/cli/directory-cli.ts b/src/cli/directory-cli.ts index 23b4d4b3fbd..93d5dd25507 100644 --- a/src/cli/directory-cli.ts +++ b/src/cli/directory-cli.ts @@ -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);