Skip to content

Commit 473db75

Browse files
committed
feat: add place holder to TextInput
1 parent 5ec8f0d commit 473db75

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

styles/text.go

+11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const (
1212
Green HexColor = "#B0EE5F"
1313
Gray HexColor = "#808080"
1414
Red HexColor = "#FF5656"
15+
Black HexColor = "#000000"
1516
)
1617

1718
// Text applies a hex color to a given string and returns the styled string
@@ -25,3 +26,13 @@ func BoldText(text string, color HexColor) string {
2526
style := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color(string(color)))
2627
return style.Render(text)
2728
}
29+
30+
func Cursor(cursorChar string) string {
31+
cursorStyle := lipgloss.NewStyle().
32+
Bold(true). // Make cursor bold
33+
Reverse(true). // Reverse the foreground and background colors
34+
Background(lipgloss.Color(string(Black))). // White background
35+
Foreground(lipgloss.Color(string(White))) // Black foreground
36+
37+
return cursorStyle.Render(cursorChar)
38+
}

utils/text_input.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ import (
44
"fmt"
55

66
tea "github.com/charmbracelet/bubbletea"
7+
"github.com/initia-labs/weave/styles"
78
)
89

910
type TextInput struct {
10-
Text string
11-
Cursor int // Cursor position within the text
11+
Text string
12+
Cursor int // Cursor position within the text
13+
Placeholder string
1214
}
1315

1416
func NewTextInput() TextInput {
15-
return TextInput{Text: "", Cursor: 0}
17+
return TextInput{Text: "", Cursor: 0, Placeholder: "<todo: Jennie revisit placeholder>"}
1618
}
1719

1820
func (ti TextInput) Update(msg tea.Msg) (TextInput, tea.Cmd, bool) {
@@ -46,20 +48,19 @@ func (ti TextInput) Update(msg tea.Msg) (TextInput, tea.Cmd, bool) {
4648

4749
func (ti TextInput) View() string {
4850
var beforeCursor, cursorChar, afterCursor string
49-
50-
if ti.Cursor < len(ti.Text) {
51+
if len(ti.Text) == 0 {
52+
return styles.Text(ti.Placeholder, styles.Gray) + styles.Cursor(" ") + "\n\nPress Enter to submit, or Ctrl+c to quit."
53+
} else if ti.Cursor < len(ti.Text) {
5154
// Cursor is within the text
52-
beforeCursor = ti.Text[:ti.Cursor]
53-
cursorChar = ti.Text[ti.Cursor : ti.Cursor+1] // Character at the cursor
54-
afterCursor = ti.Text[ti.Cursor+1:] // Text after the cursor
55+
beforeCursor = styles.Text(ti.Text[:ti.Cursor], styles.White)
56+
cursorChar = styles.Cursor(ti.Text[ti.Cursor : ti.Cursor+1])
57+
afterCursor = styles.Text(ti.Text[ti.Cursor+1:], styles.White)
5558
} else {
5659
// Cursor is at the end of the text
57-
beforeCursor = ti.Text
58-
cursorChar = " " // Use a space to represent the cursor at the end
59-
afterCursor = "" // No text after the cursor
60+
beforeCursor = styles.Text(ti.Text, styles.White)
61+
cursorChar = styles.Cursor(" ")
6062
}
6163

62-
// Render the text with the cursor
63-
// Use reverse video for the cursor character to highlight it
64-
return fmt.Sprintf("%s\x1b[7m%s\x1b[0m%s\n\nPress Enter to submit, or Ctrl+c to quit.", beforeCursor, cursorChar, afterCursor)
64+
// Compose the full view string
65+
return fmt.Sprintf("%s%s%s\n\nPress Enter to submit, or Ctrl+c to quit.", beforeCursor, cursorChar, afterCursor)
6566
}

0 commit comments

Comments
 (0)