mirror of
https://github.com/taigrr/bubbletea.git
synced 2026-04-13 16:28:26 -07:00
Remove input module (moved to another repo) and update input example
The input module is now in github.com/charmbracelet/teaparty/input
This commit is contained in:
5
examples/input/go.mod
Normal file
5
examples/input/go.mod
Normal file
@@ -0,0 +1,5 @@
|
||||
module input-example
|
||||
|
||||
go 1.13
|
||||
|
||||
require github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9 // indirect
|
||||
6
examples/input/go.sum
Normal file
6
examples/input/go.sum
Normal file
@@ -0,0 +1,6 @@
|
||||
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/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9 h1:YQvJgppGVexnzIJ+KJlK9lBYA3+zXfdqZO/5Ngedtb0=
|
||||
github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9/go.mod h1:z8JWtuxM0oA+dZfi7BkgBW2YGbyOTbWAixFs46W3SK4=
|
||||
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=
|
||||
@@ -6,9 +6,9 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"tea"
|
||||
|
||||
"tea/input"
|
||||
"github.com/charmbracelet/tea"
|
||||
"github.com/charmbracelet/teaparty/input"
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
|
||||
@@ -1,95 +0,0 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"tea"
|
||||
"time"
|
||||
)
|
||||
|
||||
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{}
|
||||
}
|
||||
Reference in New Issue
Block a user