Skip to content

Commit

Permalink
fixes #1, fixes #2, this should take care of it
Browse files Browse the repository at this point in the history
  • Loading branch information
terminaldweller committed May 12, 2024
1 parent 454800f commit eabdc60
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/url"
"os"
"reflect"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -120,9 +121,38 @@ func returnGeminiResponse(resp *genai.GenerateContentResponse) string {
return result
}

func extractLast256ColorEscapeCode(str string) (string, error) {
pattern := `\033\[38;5;(\d+)m`

r, err := regexp.Compile(pattern)
if err != nil {
return "", fmt.Errorf("failed to compile regular expression: %v", err)
}

matches := r.FindAllStringSubmatch(str, -1)
if len(matches) == 0 {
return "", nil // No 256-color escape codes found
}

lastMatch := matches[len(matches)-1]

return lastMatch[1], nil
}

func chunker(inputString string) []string {
chunks := strings.Split(inputString, "\n")

for count, chunk := range chunks {
lastColorCode, err := extractLast256ColorEscapeCode(chunk)
if err != nil {
continue
}

if count <= len(chunks)-2 {
chunks[count+1] = fmt.Sprintf("\033[38;5;%sm", lastColorCode) + chunks[count+1]
}
}

return chunks
}

Expand Down

0 comments on commit eabdc60

Please sign in to comment.