mirror of
https://github.com/Maks1mS/bubbles.git
synced 2025-03-14 13:03:44 +03:00
Add placeholder text functionality on input fields
This commit is contained in:
parent
c25e6266d9
commit
ca91eb2e79
@ -12,11 +12,13 @@ type Model struct {
|
|||||||
Prompt string
|
Prompt string
|
||||||
Value string
|
Value string
|
||||||
Cursor string
|
Cursor string
|
||||||
HiddenCursor string
|
|
||||||
BlinkSpeed time.Duration
|
BlinkSpeed time.Duration
|
||||||
|
Placeholder string
|
||||||
|
PlaceholderColor string
|
||||||
|
|
||||||
blink bool
|
blink bool
|
||||||
pos int
|
pos int
|
||||||
|
colorProfile termenv.Profile
|
||||||
}
|
}
|
||||||
|
|
||||||
type CursorBlinkMsg struct{}
|
type CursorBlinkMsg struct{}
|
||||||
@ -26,9 +28,12 @@ func DefaultModel() Model {
|
|||||||
Prompt: "> ",
|
Prompt: "> ",
|
||||||
Value: "",
|
Value: "",
|
||||||
BlinkSpeed: time.Millisecond * 600,
|
BlinkSpeed: time.Millisecond * 600,
|
||||||
|
Placeholder: "",
|
||||||
|
PlaceholderColor: "240",
|
||||||
|
|
||||||
blink: false,
|
blink: false,
|
||||||
pos: 0,
|
pos: 0,
|
||||||
|
colorProfile: termenv.ColorProfile(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,7 +102,30 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
|||||||
|
|
||||||
func View(model tea.Model) string {
|
func View(model tea.Model) string {
|
||||||
m, _ := model.(Model)
|
m, _ := model.(Model)
|
||||||
|
|
||||||
|
// Placeholder text
|
||||||
|
if m.Value == "" && m.Placeholder != "" {
|
||||||
|
var v string
|
||||||
|
|
||||||
|
if m.blink {
|
||||||
|
v += cursor(
|
||||||
|
termenv.String(m.Placeholder[:1]).
|
||||||
|
Foreground(m.colorProfile.Color(m.PlaceholderColor)).
|
||||||
|
String(),
|
||||||
|
m.blink,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
v += cursor(m.Placeholder[:1], m.blink)
|
||||||
|
}
|
||||||
|
|
||||||
|
v += termenv.String(m.Placeholder[1:]).
|
||||||
|
Foreground(m.colorProfile.Color(m.PlaceholderColor)).
|
||||||
|
String()
|
||||||
|
return m.Prompt + v
|
||||||
|
}
|
||||||
|
|
||||||
v := m.Value[:m.pos]
|
v := m.Value[:m.pos]
|
||||||
|
|
||||||
if m.pos < len(m.Value) {
|
if m.pos < len(m.Value) {
|
||||||
v += cursor(string(m.Value[m.pos]), m.blink)
|
v += cursor(string(m.Value[m.pos]), m.blink)
|
||||||
v += m.Value[m.pos+1:]
|
v += m.Value[m.pos+1:]
|
||||||
@ -107,6 +135,34 @@ func View(model tea.Model) string {
|
|||||||
return m.Prompt + v
|
return m.Prompt + v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func placeholderView(m Model) string {
|
||||||
|
var (
|
||||||
|
v string
|
||||||
|
p = m.Placeholder
|
||||||
|
c = m.PlaceholderColor
|
||||||
|
color = m.colorProfile.Color
|
||||||
|
)
|
||||||
|
|
||||||
|
// Cursor
|
||||||
|
if m.blink {
|
||||||
|
v += cursor(
|
||||||
|
termenv.String(p[:1]).
|
||||||
|
Foreground(color(c)).
|
||||||
|
String(),
|
||||||
|
m.blink,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
v += cursor(p[:1], m.blink)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The rest of the palceholder text
|
||||||
|
v += termenv.String(p[1:]).
|
||||||
|
Foreground(color(c)).
|
||||||
|
String()
|
||||||
|
|
||||||
|
return m.Prompt + v
|
||||||
|
}
|
||||||
|
|
||||||
// Style the cursor
|
// Style the cursor
|
||||||
func cursor(s string, blink bool) string {
|
func cursor(s string, blink bool) string {
|
||||||
if blink {
|
if blink {
|
||||||
|
Loading…
Reference in New Issue
Block a user