Skip to content
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

Making multiple inputs blink #459

Open
ic-768 opened this issue Jan 18, 2024 · 2 comments
Open

Making multiple inputs blink #459

ic-768 opened this issue Jan 18, 2024 · 2 comments

Comments

@ic-768
Copy link

ic-768 commented Jan 18, 2024

Hi all. I'm new to bubble tea and bubbles, and I'm trying to make multiple inputs blink, however I can only make the very first one work. All successive inputs don't have cursor blinking

Here's my code

package main

import (
	"fmt"
	"log"

	"github.com/charmbracelet/bubbles/textarea"
	"github.com/charmbracelet/bubbles/textinput"
	tea "github.com/charmbracelet/bubbletea"
)

func main() {
	p := tea.NewProgram(initialModel())
	if _, err := p.Run(); err != nil {
		log.Fatal(err)
	}
}

type (
	errMsg error
)

type model struct {
	nameInput textinput.Model
	noteInput textarea.Model

	step int
	err  error
}

func initialModel() model {
	nameInput := textinput.New()
	nameInput.Placeholder = "Dwight"
	nameInput.Focus()
	nameInput.CharLimit = 156
	nameInput.Width = 20

	noteInput := textarea.New()
	noteInput.Placeholder = "Make a note..."

	return model{
		nameInput: nameInput,
		noteInput: noteInput,
		err:       nil,
		step:      1,
	}
}

func (m model) Init() tea.Cmd {
	return tea.Batch(
		textinput.Blink,
		textarea.Blink, // Add the blinking command for the textarea
	)
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	var cmd tea.Cmd

	switch msg := msg.(type) {
	case tea.KeyMsg:
		switch msg.Type {
		// QUIT
		case tea.KeyCtrlC, tea.KeyEsc:
			return m, tea.Quit

		// INCREMENT STEP
		case tea.KeyEnter:
			m.step += 1
			return m, nil
		}

	case errMsg:
		m.err = msg
		return m, nil
	}

	switch m.step {
	case 1:
		m.nameInput, cmd = m.nameInput.Update(msg)
	case 2:
		m.nameInput.Blur()
		m.noteInput.Focus()
		m.noteInput, cmd = m.noteInput.Update(msg)
	}

	return m, cmd
}

func (m model) View() string {
	var s string = fmt.Sprintf(
		"What’s your name?\n\n%s",
		m.nameInput.View(),
	) + "\n"

	if m.step == 2 {
		s += fmt.Sprintf(
			"Tell me a story.\n\n%s",
			m.noteInput.View())
	}

	return s
}

nameInput's cursor blinks fine. But noteInput doesn't. Also changing noteInput to another textInput doesn't help.

@codingcn
Copy link

same issue

@naglis
Copy link
Contributor

naglis commented Feb 20, 2024

Suggestions:

  • Move nameInput.Blur()/noteInput.Focus() to where the step is incremented. Now these methods are called each time a key is pressed when noteInput is focused.
  • noteInput.Focus() returns a command. Handle it to get the cursor to blink.
@@ -66,6 +66,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 		// INCREMENT STEP
 		case tea.KeyEnter:
 			m.step += 1
+			if m.step == 2 {
+				m.nameInput.Blur()
+				return m, m.noteInput.Focus()
+			}
 			return m, nil
 		}
 
@@ -78,8 +82,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 	case 1:
 		m.nameInput, cmd = m.nameInput.Update(msg)
 	case 2:
-		m.nameInput.Blur()
-		m.noteInput.Focus()
 		m.noteInput, cmd = m.noteInput.Update(msg)
 	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants