Comments (6)
Glad you got it working!
I'll paste the example code for confirmation prompt here for others:
// Package main demonstrates how promptkit can be used as a bubbletea widget.
package main
import (
"fmt"
"os"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/erikgeiser/promptkit"
"github.com/erikgeiser/promptkit/confirmation"
)
type confirmationPrompt struct {
prompt string
selection bool
confirmation *confirmation.Model
err error
}
func newConfirmationPrompt(prompt string) *confirmationPrompt {
return &confirmationPrompt{prompt: prompt}
}
var _ tea.Model = &confirmationPrompt{}
func (c *confirmationPrompt) Init() tea.Cmd {
// Here we show how to specify your own keyMap since the default uses left
// and right arrow keys which you may already be using.
keyMap := &confirmation.KeyMap{
Yes: []string{"y", "Y"},
No: []string{"n", "N"},
Toggle: []string{"tab"},
Submit: []string{"enter"},
Abort: []string{"ctrl+c"},
}
conf := &confirmation.Confirmation{
Prompt: c.prompt,
DefaultValue: confirmation.Undecided,
Template: confirmation.DefaultTemplate,
ResultTemplate: confirmation.DefaultResultTemplate,
KeyMap: keyMap,
WrapMode: promptkit.Truncate,
}
c.confirmation = confirmation.NewModel(conf)
return c.confirmation.Init()
}
func (c *confirmationPrompt) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
keyMsg, ok := msg.(tea.KeyMsg)
if !ok {
return c, nil
}
switch {
case keyMsg.String() == "enter":
selection, err := c.confirmation.Value()
if err != nil {
c.err = err
return c, tea.Quit
}
c.selection = selection
case keyMsg.String() == "esc":
return c, tea.Quit
default:
_, cmd := c.confirmation.Update(msg)
return c, cmd
}
return c, nil
}
func (c *confirmationPrompt) View() string {
if c.err != nil {
return fmt.Sprintf("Error: %v", c.err)
}
var b strings.Builder
b.WriteString(c.confirmation.View())
b.WriteString("\n=== You Chose: ===\n")
b.WriteString(fmt.Sprintf("%t", c.selection))
return b.String()
}
func main() {
model := newConfirmationPrompt("Would you like to go shopping?")
p := tea.NewProgram(model)
_, err := p.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v", err)
os.Exit(1)
}
}
from promptkit.
Here is an example of the confirmation prompt as a bubbletea
model/widget: https://github.com/erikgeiser/promptkit/blob/main/examples/bubbletea_widget/main.go
from promptkit.
Thanks! After playing around some more, looking at your example, and getting more familiar with BubbleTea and promptkit I think I figured it all out.
I'll close this for now and if needed I can always re-open with a specific question or issue.
Thank you for a wonderful model! Super useful and looks good. : )
from promptkit.
@erikgeiser https://github.com/erikgeiser/promptkit/blob/main/examples/bubbletea_widget/main.go uses Selection
not Confirmation
, no?
@kalensk do you mind sharing what you learned on how to fit Confirmation
into bubbltea?
from promptkit.
@ryuheechul Sure thing! Added #26 with an example for confirmation prompt. Feel free to not use the PR and close it since the example is so similar to the existing selection bubbletea example. I'll leave it to you.
from promptkit.
Thanks @kalensk! Now it's clear what I was doing wrong. I should have been looking for confirmation.Model.View
but I was looking for confirmation.View
instead. But that's crystal clear now thanks to your example code 👍🏼.
from promptkit.
Related Issues (15)
- Multiple Selections HOT 8
- Integrate with bubbletea HOT 5
- Update preview clips in README
- Choices example not refactored HOT 2
- Selection example not up-to-date HOT 2
- Tests fail with github.com/muesli/termenv v0.13.0 HOT 3
- Init page of paged choices are in a weird order
- Latest bubbles break promptkit HOT 5
- error: running prompt: unable to set console to raw mode: HOT 3
- selection.New(selection.Choice[T]) doesn't work HOT 7
- selection widget fails silently on empty choice list
- request: add text area HOT 1
- Is README outdated on Windows support? HOT 2
- Allow to browse suggestions in textinput
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from promptkit.