Skip to content
Myr

Remove input module (moved to another repo) and update input example

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The input module is now in github.com/charmbracelet/teaparty/input

authored by Christian Rocha · committed by Christian Rocha · parent df52853 · Browse files · 4 files+13 added−97 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+13 added−97 removed

  • other:+13 added−97 removed

4 files+13 added−97 removed

added:examples/input/go.mod+5 added
1module input-example23go 1.1345require github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9 // indirect
added:examples/input/go.sum+6 added
1github.com/charmbracelet/tea v0.0.0-20200118154546-df52853f9d94 h1:m2xhUqOw6OcefbPBR9Il0J0n0gB1663NoKU+vvkiLdU=2github.com/charmbracelet/tea v0.0.0-20200118154546-df52853f9d94/go.mod h1:lijy1lXOKNwMjBu/jTT/DvR8yE9PhtX2olGFsCz9/Vk=3github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9 h1:YQvJgppGVexnzIJ+KJlK9lBYA3+zXfdqZO/5Ngedtb0=4github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9/go.mod h1:z8JWtuxM0oA+dZfi7BkgBW2YGbyOTbWAixFs46W3SK4=5github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0=6github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ=
modified:examples/input/main.go+2 added-2 removed
4 unmodified lines
5import (6 "errors"7 "fmt"8 "log"9 "tea"911 "tea/input"10 "github.com/charmbracelet/tea"11 "github.com/charmbracelet/teaparty/input"12)1314type Model struct {15 Input input.Model
76 unmodified lines
deleted:input/input.go-95 removed
1package input23import (4 "tea"5 "time"6)78type Model struct {9 Prompt string10 Value string11 Cursor string12 HiddenCursor string13 BlinkSpeed time.Duration1415 blink bool16 pos int17}1819type CursorBlinkMsg struct{}2021func DefaultModel() Model {22 return Model{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) {33 switch msg := msg.(type) {3435 case tea.KeyMsg:36 switch msg.Type {37 case tea.KeyBackspace:38 if len(m.Value) > 0 {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: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:62 m.blink = !m.blink63 return m, nil6465 default:66 return m, nil67 }68}6970func View(model tea.Model) string {71 m, _ := model.(Model)72 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 }79 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{}95}