charmbracelet / charmbracelet/bubbletea
when start a exec, the first key press always be ignored, it seems that I need one press to "active" the io
- Dominant language
- Go
- Stars
- 44.9k
- Forks
- 1.3k
- PR merge metrics
- No merged PRs in 30d
Description
```go
package main
import (
"fmt"
"io"
"os"
"os/exec"
tea "github.com/charmbracelet/bubbletea"
)
type editorFinishedMsg struct{ err error }
func openEditor() tea.Cmd {
return tea.Exec(&echo{}, func(err error) tea.Msg { return err })
editor := os.Getenv("EDITOR")
if editor == "" {
editor = "vim"
}
c := exec.Command(editor) //nolint:gosec
return tea.ExecProcess(c, func(err error) tea.Msg {
return editorFinishedMsg{err}
})
}
type model struct {
altscreenActive bool
err error
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "a":
m.altscreenActive = !m.altscreenActive
cmd := tea.EnterAltScreen
if !m.altscreenActive {
cmd = tea.ExitAltScreen
}
return m, cmd
case "e":
// return m, tea.Exec(&echo{}, func(err error) tea.Msg { return err })
return m, tea.Sequence(openEditor())
case "ctrl+c", "q":
return m, tea.Quit
}
case editorFinishedMsg:
if msg.err != nil {
m.err = msg.err
return m, tea.Quit
}
}
return m, nil
}
func (m model) View() string {
if m.err != nil {
return "Error: " + m.err.Error() + "\n"
}
return "Press 'e' to open your EDITOR.\nPress 'a' to toggle the altscreen\nPress 'q' to quit.\n"
}
func main() {
m := model{}
if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}
type echo struct {
stdin io.Reader
stdout, stderr io.Writer
}
func (e *echo) Run() error {
fmt.Println(io.Copy(e.stdout, e.stdin))
return nil
}
func (e *echo) SetStdin(r io.Reader) {
if e.stdin == nil {
e.stdin = r
}
}
func (e *echo) SetStdout(w io.Writer) {
if e.stdin == nil {
e.stdout = w
}
}
func (e *echo) SetStderr(w io.Writer) {
if e.stdin == nil {
e.stdout = w
}
}
```
Contributor guide
Research direction
Run the provided Go reproduction and compare the behavior of tea.Exec with tea.ExecProcess, focusing on how stdin is handed to the command. Verify the fix by confirming that the first key press reaches the executed process without requiring an extra activation key press.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100