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

🐞 Release terminal without races, and accept stdin #170

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/output/tui/bubbletea964.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
//
// TODO: Remove this function once the issue is resolved.
func safeguardBubbletea964(in io.Reader) io.Reader {
if in == nil {
return nil
if in == nil || in == os.Stdin {
return in
}
if f, ok := in.(*os.File); ok {
if st, err := f.Stat(); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/output/tui/bubbletea964_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func TestSafeguardBubbletea964(t *testing.T) {
name: "dev null",
in: openFile(t, os.DevNull),
want: nil,
}, {
name: "stdin",
in: os.Stdin,
want: os.Stdin,
}}
for _, tc := range tcs {
tc := tc
Expand Down
12 changes: 5 additions & 7 deletions pkg/output/tui/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,9 @@ func (b *BubbleProgress) tickSpeed() tea.Cmd {

func (b *BubbleProgress) start() {
b.prog = progress.New(progress.WithDefaultGradient())
out := b.OutOrStdout()
b.tea = tea.NewProgram(b,
tea.WithInput(safeguardBubbletea964(b.InOrStdin())),
tea.WithOutput(out),
tea.WithOutput(b.OutOrStdout()),
)
b.quitChan = make(chan struct{})
go func() {
Expand All @@ -253,11 +252,6 @@ func (b *BubbleProgress) start() {
b.teaErr = err
}
close(b.quitChan)
if term.IsWriterTerminal(out) {
if err := t.ReleaseTerminal(); err != nil {
panic(err)
}
}
}()
}

Expand All @@ -269,6 +263,10 @@ func (b *BubbleProgress) stop() {
b.tea.Send(b.quitSignal())
<-b.quitChan

if term.IsWriterTerminal(b.OutOrStdout()) && b.teaErr == nil {
b.teaErr = b.tea.ReleaseTerminal()
}

b.tea = nil
b.quitChan = nil
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/output/tui/spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,9 @@ func (b *BubbleSpinner) start() {
spinner.WithSpinner(spinner.Meter),
spinner.WithStyle(spinnerStyle()),
)
out := b.OutOrStdout()
b.tea = tea.NewProgram(b,
tea.WithInput(safeguardBubbletea964(b.InOrStdin())),
tea.WithOutput(out),
tea.WithOutput(b.OutOrStdout()),
)
b.quitChan = make(chan struct{})
go func() {
Expand All @@ -90,9 +89,6 @@ func (b *BubbleSpinner) start() {
b.teaErr = err
}
close(b.quitChan)
if term.IsWriterTerminal(out) {
_ = t.ReleaseTerminal()
}
}()
}

Expand All @@ -104,6 +100,10 @@ func (b *BubbleSpinner) stop() {
b.tea.Quit()
<-b.quitChan

if term.IsWriterTerminal(b.OutOrStdout()) && b.teaErr == nil {
b.teaErr = b.tea.ReleaseTerminal()
}

b.tea = nil
b.quitChan = nil
endMsg := fmt.Sprintf("%s %s\n",
Expand Down
Loading