-
Notifications
You must be signed in to change notification settings - Fork 847
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
(v2) Generic model #1298
Open
aymanbagabas
wants to merge
27
commits into
v2-exp
Choose a base branch
from
v2-generics
base: v2-exp
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
(v2) Generic model #1298
Conversation
This file contains 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
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
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
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