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

Maintain exec output #1276

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

raphaelvigee
Copy link

@raphaelvigee raphaelvigee commented Dec 27, 2024

Ref #1275

@meowgorithm
Copy link
Member

Hey, Raphaël! Thanks for the PR. Do you have any sample code to help illustrate the issue? Providing it will help us expedite this one.

We're using exec to print above inline TUIs quite a lot but haven't encountered this issue, so any sample code would really help illustrate the problem.

@raphaelvigee
Copy link
Author

package main

import (
	"fmt"
	tea "github.com/charmbracelet/bubbletea"
	"io"
	"os"
	"strings"
)

type execCmd struct {
	w io.Writer
}

type execDoneMsg struct{}

func (e *execCmd) Run() error {
	for i := range 8 {
		e.w.Write([]byte(fmt.Sprintf("exec %v\n", i)))
	}

	return nil
}

func (e *execCmd) SetStdin(r io.Reader) {
}

func (e *execCmd) SetStdout(w io.Writer) {
	e.w = w
}

func (e *execCmd) SetStderr(w io.Writer) {
}

type model struct{}

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

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

	switch msg := msg.(type) {
	case tea.KeyMsg:
		switch msg.String() {
		case "ctrl+c", "q":
			return m, tea.Quit
		}
	case execDoneMsg:
		cmds = append(cmds, tea.Quit)
	}

	cmds = append(cmds, cmd)

	return m, tea.Batch(cmds...)
}

func (m model) View() string {
	var sb strings.Builder

	for i := range 5 {
		sb.WriteString(fmt.Sprintf("view %v\n", i))
	}

	return sb.String()
}

func main() {
	initialModel := model{}

	p := tea.NewProgram(initialModel)

	go func() {
		p.Send(
			tea.Exec(&execCmd{}, func(err error) tea.Msg {
				return execDoneMsg{}
			})(),
		)
	}()

	if _, err := p.Run(); err != nil {
		fmt.Println("Error:", err)
		os.Exit(1)
	}
}

Current:

view 0
view 1
view 2
view 3
view 4
exec 0
exec 1
exec 2
view 0
view 1
view 2
view 3
view 4

i only see 3 lines from exec, but i expect to see all 8 lines that were printed

With fix:

view 0
view 1
view 2
view 3
view 4
exec 0
exec 1
exec 2
exec 3
exec 4
exec 5
exec 6
exec 7
view 0
view 1
view 2
view 3
view 4

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

Successfully merging this pull request may close these issues.

2 participants