Left/right arrows move the text input cursor
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Christian Rocha · committed by Christian Rocha · parent 4b64a7c · Browse files · 2 files·+53 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+53 added−16 removed
- other:+53 added−16 removed
Provenance
- Commit
9909356- Tree
3e08d78- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
4b64a7c
2 files changed+53 added−16 removed
modified:input/input.go+48 added-16 removed
8 unmodified lines9 Prompt string10 Value string11 Cursor string12 HiddenCursor string13- Blink bool13 BlinkSpeed time.Duration14+15+ blink bool16+ pos int17 }18 19 type CursorBlinkMsg struct{}20 21 func DefaultModel() Model {22 return Model{21- Prompt: "> ",22- Value: "",23- Cursor: "█",24- HiddenCursor: " ",25- Blink: false,26- BlinkSpeed: time.Second,23+ Prompt: "> ",24+ Value: "",25+ BlinkSpeed: time.Millisecond * 600,26+27+ blink: false,28+ pos: 0,29 }30 }31 32 func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {2 unmodified lines35 case tea.KeyMsg:36 switch msg.Type {37 case tea.KeyBackspace:38 if len(m.Value) > 0 {37- m.Value = m.Value[:len(m.Value)-1]39+ m.Value = m.Value[:m.pos-1] + m.Value[m.pos:]40+ m.pos--41 }42+ return m, nil43+ case tea.KeyLeft:44+ if m.pos > 0 {45+ m.pos--46+ }47+ return m, nil48+ case tea.KeyRight:49+ if m.pos < len(m.Value) {50+ m.pos++51+ }52+ return m, nil53 case tea.KeyRune:40- m.Value = m.Value + msg.String()54+ m.Value = m.Value[:m.pos] + msg.String() + m.Value[m.pos:]55+ m.pos++56+ return m, nil57+ default:58+ return m, nil59 }60 61 case CursorBlinkMsg:44- m.Blink = !m.Blink45- }62+ m.blink = !m.blink63+ return m, nil64 65+ default:47- return m, nil66+ return m, nil67+ }68 }69 70 func View(model tea.Model) string {71 m, _ := model.(Model)52- cursor := m.Cursor53- if m.Blink {54- cursor = m.HiddenCursor72+ v := m.Value[:m.pos]73+ if m.pos < len(m.Value) {74+ v += cursor(string(m.Value[m.pos]), m.blink)75+ v += m.Value[m.pos+1:]76+ } else {77+ v += cursor(" ", m.blink)78 }56- return m.Prompt + m.Value + cursor79+ return m.Prompt + v80 }81 82+// Style the cursor83+func cursor(s string, blink bool) string {84+ if blink {85+ return s86+ }87+ return tea.Invert(s)88+}89+90+// Subscription91 func Blink(model tea.Model) tea.Msg {92 m, _ := model.(Model)93 time.Sleep(m.BlinkSpeed)94 return CursorBlinkMsg{}1 unmodified linemodified:tea.go+5 added
211 unmodified lines212 func ClearScreen() {213 fmt.Printf(esc + "2J" + esc + "3J" + esc + "1;1H")214 }215 216+// Invert inverts the foreground and background colors of a given string217+func Invert(s string) string {218+ return esc + "7m" + s + esc + "0m"219+}220+221 // UseSysLog logs to the system log. This becomes helpful when debugging since222 // we can't easily print to the terminal since our TUI is occupying it!223 //224 // On macOS this is a just a matter of: tail -f /var/log/system.log8 unmodified lines