Replies: 4 comments 1 reply
-
Hi! I think we can sort this one out fastest via chat — want to hop into our Slack and go from there? |
Beta Was this translation helpful? Give feedback.
1 reply
-
Hey. Did you get your answer? |
Beta Was this translation helpful? Give feedback.
0 replies
-
Hello, i have a sample, maybe you need it: https://github.com/fzdwx/infinite/blob/main/_examples/dyn_single_select/main.go |
Beta Was this translation helpful? Give feedback.
0 replies
-
A searchable list example package main
import (
"fmt"
tea "github.com/charmbracelet/bubbletea"
"os"
"strings"
)
type model struct {
choices []string // all items
cursor int // which to-do list item our cursor is pointing at
selected map[int]struct{} // which to-do items are selected
input string // user inputs
}
func initialModel(choices []string) model {
return model{
choices: choices,
// A map which indicates which choices are selected. We're using
// the map like a mathematical set. The keys refer to the indexes
// of the `choices` slice, above.
selected: make(map[int]struct{}),
input: "",
}
}
func (m model) Init() tea.Cmd {
// Just return `nil`, which means "no I/O right now, please."
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
// Is it a key press?
case tea.KeyMsg:
// Cool, what was the actual key pressed?
switch msg.String() {
// These keys should exit the program.
case "ctrl+c", "q":
return m, tea.Quit
// The "up" and "k" keys move the cursor up
case "up", "k":
if m.cursor > 0 {
m.cursor--
} else {
m.cursor = len(m.choices) - 1 // loop over to the cursor bottom
}
// The "down" and "j" keys move the cursor down
case "down", "j":
if m.cursor < len(m.choices)-1 {
m.cursor++
} else {
m.cursor = 0 // loop over to the cursor top
}
// The "enter" key and the spacebar (a literal space) toggle
// the selected state for the item that the cursor is pointing at.
case "enter", " ":
_, ok := m.selected[m.cursor]
if ok {
delete(m.selected, m.cursor)
} else {
m.selected[m.cursor] = struct{}{}
}
case "backspace":
// delete last char
if m.input != "" {
m.input = m.input[:len(m.input)-1]
}
default:
m.input += msg.String()
}
}
// Return the updated model to the Bubble Tea runtime for processing.
// Note that we're not returning a command.
return m, nil
}
func (m model) View() string {
// the header
s := fmt.Sprintf("Please input string: %s\n", m.input)
var choices []string
if len(m.input) > 0 {
for _, choice := range m.choices {
if strings.Contains(choice, m.input) {
choices = append(choices, choice)
}
}
} else {
choices = m.choices[:]
}
// Iterate over our choices
for i, choice := range choices {
// Is the cursor pointing at this choice?
cursor := " " // no cursor
if m.cursor == i {
cursor = ">" // cursor!
}
// Is this choice selected?
checked := " " // not selected
if _, ok := m.selected[i]; ok {
checked = "x" // selected!
}
// Render the row
s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)
}
// The footer
s += "\nPress q to quit.\n"
// Send the UI for rendering
return s
}
func main() {
choices := []string{"foo", "bar", "baz"}
p := tea.NewProgram(initialModel(choices))
if _, err := p.Run(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1)
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I'm trying to make a CLI utility with Bubbletea (and LipGloss + Bubbles) for some kind of full-text search on data from my database, but I can't implement the following principle (with the level of understanding of your library that I have now):
Enter
), the search is done (under the hood, in goroutine, for example);Enter
(orSpace
) on the selected item, it opens the browser and goes to the link (which was in the metadata of this item from my database);Esc
and you're back in the original screen (where the search bar);I've been studying Bubbletea for about a week now through the source code and the examples in all of your (and not only) repositories, but I can't figure it out and put it all together... Can you please give me a hint? Please... 😢
Beta Was this translation helpful? Give feedback.
All reactions