mirror of
https://github.com/Maks1mS/bubbles.git
synced 2025-01-11 22:41:03 +03:00
Rename input
to textinput
+ DefaultModel is now NewModel
This commit is contained in:
parent
a2ae308e68
commit
bf2d13df66
@ -5,7 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/charmbracelet/tea"
|
"github.com/charmbracelet/tea"
|
||||||
"github.com/charmbracelet/teaparty/input"
|
input "github.com/charmbracelet/teaparty/textinput"
|
||||||
te "github.com/muesli/termenv"
|
te "github.com/muesli/termenv"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -39,17 +39,17 @@ type Model struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func initialize() (tea.Model, tea.Cmd) {
|
func initialize() (tea.Model, tea.Cmd) {
|
||||||
name := input.DefaultModel()
|
name := input.NewModel()
|
||||||
name.Placeholder = "Name"
|
name.Placeholder = "Name"
|
||||||
name.Focus()
|
name.Focus()
|
||||||
name.Prompt = focusedPrompt
|
name.Prompt = focusedPrompt
|
||||||
name.TextColor = focusedText
|
name.TextColor = focusedText
|
||||||
|
|
||||||
nickName := input.DefaultModel()
|
nickName := input.NewModel()
|
||||||
nickName.Placeholder = "Nickname"
|
nickName.Placeholder = "Nickname"
|
||||||
nickName.Prompt = blurredPrompt
|
nickName.Prompt = blurredPrompt
|
||||||
|
|
||||||
email := input.DefaultModel()
|
email := input.NewModel()
|
||||||
email.Placeholder = "Email"
|
email.Placeholder = "Email"
|
||||||
email.Prompt = blurredPrompt
|
email.Prompt = blurredPrompt
|
||||||
|
|
||||||
@ -134,22 +134,30 @@ func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
|
|||||||
return m, nil
|
return m, nil
|
||||||
|
|
||||||
default:
|
default:
|
||||||
m.nameInput, _ = input.Update(msg, m.nameInput)
|
// Handle character input
|
||||||
m.nickNameInput, _ = input.Update(msg, m.nickNameInput)
|
m = updateInputs(msg, m)
|
||||||
m.emailInput, _ = input.Update(msg, m.emailInput)
|
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
m.nameInput, _ = input.Update(msg, m.nameInput)
|
// Handle blinks
|
||||||
m.nickNameInput, _ = input.Update(msg, m.nickNameInput)
|
m = updateInputs(msg, m)
|
||||||
m.emailInput, _ = input.Update(msg, m.emailInput)
|
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func updateInputs(msg tea.Msg, m Model) Model {
|
||||||
|
m.nameInput, _ = input.Update(msg, m.nameInput)
|
||||||
|
m.nickNameInput, _ = input.Update(msg, m.nickNameInput)
|
||||||
|
m.emailInput, _ = input.Update(msg, m.emailInput)
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
func subscriptions(model tea.Model) tea.Subs {
|
func subscriptions(model tea.Model) tea.Subs {
|
||||||
return tea.Subs{
|
return tea.Subs{
|
||||||
|
// It's a little hacky, but we're using the subscription from one
|
||||||
|
// input element to handle the blinking for all elements. It doesn't
|
||||||
|
// have to be this way, we're just feeling a bit lazy at the moment.
|
||||||
"blink": func(model tea.Model) tea.Msg {
|
"blink": func(model tea.Model) tea.Msg {
|
||||||
m, _ := model.(Model)
|
m, _ := model.(Model)
|
||||||
return input.Blink(m.nameInput)
|
return input.Blink(m.nameInput)
|
@ -149,7 +149,7 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
|||||||
func View(model tea.Model) string {
|
func View(model tea.Model) string {
|
||||||
m, ok := model.(Model)
|
m, ok := model.(Model)
|
||||||
if !ok {
|
if !ok {
|
||||||
return ""
|
return "could not perform assertion on model"
|
||||||
}
|
}
|
||||||
switch m.Type {
|
switch m.Type {
|
||||||
case Dots:
|
case Dots:
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package input
|
package textinput
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@ -9,12 +9,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// Helper for returning colors
|
// color is a helper for returning colors
|
||||||
color func(s string) termenv.Color = termenv.ColorProfile().Color
|
color func(s string) termenv.Color = termenv.ColorProfile().Color
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ErrMsg indicates there's been an error. We don't handle errors in the this
|
||||||
|
// package; we're expecting errors to be handle in the program that implements
|
||||||
|
// this text input.
|
||||||
type ErrMsg error
|
type ErrMsg error
|
||||||
|
|
||||||
|
// Model is the Tea model for this text input element
|
||||||
type Model struct {
|
type Model struct {
|
||||||
Err error
|
Err error
|
||||||
Prompt string
|
Prompt string
|
||||||
@ -73,9 +77,11 @@ func (m *Model) colorPlaceholder(s string) string {
|
|||||||
String()
|
String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CursorBlinkMsg is sent when the cursor should alternate it's blinking state
|
||||||
type CursorBlinkMsg struct{}
|
type CursorBlinkMsg struct{}
|
||||||
|
|
||||||
func DefaultModel() Model {
|
// NewModel creates a new model with default settings
|
||||||
|
func NewModel() Model {
|
||||||
return Model{
|
return Model{
|
||||||
Prompt: "> ",
|
Prompt: "> ",
|
||||||
Value: "",
|
Value: "",
|
||||||
@ -84,6 +90,7 @@ func DefaultModel() Model {
|
|||||||
TextColor: "",
|
TextColor: "",
|
||||||
PlaceholderColor: "240",
|
PlaceholderColor: "240",
|
||||||
CursorColor: "",
|
CursorColor: "",
|
||||||
|
CharLimit: 0,
|
||||||
|
|
||||||
focus: false,
|
focus: false,
|
||||||
blink: true,
|
blink: true,
|
||||||
@ -91,6 +98,7 @@ func DefaultModel() Model {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update is the Tea update loop
|
||||||
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
||||||
if !m.focus {
|
if !m.focus {
|
||||||
m.blink = true
|
m.blink = true
|
||||||
@ -123,7 +131,7 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
|||||||
fallthrough
|
fallthrough
|
||||||
case tea.KeyCtrlB: // ^B, back one charcter
|
case tea.KeyCtrlB: // ^B, back one charcter
|
||||||
fallthrough
|
fallthrough
|
||||||
case tea.KeyCtrlA: // ^A, beginning
|
case tea.KeyCtrlA: // ^A, go to beginning
|
||||||
m.pos = 0
|
m.pos = 0
|
||||||
return m, nil
|
return m, nil
|
||||||
case tea.KeyCtrlD: // ^D, delete char under cursor
|
case tea.KeyCtrlD: // ^D, delete char under cursor
|
||||||
@ -131,7 +139,7 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
|||||||
m.Value = m.Value[:m.pos] + m.Value[m.pos+1:]
|
m.Value = m.Value[:m.pos] + m.Value[m.pos+1:]
|
||||||
}
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
case tea.KeyCtrlE: // ^E, end
|
case tea.KeyCtrlE: // ^E, go to end
|
||||||
m.pos = len(m.Value)
|
m.pos = len(m.Value)
|
||||||
return m, nil
|
return m, nil
|
||||||
case tea.KeyCtrlK: // ^K, kill text after cursor
|
case tea.KeyCtrlK: // ^K, kill text after cursor
|
||||||
@ -165,8 +173,12 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// View renders the textinput in its current state
|
||||||
func View(model tea.Model) string {
|
func View(model tea.Model) string {
|
||||||
m, _ := model.(Model)
|
m, ok := model.(Model)
|
||||||
|
if !ok {
|
||||||
|
return "could not perform assertion on model"
|
||||||
|
}
|
||||||
|
|
||||||
// Placeholder text
|
// Placeholder text
|
||||||
if m.Value == "" && m.Placeholder != "" {
|
if m.Value == "" && m.Placeholder != "" {
|
||||||
@ -184,6 +196,7 @@ func View(model tea.Model) string {
|
|||||||
return m.Prompt + v
|
return m.Prompt + v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// placeholderView
|
||||||
func placeholderView(m Model) string {
|
func placeholderView(m Model) string {
|
||||||
var (
|
var (
|
||||||
v string
|
v string
|
||||||
@ -206,19 +219,19 @@ func placeholderView(m Model) string {
|
|||||||
return m.Prompt + v
|
return m.Prompt + v
|
||||||
}
|
}
|
||||||
|
|
||||||
// Style the cursor
|
// cursorView style the cursor
|
||||||
func cursorView(s string, m Model) string {
|
func cursorView(s string, m Model) string {
|
||||||
if m.blink {
|
if m.blink {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
return termenv.String(s).
|
return termenv.String(s).
|
||||||
Foreground(color(m.CursorColor)).
|
Foreground(color(m.CursorColor)).
|
||||||
Reverse().
|
Reverse().
|
||||||
String()
|
String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subscription
|
// Blink is the subscription that lets us know when to alternate the blinking
|
||||||
|
// of the cursor.
|
||||||
func Blink(model tea.Model) tea.Msg {
|
func Blink(model tea.Model) tea.Msg {
|
||||||
m, ok := model.(Model)
|
m, ok := model.(Model)
|
||||||
if !ok {
|
if !ok {
|
Loading…
Reference in New Issue
Block a user