Skip to content

Commit

Permalink
Better Performance (#22)
Browse files Browse the repository at this point in the history
* Refactor [Terminal] [Chats] Better Performance

- [+] refactor(chat.go): optimize GetHistory method to use strings.Builder for better performance and memory allocation
- [+] fix(chat.go): append newline character after each message in GetHistory method

* Chore [Terminal] [GenAI] Colorizing single asterisks

- [+] chore(genai.go): add support for colorizing single asterisks in the output
  • Loading branch information
H0llyW00dzZ authored Jan 4, 2024
1 parent c7ddc91 commit 8c51c18
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 7 additions & 5 deletions terminal/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ func (h *ChatHistory) AddMessage(user, text string) {
//
// string: A newline-separated string of all messages in the chat history.
func (h *ChatHistory) GetHistory() string {
// Create a new slice to hold messages without emoji prefixes
sanitizedMessages := make([]string, 0, len(h.Messages))
var builder strings.Builder

// Define the prefixes to be removed
prefixesToRemove := []string{YouNerd, AiNerd}
Expand All @@ -60,9 +59,12 @@ func (h *ChatHistory) GetHistory() string {
break // Assume only one prefix will match and then break the loop
}
}
sanitizedMessages = append(sanitizedMessages, sanitizedMsg)
// Optimized to use Builder.WriteString() for better performance and to avoid memory allocation overhead.
builder.WriteString(sanitizedMsg)
builder.WriteRune(NewLineChars) // Append a newline character after each message.
}

// Join the sanitized messages with a newline character
return strings.Join(sanitizedMessages, "\n")
// The builder.String() method returns the complete, concatenated chat history.
return builder.String()

}
2 changes: 2 additions & 0 deletions terminal/genai.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@ func printResponse(resp *genai.GenerateContentResponse) string {
colorPairs := []string{
"**", ColorGreen,
"`", ColorYellow,
"*", ColorCyan,
}

keepDelimiters := map[string]bool{
"**": false, // Remove double asterisks from the output
"`": true, // Keep single backticks in the output
"*": true, // Keep single asterisks in the output
}

colorized := Colorize(content, colorPairs, keepDelimiters)
Expand Down

0 comments on commit 8c51c18

Please sign in to comment.