Skip to content

Commit

Permalink
Hack around the issue that /dev/stdin doesn't support stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Mar 28, 2024
1 parent bce63da commit cf59659
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
10 changes: 9 additions & 1 deletion pkg/output/tui/bubbletea964.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ import (
//
// TODO: Remove this function once the issue is resolved.
func safeguardBubbletea964(in io.Reader) io.Reader {
if in == nil || in == os.Stdin {
if in == nil {
return in
}
if in == os.Stdin {
// this is not a *os.File, so it will not try to do the epoll stuff
return bubbletea964Input{Reader: in}
}
if f, ok := in.(*os.File); ok {
if st, err := f.Stat(); err != nil {
log.Fatal("unexpected: ", err)
Expand All @@ -47,3 +51,7 @@ func safeguardBubbletea964(in io.Reader) io.Reader {
}
return in
}

type bubbletea964Input struct {
io.Reader
}
14 changes: 6 additions & 8 deletions pkg/output/tui/bubbletea964_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ func TestSafeguardBubbletea964(t *testing.T) {
t.Parallel()
tmp := t.TempDir()
assert.NilError(t, os.WriteFile(tmp+"/file", []byte("test"), 0o600))
td := openFile(t, tmp)
tf := openFile(t, tmp+"/file")
tcs := []safeguardBubbletea964TestCase{{
name: "nil input",
in: nil,
want: nil,
}, {
name: "non-regular file",
in: os.NewFile(0, "/"),
in: os.NewFile(td.Fd(), "/"),
want: nil,
}, {
name: "regular file",
Expand All @@ -48,18 +49,12 @@ func TestSafeguardBubbletea964(t *testing.T) {
}, {
name: "stdin",
in: os.Stdin,
want: os.Stdin,
want: bubbletea964Input{Reader: os.Stdin},
}}
for _, tc := range tcs {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
if tf, ok := tc.in.(*os.File); ok {
defer func(tf *os.File) {
_ = tf.Close()
}(tf)
}

got := safeguardBubbletea964(tc.in)
assert.Equal(t, got, tc.want)
})
Expand All @@ -77,5 +72,8 @@ func openFile(tb testing.TB, name string) *os.File {
tb.Helper()
f, err := os.Open(name)
assert.NilError(tb, err)
tb.Cleanup(func() {
assert.NilError(tb, f.Close())
})
return f
}
4 changes: 1 addition & 3 deletions pkg/output/tui/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ import (

func ioProgramOptions(io output.InputOutput) []tea.ProgramOption {
opts := make([]tea.ProgramOption, 0, 2)
if io.InOrStdin() != nil && io.InOrStdin() != os.Stdin {
opts = append(opts, tea.WithInput(safeguardBubbletea964(io.InOrStdin())))
}
opts = append(opts, tea.WithInput(safeguardBubbletea964(io.InOrStdin())))
if io.OutOrStdout() != nil && io.OutOrStdout() != os.Stdout {
opts = append(opts, tea.WithOutput(io.OutOrStdout()))
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/output/tui/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/charmbracelet/lipgloss"
"go.uber.org/multierr"
"knative.dev/client-pkg/pkg/output"
"knative.dev/client-pkg/pkg/output/term"
)

const speedInterval = time.Second / 5
Expand Down Expand Up @@ -259,6 +260,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
5 changes: 5 additions & 0 deletions pkg/output/tui/spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/charmbracelet/lipgloss"
"go.uber.org/multierr"
"knative.dev/client-pkg/pkg/output"
"knative.dev/client-pkg/pkg/output/term"
)

const spinnerColor = lipgloss.Color("205")
Expand Down Expand Up @@ -96,6 +97,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 cf59659

Please sign in to comment.