Git Product home page Git Product logo

Comments (4)

curio77 avatar curio77 commented on July 17, 2024 7

Here you go… This example shows both lists at once, does without any fancy colors and such, and can be quit via Esc or Ctrl-C. Items can be toggled via Space or Enter. As it is, it lacks a means of submitting the selection, does not validate your constraints (selecting two flavors), and can surely be improved/optimized, but you should get the idea.

package main

import (
	"fmt"

	tea "github.com/charmbracelet/bubbletea"
)

func main() {
	m := &model{
		flavors: []item{
			{"orange", false},
			{"strawberry", false},
			{"watermelon", false},
			{"apple", false},
		},
		adds: []item{
			{"candies", false},
			{"mixed colors", false},
			{"strange glass shape", false},
		},
	}
	if err := tea.NewProgram(m).Start(); err != nil {
		panic(fmt.Sprintf("failed to run program: %v", err))
	}
}

type model struct {
	flavors, adds []item
	list, item    int
}

type item struct {
	text    string
	checked bool
}

func (m *model) Init() tea.Cmd {
	return nil
}

func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	switch typed := msg.(type) {
	case tea.KeyMsg:
		return m, m.handleKeyMsg(typed)
	}
	return m, nil
}

func (m *model) handleKeyMsg(msg tea.KeyMsg) tea.Cmd {
	switch msg.String() {
	case "esc", "ctrl+c":
		return tea.Quit
	case " ", "enter":
		switch m.list {
		case 0:
			m.flavors[m.item].checked = !m.flavors[m.item].checked
		case 1:
			m.adds[m.item].checked = !m.adds[m.item].checked
		}
	case "up":
		if m.item > 0 {
			m.item--
		} else if m.list > 0 {
			m.list--
			m.item = len(m.flavors) - 1
		}
	case "down":
		switch m.list {
		case 0:
			if m.item+1 < len(m.flavors) {
				m.item++
			} else {
				m.list++
				m.item = 0
			}
		case 1:
			if m.item+1 < len(m.adds) {
				m.item++
			}
		}
	}
	return nil
}

func (m *model) View() string {
	curFlavor, curAdd := -1, -1
	switch m.list {
	case 0:
		curFlavor = m.item
	case 1:
		curAdd = m.item
	}
	return m.renderList("choose two flavors", m.flavors, curFlavor) +
		"\n" +
		m.renderList("select adds", m.adds, curAdd)
}

func (m *model) renderList(header string, items []item, selected int) string {
	out := "~ " + header + ":\n"
	for i, item := range items {
		sel := " "
		if i == selected {
			sel = ">"
		}
		check := " "
		if items[i].checked {
			check = "✓"
		}
		out += fmt.Sprintf("%s [%s] %s\n", sel, check, item.text)
	}
	return out
}

from bubbletea.

curio77 avatar curio77 commented on July 17, 2024

I think you don't need multiple models, just have two separate structs/maps/slices/whatever for the respective selection state (checked items, current item) and a separate field that holds which of the two lists is active, both in the same model. From this, you can render the two lists consecutively (or, if that's what you're after, both at once with the current item moving between lists).

from bubbletea.

adrien-barret avatar adrien-barret commented on July 17, 2024

by chance do you get time to make a small example ? I tried that but it seems the view don't load and the keyboard movements get some issues as it go with two slices

from bubbletea.

adrien-barret avatar adrien-barret commented on July 17, 2024

thanks ! :)

from bubbletea.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.