Skip to content

Commit 1e9ac33

Browse files
committed
Prevent duplicate terminal output when combining terminal output messages
1 parent 97f0dd4 commit 1e9ac33

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

src/shared/__tests__/combineCommandSequences.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ const messages: ClineMessage[] = [
4040
describe("combineCommandSequences", () => {
4141
it("should combine command sequences", () => {
4242
const message = combineCommandSequences(messages).at(-1)
43-
expect(message!.text!.length).toEqual(131)
43+
expect(message!.text).toEqual(
44+
"ping www.google.com\nOutput:PING www.google.com (142.251.46.228): 56 data bytes\n",
45+
)
4446
})
4547
})

src/shared/combineCommandSequences.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { ClineMessage } from "./ExtensionMessage"
22

3+
export const COMMAND_OUTPUT_STRING = "Output:"
4+
35
/**
46
* Combines sequences of command and command_output messages in an array of ClineMessages.
57
*
@@ -27,30 +29,28 @@ export function combineCommandSequences(messages: ClineMessage[]): ClineMessage[
2729
for (let i = 0; i < messages.length; i++) {
2830
if (messages[i].type === "ask" && messages[i].ask === "command") {
2931
let combinedText = messages[i].text || ""
30-
let didAddOutput = false
3132
let j = i + 1
33+
let previous: { type: "ask" | "say"; text: string } | undefined
3234

3335
while (j < messages.length) {
34-
if (messages[j].type === "ask" && messages[j].ask === "command") {
35-
// Stop if we encounter the next command.
36-
break
36+
const { type, ask, say, text = "" } = messages[j]
37+
38+
if (type === "ask" && ask === "command") {
39+
break // Stop if we encounter the next command.
3740
}
3841

39-
if (messages[j].ask === "command_output" || messages[j].say === "command_output") {
40-
if (!didAddOutput) {
41-
// Add a newline before the first output.
42+
if (ask === "command_output" || say === "command_output") {
43+
if (!previous) {
4244
combinedText += `\n${COMMAND_OUTPUT_STRING}`
43-
didAddOutput = true
4445
}
4546

46-
// Handle cases where we receive empty command_output (i.e.
47-
// when extension is relinquishing control over exit command
48-
// button).
49-
const output = messages[j].text || ""
47+
const isDuplicate = previous && previous.type !== type && previous.text === text
5048

51-
if (output.length > 0) {
52-
combinedText += output
49+
if (text.length > 0 && !isDuplicate) {
50+
combinedText += text
5351
}
52+
53+
previous = { type, text }
5454
}
5555

5656
j++
@@ -109,5 +109,3 @@ export const splitCommandOutput = (text: string) => {
109109
.join(""),
110110
}
111111
}
112-
113-
export const COMMAND_OUTPUT_STRING = "Output:"

0 commit comments

Comments
 (0)