diff --git a/input/input.go b/input/input.go index 07f5b48..09f735a 100644 --- a/input/input.go +++ b/input/input.go @@ -10,20 +10,22 @@ type Model struct { Value string Cursor string HiddenCursor string - Blink bool BlinkSpeed time.Duration + + blink bool + pos int } type CursorBlinkMsg struct{} func DefaultModel() Model { return Model{ - Prompt: "> ", - Value: "", - Cursor: "█", - HiddenCursor: " ", - Blink: false, - BlinkSpeed: time.Second, + Prompt: "> ", + Value: "", + BlinkSpeed: time.Millisecond * 600, + + blink: false, + pos: 0, } } @@ -34,28 +36,58 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) { switch msg.Type { case tea.KeyBackspace: if len(m.Value) > 0 { - m.Value = m.Value[:len(m.Value)-1] + m.Value = m.Value[:m.pos-1] + m.Value[m.pos:] + m.pos-- } + return m, nil + case tea.KeyLeft: + if m.pos > 0 { + m.pos-- + } + return m, nil + case tea.KeyRight: + if m.pos < len(m.Value) { + m.pos++ + } + return m, nil case tea.KeyRune: - m.Value = m.Value + msg.String() + m.Value = m.Value[:m.pos] + msg.String() + m.Value[m.pos:] + m.pos++ + return m, nil + default: + return m, nil } case CursorBlinkMsg: - m.Blink = !m.Blink - } + m.blink = !m.blink + return m, nil - return m, nil + default: + return m, nil + } } func View(model tea.Model) string { m, _ := model.(Model) - cursor := m.Cursor - if m.Blink { - cursor = m.HiddenCursor + v := m.Value[:m.pos] + if m.pos < len(m.Value) { + v += cursor(string(m.Value[m.pos]), m.blink) + v += m.Value[m.pos+1:] + } else { + v += cursor(" ", m.blink) } - return m.Prompt + m.Value + cursor + return m.Prompt + v } +// Style the cursor +func cursor(s string, blink bool) string { + if blink { + return s + } + return tea.Invert(s) +} + +// Subscription func Blink(model tea.Model) tea.Msg { m, _ := model.(Model) time.Sleep(m.BlinkSpeed) diff --git a/tea.go b/tea.go index a31acff..326c5a7 100644 --- a/tea.go +++ b/tea.go @@ -213,6 +213,11 @@ func ClearScreen() { fmt.Printf(esc + "2J" + esc + "3J" + esc + "1;1H") } +// Invert inverts the foreground and background colors of a given string +func Invert(s string) string { + return esc + "7m" + s + esc + "0m" +} + // UseSysLog logs to the system log. This becomes helpful when debugging since // we can't easily print to the terminal since our TUI is occupying it! //