Skip to content

Commit

Permalink
Release terminal without races, and accept stdin (#170)
Browse files Browse the repository at this point in the history
Fixes:

 * #169
 * #168
  • Loading branch information
cardil committed Mar 27, 2024
1 parent fbd71a8 commit 6984d81
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
4 changes: 2 additions & 2 deletions pkg/output/tui/bubbletea964.go
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
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
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
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

0 comments on commit 6984d81

Please sign in to comment.