From c83a0bee59b96922786208289c4b9db93b82b023 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Sat, 18 Jan 2020 10:57:38 -0500 Subject: [PATCH] Add input module from github.com/charmbracelet/tea --- go.mod | 5 +++ go.sum | 4 +++ input/input.go | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++ teaparty.go | 1 + 4 files changed, 106 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 input/input.go create mode 100644 teaparty.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..65b68c4 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/charmbracelet/teaparty + +go 1.13 + +require github.com/charmbracelet/tea v0.0.0-20200118154546-df52853f9d94 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..0039d8e --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/charmbracelet/tea v0.0.0-20200118154546-df52853f9d94 h1:m2xhUqOw6OcefbPBR9Il0J0n0gB1663NoKU+vvkiLdU= +github.com/charmbracelet/tea v0.0.0-20200118154546-df52853f9d94/go.mod h1:lijy1lXOKNwMjBu/jTT/DvR8yE9PhtX2olGFsCz9/Vk= +github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0= +github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ= diff --git a/input/input.go b/input/input.go new file mode 100644 index 0000000..0381915 --- /dev/null +++ b/input/input.go @@ -0,0 +1,96 @@ +package input + +import ( + "time" + + "github.com/charmbracelet/tea" +) + +type Model struct { + Prompt string + Value string + Cursor string + HiddenCursor string + BlinkSpeed time.Duration + + blink bool + pos int +} + +type CursorBlinkMsg struct{} + +func DefaultModel() Model { + return Model{ + Prompt: "> ", + Value: "", + BlinkSpeed: time.Millisecond * 600, + + blink: false, + pos: 0, + } +} + +func Update(msg tea.Msg, m Model) (Model, tea.Cmd) { + switch msg := msg.(type) { + + case tea.KeyMsg: + switch msg.Type { + case tea.KeyBackspace: + if len(m.Value) > 0 { + 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[:m.pos] + msg.String() + m.Value[m.pos:] + m.pos++ + return m, nil + default: + return m, nil + } + + case CursorBlinkMsg: + m.blink = !m.blink + return m, nil + + default: + return m, nil + } +} + +func View(model tea.Model) string { + m, _ := model.(Model) + 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 + 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) + return CursorBlinkMsg{} +} diff --git a/teaparty.go b/teaparty.go new file mode 100644 index 0000000..e9e3cd5 --- /dev/null +++ b/teaparty.go @@ -0,0 +1 @@ +package teaparty