forked from Mirrors/bubbletea
Remove ErrMsg and ModelAssertionErr
This commit is contained in:
parent
aa6d766e42
commit
f93b752fcc
|
@ -15,11 +15,12 @@ import (
|
|||
const url = "https://charm.sh/"
|
||||
|
||||
type Model struct {
|
||||
Status int
|
||||
Error error
|
||||
status int
|
||||
err error
|
||||
}
|
||||
|
||||
type statusMsg int
|
||||
type errMsg error
|
||||
|
||||
func main() {
|
||||
p := tea.NewProgram(initialize, update, view, nil)
|
||||
|
@ -33,7 +34,10 @@ func initialize() (tea.Model, tea.Cmd) {
|
|||
}
|
||||
|
||||
func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
|
||||
m, _ := model.(Model)
|
||||
m, ok := model.(Model)
|
||||
if !ok {
|
||||
return Model{err: errors.New("could not perform assertion on model during update")}, nil
|
||||
}
|
||||
|
||||
switch msg := msg.(type) {
|
||||
|
||||
|
@ -50,12 +54,11 @@ func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
|
|||
}
|
||||
|
||||
case statusMsg:
|
||||
m.Status = int(msg)
|
||||
m.status = int(msg)
|
||||
return m, tea.Quit
|
||||
|
||||
case tea.ErrMsg:
|
||||
// TODO: get the error out of tea.ErrMsg less hackily
|
||||
m.Error = errors.New(msg.Error())
|
||||
case errMsg:
|
||||
m.err = msg
|
||||
return m, nil
|
||||
|
||||
default:
|
||||
|
@ -66,10 +69,10 @@ func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
|
|||
func view(model tea.Model) string {
|
||||
m, _ := model.(Model)
|
||||
s := fmt.Sprintf("Checking %s...", url)
|
||||
if m.Error != nil {
|
||||
s += fmt.Sprintf("something went wrong: %s", m.Error)
|
||||
} else if m.Status != 0 {
|
||||
s += fmt.Sprintf("%d %s", m.Status, http.StatusText(m.Status))
|
||||
if m.err != nil {
|
||||
s += fmt.Sprintf("something went wrong: %s", m.err)
|
||||
} else if m.status != 0 {
|
||||
s += fmt.Sprintf("%d %s", m.status, http.StatusText(m.status))
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
@ -80,7 +83,7 @@ func checkServer() tea.Msg {
|
|||
}
|
||||
res, err := c.Get(url)
|
||||
if err != nil {
|
||||
return tea.NewErrMsg(err.Error())
|
||||
return errMsg(err)
|
||||
}
|
||||
return statusMsg(res.StatusCode)
|
||||
}
|
||||
|
|
|
@ -12,15 +12,14 @@ import (
|
|||
)
|
||||
|
||||
type Model struct {
|
||||
Input input.Model
|
||||
Error error
|
||||
textInput input.Model
|
||||
err error
|
||||
}
|
||||
|
||||
type tickMsg struct{}
|
||||
type errMsg error
|
||||
|
||||
func main() {
|
||||
tea.UseSysLog("tea")
|
||||
|
||||
p := tea.NewProgram(
|
||||
initialize,
|
||||
update,
|
||||
|
@ -38,8 +37,8 @@ func initialize() (tea.Model, tea.Cmd) {
|
|||
inputModel.Placeholder = "Pikachu"
|
||||
|
||||
return Model{
|
||||
Input: inputModel,
|
||||
Error: nil,
|
||||
textInput: inputModel,
|
||||
err: nil,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
@ -50,8 +49,9 @@ func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
|
|||
// When we encounter errors in Update we simply add the error to the
|
||||
// model so we can handle it in the view. We could also return a command
|
||||
// that does something else with the error, like logs it via IO.
|
||||
m.Error = errors.New("could not perform assertion on model")
|
||||
return m, nil
|
||||
return Model{
|
||||
err: errors.New("could not perform assertion on model in update"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
switch msg := msg.(type) {
|
||||
|
@ -64,12 +64,12 @@ func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
|
|||
}
|
||||
|
||||
// We handle errors just like any other message
|
||||
case tea.ErrMsg:
|
||||
m.Error = msg
|
||||
case errMsg:
|
||||
m.err = msg
|
||||
return m, nil
|
||||
}
|
||||
|
||||
m.Input, cmd = input.Update(msg, m.Input)
|
||||
m.textInput, cmd = input.Update(msg, m.textInput)
|
||||
return m, cmd
|
||||
}
|
||||
|
||||
|
@ -79,7 +79,7 @@ func subscriptions(model tea.Model) tea.Subs {
|
|||
// it the model it expects.
|
||||
"input": func(model tea.Model) tea.Msg {
|
||||
m, _ := model.(Model)
|
||||
return input.Blink(m.Input)
|
||||
return input.Blink(m.textInput)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -88,12 +88,12 @@ func view(model tea.Model) string {
|
|||
m, ok := model.(Model)
|
||||
if !ok {
|
||||
return "Oh no: could not perform assertion on model."
|
||||
} else if m.Error != nil {
|
||||
return fmt.Sprintf("Uh oh: %s", m.Error)
|
||||
} else if m.err != nil {
|
||||
return fmt.Sprintf("Uh oh: %s", m.err)
|
||||
}
|
||||
return fmt.Sprintf(
|
||||
"What’s your favorite Pokémon?\n\n%s\n\n%s",
|
||||
input.View(m.Input),
|
||||
input.View(m.textInput),
|
||||
"(esc to quit)",
|
||||
)
|
||||
}
|
||||
|
|
27
tea.go
27
tea.go
|
@ -1,7 +1,6 @@
|
|||
package tea
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
@ -94,32 +93,6 @@ type Program struct {
|
|||
subscriptions Subscriptions
|
||||
}
|
||||
|
||||
// ErrMsg is just a regular message containing an error. We handle it in Update
|
||||
// just like a regular message by case switching. Of course, the developer
|
||||
// could also define her own errors as well.
|
||||
type ErrMsg struct {
|
||||
error
|
||||
}
|
||||
|
||||
// String implements String() on the error interface for ErrMsg
|
||||
func (e ErrMsg) String() string {
|
||||
return e.Error()
|
||||
}
|
||||
|
||||
// NewErrMsg is a convenience function for creating a generic ErrMsg
|
||||
func NewErrMsg(s string) ErrMsg {
|
||||
return ErrMsg{errors.New(s)}
|
||||
}
|
||||
|
||||
// NewErrMsgFromErr is a convenience function for creating an ErrMsg from an
|
||||
// existing error
|
||||
func NewErrMsgFromErr(e error) ErrMsg {
|
||||
return ErrMsg{e}
|
||||
}
|
||||
|
||||
// ModelAssertionErr can be used when a model assertion didn't go as planned
|
||||
var ModelAssertionErr = NewErrMsg("could not perform assertion on model")
|
||||
|
||||
// Quit is a command that tells the program to exit
|
||||
func Quit() Msg {
|
||||
return quitMsg{}
|
||||
|
|
Loading…
Reference in New Issue