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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悶 Pass hacked stdin, and don't pass stdout to bubbletea #174

Merged
merged 3 commits into from Mar 28, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion pkg/output/tui/bubbletea964.go
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
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
}
33 changes: 33 additions & 0 deletions pkg/output/tui/io.go
@@ -0,0 +1,33 @@
/*
Copyright 2024 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package tui

import (
"os"

tea "github.com/charmbracelet/bubbletea"
"knative.dev/client-pkg/pkg/output"
)

func ioProgramOptions(io output.InputOutput) []tea.ProgramOption {
opts := make([]tea.ProgramOption, 0, 2)
opts = append(opts, tea.WithInput(safeguardBubbletea964(io.InOrStdin())))
if io.OutOrStdout() != nil && io.OutOrStdout() != os.Stdout {
opts = append(opts, tea.WithOutput(io.OutOrStdout()))
}
return opts
}
5 changes: 1 addition & 4 deletions pkg/output/tui/progress.go
Expand Up @@ -241,10 +241,7 @@ func (b *BubbleProgress) tickSpeed() tea.Cmd {

func (b *BubbleProgress) start() {
b.prog = progress.New(progress.WithDefaultGradient())
b.tea = tea.NewProgram(b,
tea.WithInput(safeguardBubbletea964(b.InOrStdin())),
tea.WithOutput(b.OutOrStdout()),
)
b.tea = tea.NewProgram(b, ioProgramOptions(b.InputOutput)...)
b.quitChan = make(chan struct{})
go func() {
t := b.tea
Expand Down
5 changes: 1 addition & 4 deletions pkg/output/tui/spinner.go
Expand Up @@ -78,10 +78,7 @@ func (b *BubbleSpinner) start() {
spinner.WithSpinner(spinner.Meter),
spinner.WithStyle(spinnerStyle()),
)
b.tea = tea.NewProgram(b,
tea.WithInput(safeguardBubbletea964(b.InOrStdin())),
tea.WithOutput(b.OutOrStdout()),
)
b.tea = tea.NewProgram(b, ioProgramOptions(b.InputOutput)...)
b.quitChan = make(chan struct{})
go func() {
t := b.tea
Expand Down