|
1 | 1 | import { Args, Flags } from "@oclif/core"; |
2 | 2 | import * as Ably from "ably"; |
3 | | -import chalk from "chalk"; |
4 | 3 |
|
5 | 4 | import { AblyBaseCommand } from "../../base-command.js"; |
6 | 5 | import { productApiFlags, timeRangeFlags } from "../../flags.js"; |
7 | | -import { formatMessageData } from "../../utils/json-formatter.js"; |
8 | 6 | import { buildHistoryParams } from "../../utils/history.js"; |
9 | 7 | import { errorMessage } from "../../utils/errors.js"; |
10 | 8 | import { |
11 | | - countLabel, |
| 9 | + formatMessagesOutput, |
12 | 10 | formatTimestamp, |
13 | 11 | formatMessageTimestamp, |
14 | 12 | limitWarning, |
15 | | - resource, |
| 13 | + toMessageJson, |
16 | 14 | } from "../../utils/output.js"; |
| 15 | +import type { MessageDisplayFields } from "../../utils/output.js"; |
17 | 16 |
|
18 | 17 | export default class ChannelsHistory extends AblyBaseCommand { |
19 | 18 | static override args = { |
@@ -85,41 +84,30 @@ export default class ChannelsHistory extends AblyBaseCommand { |
85 | 84 | const history = await channel.history(historyParams); |
86 | 85 | const messages = history.items; |
87 | 86 |
|
| 87 | + // Build display fields from history results |
| 88 | + const displayMessages: MessageDisplayFields[] = messages.map( |
| 89 | + (message, index) => ({ |
| 90 | + channel: channelName, |
| 91 | + clientId: message.clientId, |
| 92 | + data: message.data, |
| 93 | + event: message.name || "(none)", |
| 94 | + id: message.id, |
| 95 | + indexPrefix: `[${index + 1}] ${formatTimestamp(formatMessageTimestamp(message.timestamp))}`, |
| 96 | + serial: message.serial, |
| 97 | + timestamp: message.timestamp ?? Date.now(), |
| 98 | + }), |
| 99 | + ); |
| 100 | + |
88 | 101 | // Display results based on format |
89 | 102 | if (this.shouldOutputJson(flags)) { |
90 | | - this.log(this.formatJsonOutput({ messages }, flags)); |
91 | | - } else { |
92 | | - if (messages.length === 0) { |
93 | | - this.log("No messages found in the channel history."); |
94 | | - return; |
95 | | - } |
96 | | - |
97 | 103 | this.log( |
98 | | - `Found ${countLabel(messages.length, "message")} in the history of channel: ${resource(channelName)}`, |
| 104 | + this.formatJsonOutput( |
| 105 | + displayMessages.map((msg) => toMessageJson(msg)), |
| 106 | + flags, |
| 107 | + ), |
99 | 108 | ); |
100 | | - this.log(""); |
101 | | - |
102 | | - for (const [index, message] of messages.entries()) { |
103 | | - const timestampDisplay = message.timestamp |
104 | | - ? formatTimestamp(formatMessageTimestamp(message.timestamp)) |
105 | | - : chalk.dim("[Unknown timestamp]"); |
106 | | - |
107 | | - this.log(`${chalk.dim(`[${index + 1}]`)} ${timestampDisplay}`); |
108 | | - this.log( |
109 | | - `${chalk.dim("Event:")} ${chalk.yellow(message.name || "(none)")}`, |
110 | | - ); |
111 | | - |
112 | | - if (message.clientId) { |
113 | | - this.log( |
114 | | - `${chalk.dim("Client ID:")} ${chalk.blue(message.clientId)}`, |
115 | | - ); |
116 | | - } |
117 | | - |
118 | | - this.log(chalk.dim("Data:")); |
119 | | - this.log(formatMessageData(message.data)); |
120 | | - |
121 | | - this.log(""); |
122 | | - } |
| 109 | + } else { |
| 110 | + this.log(formatMessagesOutput(displayMessages)); |
123 | 111 |
|
124 | 112 | const warning = limitWarning(messages.length, flags.limit, "messages"); |
125 | 113 | if (warning) this.log(warning); |
|
0 commit comments