-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtext.go
88 lines (72 loc) · 2.42 KB
/
text.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package styles
import (
"strings"
"github.com/charmbracelet/lipgloss"
)
// Text applies a hex color to a given string and returns the styled string
func Text(text string, color HexColor) string {
style := lipgloss.NewStyle().Foreground(lipgloss.Color(string(color)))
return style.Render(text)
}
// BoldText applies bold and color styling to a string
func BoldText(text string, color HexColor) string {
style := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color(string(color)))
return style.Render(text)
}
func Cursor(cursorChar string) string {
cursorStyle := lipgloss.NewStyle().
Bold(true). // Make cursor bold
Reverse(true). // Reverse the foreground and background colors
Background(lipgloss.Color(string(Black))). // Black background
Foreground(lipgloss.Color(string(White))) // White foreground
return cursorStyle.Render(cursorChar)
}
type PromptStatus string
const (
Empty PromptStatus = "empty"
Completed PromptStatus = "completed"
Question PromptStatus = "question"
Information PromptStatus = "information"
)
var (
QuestionMark string = Text("? ", Cyan)
CorrectMark string = Text("✓ ", Green)
InformationMark string = Text("i ", Cyan)
)
// RenderPrompt highlights phrases in the text if they match any phrase in the highlights list
func RenderPrompt(text string, highlights []string, status PromptStatus) string {
prompt := ""
switch status {
case Question:
prompt += QuestionMark
case Completed:
prompt += CorrectMark
case Information:
prompt += InformationMark
}
// Iterate over each highlight phrase and replace it in the text
for _, highlight := range highlights {
if strings.Contains(text, highlight) {
// Apply Cyan color to the matching highlight
coloredHighlight := BoldText(highlight, Cyan)
text = strings.ReplaceAll(text, highlight, coloredHighlight)
}
}
// Return the prompt with the highlighted text
return prompt + text
}
type ResponseSeparator int64
const (
ArrowSeparator ResponseSeparator = iota
DotsSeparator
)
func RenderPreviousResponse(separator ResponseSeparator, question string, highlights []string, answer string) string {
var separatorString string
switch separator {
case ArrowSeparator:
separatorString = Text(" > ", Gray)
case DotsSeparator:
separatorString = Text(" ... ", Gray)
}
return RenderPrompt(question, highlights, Completed) + separatorString + answer + "\n"
}