Skip to content
Myr

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

2 files+53 added−16 removed

modified:input/input.go+48 added-16 removed
8 unmodified lines
9 Prompt string10 Value string11 Cursor string12 HiddenCursor string13 Blink bool13 BlinkSpeed time.Duration1415 blink bool16 pos int17}1819type CursorBlinkMsg struct{}2021func 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,2627 blink: false,28 pos: 0,29 }30}3132func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
2 unmodified lines
35 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 }6061 case CursorBlinkMsg:44 m.Blink = !m.Blink45 }62 m.blink = !m.blink63 return m, nil6465 default:47 return m, nil66 return m, nil67 }68}6970func 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}8182// Style the cursor83func cursor(s string, blink bool) string {84 if blink {85 return s86 }87 return tea.Invert(s)88}8990// Subscription91func Blink(model tea.Model) tea.Msg {92 m, _ := model.(Model)93 time.Sleep(m.BlinkSpeed)94 return CursorBlinkMsg{}
1 unmodified line
modified:tea.go+5 added
211 unmodified lines
212func ClearScreen() {213 fmt.Printf(esc + "2J" + esc + "3J" + esc + "1;1H")214}215216// Invert inverts the foreground and background colors of a given string217func Invert(s string) string {218 return esc + "7m" + s + esc + "0m"219}220221// 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.log
8 unmodified lines