Convert remaining examples to Model interface-based structure

This commit is contained in:
Christian Rocha
2020-10-15 19:48:42 -04:00
parent 7530fb0112
commit 847787e46d
8 changed files with 78 additions and 138 deletions

View File

@@ -12,9 +12,7 @@ import (
te "github.com/muesli/termenv"
)
const (
focusedTextColor = "205"
)
const focusedTextColor = "205"
var (
color = te.ColorProfile().Color
@@ -25,11 +23,7 @@ var (
)
func main() {
if err := tea.NewProgram(
initialize,
update,
view,
).Start(); err != nil {
if err := tea.NewProgram(initialModel()).Start(); err != nil {
fmt.Printf("could not start program: %s\n", err)
os.Exit(1)
}
@@ -43,7 +37,7 @@ type model struct {
submitButton string
}
func initialize() (tea.Model, tea.Cmd) {
func initialModel() model {
name := input.NewModel()
name.Placeholder = "Name"
name.Focus()
@@ -58,25 +52,21 @@ func initialize() (tea.Model, tea.Cmd) {
email.Placeholder = "Email"
email.Prompt = blurredPrompt
return model{0, name, nickName, email, blurredSubmitButton},
tea.Batch(
input.Blink(name),
input.Blink(nickName),
input.Blink(email),
)
return model{0, name, nickName, email, blurredSubmitButton}
}
func (m model) Init() tea.Cmd {
return tea.Batch(
input.Blink(m.nameInput),
input.Blink(m.nickNameInput),
input.Blink(m.emailInput),
)
}
func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
m, ok := mdl.(model)
if !ok {
panic("could not perform assertion on model")
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
@@ -167,12 +157,7 @@ func updateInputs(msg tea.Msg, m model) (model, tea.Cmd) {
return m, tea.Batch(cmds...)
}
func view(mdl tea.Model) string {
m, ok := mdl.(model)
if !ok {
return "could not perform assertion on model"
}
func (m model) View() string {
s := "\n"
inputs := []string{
@@ -189,6 +174,5 @@ func view(mdl tea.Model) string {
}
s += "\n\n" + m.submitButton + "\n"
return s
}