Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
# Project specific
GrokCookies.swift
.plan/
credentials.json
credentials.json

.vscode/
22 changes: 20 additions & 2 deletions Sources/GrokCLI/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ struct MessageCommand: ParsableCommand {
if accumulatedMessage.isEmpty {
formatter.printResponse(response.message, webSearchResults: response.webSearchResults, xposts: response.xposts)
} else {
formatter.flushBuffer() // Ensure any remaining buffer is printed
formatter.printSources(webSearchResults: response.webSearchResults, xposts: response.xposts)
}
} else {
Expand Down Expand Up @@ -516,6 +517,7 @@ struct GrokCLI {
if accumulatedMessage.isEmpty {
formatter.printResponse(response.message, webSearchResults: response.webSearchResults, xposts: response.xposts)
} else {
formatter.flushBuffer() // Ensure any remaining buffer is printed
formatter.printSources(webSearchResults: response.webSearchResults, xposts: response.xposts)
}
} else {
Expand Down Expand Up @@ -927,6 +929,7 @@ struct GrokCLI {
if accumulatedMessage.isEmpty {
formatter.printResponse(response.message, webSearchResults: response.webSearchResults, xposts: response.xposts)
} else {
formatter.flushBuffer() // Ensure any remaining buffer is printed
formatter.printSources(webSearchResults: response.webSearchResults, xposts: response.xposts)
}
} else {
Expand Down Expand Up @@ -1054,6 +1057,7 @@ struct GrokCLI {
if accumulatedMessage.isEmpty {
formatter.printResponse(response.message, webSearchResults: response.webSearchResults, xposts: response.xposts)
} else {
formatter.flushBuffer() // Ensure any remaining buffer is printed
formatter.printSources(webSearchResults: response.webSearchResults, xposts: response.xposts)
}
} else {
Expand Down Expand Up @@ -1318,11 +1322,19 @@ struct GrokCLI {
// Output formatting
class OutputFormatter {
private let useMarkdown: Bool
private var markdownBuffer: String = ""

init(useMarkdown: Bool = false) {
self.useMarkdown = useMarkdown
}

func flushBuffer() {
if !markdownBuffer.isEmpty {
printMarkdown(markdownBuffer)
markdownBuffer = ""
}
}

func printResponse(_ response: String, conversationId: String? = nil, responseId: String? = nil, debug: Bool = false, webSearchResults: [WebSearchResult]? = nil, xposts: [XPost]? = nil) {
print("\n" + "Grok:".green.bold)

Expand Down Expand Up @@ -1360,7 +1372,10 @@ class OutputFormatter {
}

if useMarkdown {
printMarkdown(chunk)
markdownBuffer += chunk
if chunk.contains("\n") || isLast {
flushBuffer()
}
} else {
print(chunk, terminator: "")
}
Expand Down Expand Up @@ -1390,7 +1405,10 @@ class OutputFormatter {
print("\n" + "Grok:".green.bold, terminator: "")
}
if useMarkdown {
printMarkdown(chunk)
markdownBuffer += chunk
if chunk.contains("\n") {
flushBuffer()
}
} else {
print(chunk, terminator: "")
}
Expand Down