Closed
Conversation
e9aea7c to
95b0498
Compare
1deae97 to
a69f856
Compare
aymanbagabas
added a commit
to charmbracelet/bubbles
that referenced
this pull request
Jan 29, 2025
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
Member
Author
|
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)
}
} |
aymanbagabas
added a commit
to charmbracelet/bubbles
that referenced
this pull request
Jan 29, 2025
This conforms to the new generic bubbletea API. See charmbracelet/bubbletea#1298
a5b3e0d to
67f8ede
Compare
a56439f to
ff6833c
Compare
dolmen
reviewed
Mar 26, 2025
| 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.
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)
dolmen
suggested changes
Mar 26, 2025
There was a problem hiding this comment.
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).
Member
|
I still absolutely love this design. For now, however, in the spirit of keeping things tidy, I'm closing this PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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