-
Notifications
You must be signed in to change notification settings - Fork 886
(v2) Generic model #1298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v2-exp
Are you sure you want to change the base?
(v2) Generic model #1298
Conversation
e9aea7c
to
95b0498
Compare
1deae97
to
a69f856
Compare
This conforms to the new generic bubbletea API. See charmbracelet/bubbletea#1298
This replace program options by exposing them in the Program struct.
We don't need to expose the context field in the Program struct. It's better to keep it private. People wishing to cancel the program should execution should use `p.Kill()` instead.
a69f856
to
787a9a2
Compare
Here's an example of rewriting the package main
import (
"fmt"
"os"
tea "github.com/charmbracelet/bubbletea/v2"
)
type model struct {
shape tea.CursorShape
blink bool
}
func (m model) describeCursor() string {
var adj, noun string
if m.blink {
adj = "blinking"
} else {
adj = "steady"
}
switch m.shape {
case tea.CursorBlock:
noun = "block"
case tea.CursorUnderline:
noun = "underline"
case tea.CursorBar:
noun = "bar"
}
return fmt.Sprintf("%s %s", adj, noun)
}
func initialModel() (model, tea.Cmd) {
m := model{blink: true}
return m, nil
}
func updateModel(m model, msg tea.Msg) (model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyPressMsg:
switch msg.String() {
case "ctrl+q", "q":
return m, tea.Quit
case "h", "left":
if m.shape == tea.CursorBlock && m.blink {
break
}
if m.blink {
m.shape--
}
m.blink = !m.blink
case "l", "right":
if m.shape == tea.CursorBar && !m.blink {
break
}
if !m.blink {
m.shape++
}
m.blink = !m.blink
}
}
return m, nil
}
func viewModel(m model) fmt.Stringer {
f := tea.NewFrame("Press left/right to change the cursor style, q or ctrl+c to quit." +
"\n\n" +
" <- This is the cursor (a " + m.describeCursor() + ")")
f.Cursor = tea.NewCursor(0, 2)
f.Cursor.Shape = m.shape
f.Cursor.Blink = m.blink
return f
}
func main() {
p := tea.Program[model]{
Init: initialModel,
Update: updateModel,
View: viewModel,
}
if err := p.Run(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v", err)
os.Exit(1)
}
} |
This conforms to the new generic bubbletea API. See charmbracelet/bubbletea#1298
a5b3e0d
to
67f8ede
Compare
a56439f
to
ff6833c
Compare
func NewProgram[T any](model Model[T]) *Program[T] { | ||
p := new(Program[T]) | ||
p.Init = model.Init | ||
p.Update = func(t T, msg Msg) (T, Cmd) { return any(t).(Model[T]).Update(msg) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line would me much simpler (and less runtime cost in every call to Update
, View
) if Model[T]
was a struct instead of an interface. See #1136 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The NewProgram implementation wraps the Update
and View
functions (which are called frequently in the render loop) in a costly way. See my proposal for an alternate representation of Model[T]
in #1136 (comment).
This proposal removes all program options and, instead, define them directly on the Program struct. This also changes the API and implement this proposal #1136