Add Button component

This commit is contained in:
Christian Muehlhaeuser 2020-10-30 08:29:45 +01:00
parent 9b47f26bdd
commit 34cd93d6b5
No known key found for this signature in database
GPG Key ID: 3CF9FA45CA1EBB7E
2 changed files with 95 additions and 0 deletions

View File

@ -36,6 +36,11 @@ the common, and many customization options.
* [Example code, many fields](https://github.com/charmbracelet/tea/tree/master/examples/textinputs/main.go) * [Example code, many fields](https://github.com/charmbracelet/tea/tree/master/examples/textinputs/main.go)
## Button
A button component.
## Paginator ## Paginator
<img src="https://stuff.charm.sh/bubbles-examples/pagination.gif" width="200" alt="Paginator Example"> <img src="https://stuff.charm.sh/bubbles-examples/pagination.gif" width="200" alt="Paginator Example">

90
button/button.go Normal file
View File

@ -0,0 +1,90 @@
package button
import (
tea "github.com/charmbracelet/bubbletea"
"github.com/muesli/termenv"
)
var (
// color is a helper for returning colors.
color func(s string) termenv.Color = termenv.ColorProfile().Color
)
// Model is the Bubble Tea model for a button element.
type Model struct {
Err error
Label string
Default bool
TextColor string
BackgroundColor string
FocusedTextColor string
FocusedBackgroundColor string
// Focus indicates whether user focus should be on this button component
focus bool
}
// NewModel creates a new model with default settings.
func NewModel() Model {
return Model{
Label: "Button",
}
}
// Update is the Tea update loop.
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
if !m.focus {
return m, nil
}
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "enter":
// TODO: implement
}
}
return m, nil
}
// View renders the button in its current state.
func (m Model) View() string {
margin := m.styled(" ").String()
label := m.styled(m.Label)
if m.Default {
label = label.Underline()
}
return margin + label.String() + margin
}
// Focused returns the focus state on the model.
func (m Model) Focused() bool {
return m.focus
}
// Focus sets the focus state on the model.
func (m *Model) Focus() {
m.focus = true
}
// Blur removes the focus state on the model.
func (m *Model) Blur() {
m.focus = false
}
func (m Model) styled(s string) termenv.Style {
view := termenv.String(s)
if m.focus {
view = view.Foreground(color(m.FocusedTextColor)).
Background(color(m.FocusedBackgroundColor))
} else {
view = view.Foreground(color(m.TextColor)).
Background(color(m.BackgroundColor))
}
return view
}