Skip to content

Commit ff7ac57

Browse files
committed
fix: RenderPrompt add style to non Ansi rune
1 parent 45b50f1 commit ff7ac57

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

styles/text.go

+33-5
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,8 @@ func RenderPrompt(text string, highlights []string, status PromptStatus) string
109109
prompt += InformationMark
110110
}
111111

112-
text = DefaultText(text)
113-
114-
// Iterate over each highlight phrase and replace it in the text
115112
for _, highlight := range highlights {
116113
if strings.Contains(text, highlight) {
117-
// Apply Cyan color to the matching highlight
118114
coloredHighlight := ""
119115
if status == Information {
120116
coloredHighlight = BoldText(highlight, Ivory)
@@ -125,10 +121,42 @@ func RenderPrompt(text string, highlights []string, status PromptStatus) string
125121
}
126122
}
127123

128-
// Return the prompt with the highlighted text
124+
text = DefaultTextWithoutOverridingStyledText(text)
125+
129126
return prompt + text
130127
}
131128

129+
// Helper function to apply default styling without overriding existing styles
130+
func DefaultTextWithoutOverridingStyledText(text string) string {
131+
styledText := ""
132+
for _, line := range strings.Split(text, "\n") {
133+
// Split the line by ANSI escape codes to detect already styled substrings
134+
parts := splitPreservingANSI(line)
135+
for _, part := range parts {
136+
if !containsANSI(part) {
137+
// Only style the parts that don't already have styling
138+
part = DefaultText(part)
139+
}
140+
styledText += part
141+
}
142+
styledText += "\n"
143+
}
144+
return strings.TrimSuffix(styledText, "\n")
145+
}
146+
147+
// Utility functions to handle ANSI escape codes
148+
func splitPreservingANSI(text string) []string {
149+
// Implement splitting that preserves ANSI codes
150+
// This is a placeholder; you'll need to implement or use a library
151+
return []string{text}
152+
}
153+
154+
func containsANSI(text string) bool {
155+
// Check if the text contains ANSI escape codes
156+
// This is a placeholder; you'll need to implement or use a library
157+
return strings.Contains(text, "\x1b[")
158+
}
159+
132160
var (
133161
NoSeparator = ""
134162
ArrowSeparator = Text(" > ", Gray)

utils/text_input.go

+15-8
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,25 @@ import (
88
)
99

1010
type TextInput struct {
11-
Text string
12-
Cursor int // Cursor position within the text
13-
Placeholder string
11+
Text string
12+
Cursor int // Cursor position within the text
13+
Placeholder string
14+
ValidationFn func(string) bool
1415
}
1516

1617
func NewTextInput() TextInput {
1718
return TextInput{
18-
Text: "",
19-
Cursor: 0,
20-
Placeholder: "<todo: Jennie revisit placeholder>",
19+
Text: "",
20+
Cursor: 0,
21+
Placeholder: "<todo: Jennie revisit placeholder>",
22+
ValidationFn: NoOps,
2123
}
2224
}
2325

26+
func NoOps(c string) bool {
27+
return true
28+
}
29+
2430
func (ti TextInput) Update(msg tea.Msg) (TextInput, tea.Cmd, bool) {
2531
switch msg := msg.(type) {
2632
case tea.KeyMsg:
@@ -53,8 +59,9 @@ func (ti TextInput) Update(msg tea.Msg) (TextInput, tea.Cmd, bool) {
5359

5460
func (ti TextInput) View() string {
5561
var beforeCursor, cursorChar, afterCursor string
62+
bottomText := styles.Text("Press Enter to submit, or Ctrl+c to quit.", styles.Gray)
5663
if len(ti.Text) == 0 {
57-
return "\n" + styles.Text("> ", styles.Yellow) + styles.Text(ti.Placeholder, styles.Gray) + styles.Cursor(" ") + "\n\nPress Enter to submit, or Ctrl+c to quit."
64+
return "\n" + styles.Text("> ", styles.Yellow) + styles.Text(ti.Placeholder, styles.Gray) + styles.Cursor(" ") + "\n\n" + bottomText
5865
} else if ti.Cursor < len(ti.Text) {
5966
// Cursor is within the text
6067
beforeCursor = styles.Text(ti.Text[:ti.Cursor], styles.Ivory)
@@ -67,5 +74,5 @@ func (ti TextInput) View() string {
6774
}
6875

6976
// Compose the full view string
70-
return fmt.Sprintf("\n%s %s%s%s\n\nPress Enter to submit, or Ctrl+c to quit.", styles.Text(">", styles.Yellow), beforeCursor, cursorChar, afterCursor)
77+
return fmt.Sprintf("\n%s %s%s%s\n\n%s", styles.Text(">", styles.Yellow), beforeCursor, cursorChar, afterCursor, bottomText)
7178
}

0 commit comments

Comments
 (0)