Skip to content

Commit fa57231

Browse files
authored
refactor(cli): simplify message result rendering (#131980)
* refactor(cli): simplify message result rendering * chore(cli): tighten message formatter assertion budget
1 parent 86789b6 commit fa57231

2 files changed

Lines changed: 57 additions & 101 deletions

File tree

‎config/assertion-safety-baseline.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2589,7 +2589,7 @@ src/commands/export-trajectory.ts 1
25892589
src/commands/gateway-health-auth-diagnostic.ts 2
25902590
src/commands/gateway-presence.ts 1
25912591
src/commands/gateway-status/helpers.ts 11
2592-
src/commands/message-format.ts 21
2592+
src/commands/message-format.ts 20
25932593
src/commands/message.ts 4
25942594
src/commands/models/fallbacks-shared.ts 2
25952595
src/commands/models/list.model-projection.ts 1

‎src/commands/message-format.ts‎

Lines changed: 56 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ type FormatOpts = {
4343
displayLimit?: number;
4444
};
4545

46-
function renderObjectSummary(payload: unknown, opts: FormatOpts): string[] {
46+
function renderObjectSummary(payload: unknown, opts: FormatOpts): string {
4747
if (!payload || typeof payload !== "object") {
48-
return [String(payload)];
48+
return String(payload);
4949
}
5050
const obj = payload as Record<string, unknown>;
5151
const keys = Object.keys(obj);
5252
if (keys.length === 0) {
53-
return [theme.muted("(empty)")];
53+
return theme.muted("(empty)");
5454
}
5555

5656
const rows = keys.slice(0, 20).map((k) => {
@@ -79,19 +79,17 @@ function renderObjectSummary(payload: unknown, opts: FormatOpts): string[] {
7979
: "unknown";
8080
return { Key: k, Value: shortenText(value, 96) };
8181
});
82-
return [
83-
renderTable({
84-
width: opts.width,
85-
columns: [
86-
{ key: "Key", header: "Key", minWidth: 16 },
87-
{ key: "Value", header: "Value", flex: true, minWidth: 24 },
88-
],
89-
rows,
90-
}).trimEnd(),
91-
];
82+
return renderTable({
83+
width: opts.width,
84+
columns: [
85+
{ key: "Key", header: "Key", minWidth: 16 },
86+
{ key: "Value", header: "Value", flex: true, minWidth: 24 },
87+
],
88+
rows,
89+
}).trimEnd();
9290
}
9391

94-
function renderMessageList(messages: unknown[], opts: FormatOpts, emptyLabel: string): string[] {
92+
function renderMessageList(messages: unknown[], opts: FormatOpts, emptyLabel: string): string {
9593
const cap = opts.displayLimit ?? 25;
9694
const rows = messages.slice(0, cap).map((m) => {
9795
const msg = m as Record<string, unknown>;
@@ -123,43 +121,19 @@ function renderMessageList(messages: unknown[], opts: FormatOpts, emptyLabel: st
123121
});
124122

125123
if (rows.length === 0) {
126-
return [theme.muted(emptyLabel)];
124+
return theme.muted(emptyLabel);
127125
}
128126

129-
return [
130-
renderTable({
131-
width: opts.width,
132-
columns: [
133-
{ key: "Time", header: "Time", minWidth: 14 },
134-
{ key: "Author", header: "Author", minWidth: 10 },
135-
{ key: "Text", header: "Text", flex: true, minWidth: 24 },
136-
{ key: "Id", header: "Id", minWidth: 10 },
137-
],
138-
rows,
139-
}).trimEnd(),
140-
];
141-
}
142-
143-
function renderMessagesFromPayload(payload: unknown, opts: FormatOpts): string[] | null {
144-
if (!payload || typeof payload !== "object") {
145-
return null;
146-
}
147-
const messages = (payload as { messages?: unknown }).messages;
148-
if (!Array.isArray(messages)) {
149-
return null;
150-
}
151-
return renderMessageList(messages, opts, "No messages.");
152-
}
153-
154-
function renderPinsFromPayload(payload: unknown, opts: FormatOpts): string[] | null {
155-
if (!payload || typeof payload !== "object") {
156-
return null;
157-
}
158-
const pins = (payload as { pins?: unknown }).pins;
159-
if (!Array.isArray(pins)) {
160-
return null;
161-
}
162-
return renderMessageList(pins, opts, "No pins.");
127+
return renderTable({
128+
width: opts.width,
129+
columns: [
130+
{ key: "Time", header: "Time", minWidth: 14 },
131+
{ key: "Author", header: "Author", minWidth: 10 },
132+
{ key: "Text", header: "Text", flex: true, minWidth: 24 },
133+
{ key: "Id", header: "Id", minWidth: 10 },
134+
],
135+
rows,
136+
}).trimEnd();
163137
}
164138

165139
function extractDiscordSearchResultsMessages(results: unknown): unknown[] | null {
@@ -182,7 +156,7 @@ function extractDiscordSearchResultsMessages(results: unknown): unknown[] | null
182156
return flattened;
183157
}
184158

185-
function renderReactions(payload: unknown, opts: FormatOpts): string[] | null {
159+
function renderReactions(payload: unknown, opts: FormatOpts): string | null {
186160
if (!payload || typeof payload !== "object") {
187161
return null;
188162
}
@@ -228,20 +202,18 @@ function renderReactions(payload: unknown, opts: FormatOpts): string[] | null {
228202
});
229203

230204
if (rows.length === 0) {
231-
return [theme.muted("No reactions.")];
205+
return theme.muted("No reactions.");
232206
}
233207

234-
return [
235-
renderTable({
236-
width: opts.width,
237-
columns: [
238-
{ key: "Emoji", header: "Emoji", minWidth: 8 },
239-
{ key: "Count", header: "Count", align: "right", minWidth: 6 },
240-
{ key: "Users", header: "Users", flex: true, minWidth: 20 },
241-
],
242-
rows,
243-
}).trimEnd(),
244-
];
208+
return renderTable({
209+
width: opts.width,
210+
columns: [
211+
{ key: "Emoji", header: "Emoji", minWidth: 8 },
212+
{ key: "Count", header: "Count", align: "right", minWidth: 6 },
213+
{ key: "Users", header: "Users", flex: true, minWidth: 20 },
214+
],
215+
rows,
216+
}).trimEnd();
245217
}
246218

247219
/**
@@ -253,17 +225,11 @@ function renderPaginationHint(payload: unknown, muted: (text: string) => string)
253225
return null;
254226
}
255227
const obj = payload as Record<string, unknown>;
256-
if (obj.hasMore === true) {
257-
return muted(
258-
"More results available. Use --limit to fetch more, or --json for the raw cursor.",
259-
);
260-
}
261-
if (typeof obj.nextBatch === "string" && obj.nextBatch) {
262-
return muted(
263-
"More results available. Use --limit to fetch more, or --json for the raw cursor.",
264-
);
265-
}
266-
if (typeof obj["@odata.nextLink"] === "string" && obj["@odata.nextLink"]) {
228+
if (
229+
obj.hasMore === true ||
230+
(typeof obj.nextBatch === "string" && obj.nextBatch) ||
231+
(typeof obj["@odata.nextLink"] === "string" && obj["@odata.nextLink"])
232+
) {
267233
return muted(
268234
"More results available. Use --limit to fetch more, or --json for the raw cursor.",
269235
);
@@ -414,30 +380,22 @@ export function formatMessageCliText(
414380
}
415381

416382
const reactionsTable = renderReactions(payload, formatOpts);
417-
if (reactionsTable && result.action === "reactions") {
383+
if (reactionsTable !== null && result.action === "reactions") {
418384
lines.push(heading("Reactions"));
419-
lines.push(reactionsTable[0] ?? "");
385+
lines.push(reactionsTable);
420386
return lines;
421387
}
422388

423-
if (result.action === "read") {
424-
const messagesTable = renderMessagesFromPayload(payload, formatOpts);
425-
if (messagesTable) {
426-
lines.push(heading("Messages"));
427-
lines.push(messagesTable[0] ?? "");
428-
const hint = renderPaginationHint(payload, muted);
429-
if (hint) {
430-
lines.push(hint);
431-
}
432-
return lines;
433-
}
434-
}
435-
436-
if (result.action === "list-pins") {
437-
const pinsTable = renderPinsFromPayload(payload, formatOpts);
438-
if (pinsTable) {
439-
lines.push(heading("Pinned messages"));
440-
lines.push(pinsTable[0] ?? "");
389+
if (result.action === "read" || result.action === "list-pins") {
390+
const read = result.action === "read";
391+
const messages =
392+
payload && typeof payload === "object"
393+
? (payload as { messages?: unknown; pins?: unknown })[read ? "messages" : "pins"]
394+
: undefined;
395+
if (Array.isArray(messages)) {
396+
const table = renderMessageList(messages, formatOpts, read ? "No messages." : "No pins.");
397+
lines.push(heading(read ? "Messages" : "Pinned messages"));
398+
lines.push(table);
441399
const hint = renderPaginationHint(payload, muted);
442400
if (hint) {
443401
lines.push(hint);
@@ -451,7 +409,7 @@ export function formatMessageCliText(
451409
const list = extractDiscordSearchResultsMessages(results);
452410
if (list) {
453411
lines.push(heading("Search results"));
454-
lines.push(renderMessageList(list, formatOpts, "No results.")[0] ?? "");
412+
lines.push(renderMessageList(list, formatOpts, "No results."));
455413
// Discord's approximate result count cannot prove another page exists.
456414
const hint = renderPaginationHint(payload, muted) ?? renderPaginationHint(results, muted);
457415
if (hint) {
@@ -464,11 +422,9 @@ export function formatMessageCliText(
464422
// Generic success + compact details table.
465423
lines.push(ok(`✅ ${result.action} via ${resolveChannelLabel(result.channel)}.`));
466424
const summary = renderObjectSummary(payload, formatOpts);
467-
if (summary.length) {
468-
lines.push("");
469-
lines.push(...summary);
470-
lines.push("");
471-
lines.push(muted("Tip: use --json for full output."));
472-
}
425+
lines.push("");
426+
lines.push(summary);
427+
lines.push("");
428+
lines.push(muted("Tip: use --json for full output."));
473429
return lines;
474430
}

0 commit comments

Comments
 (0)