Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Mar 26, 2024
1 parent b82832b commit d28712a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -16,6 +16,7 @@ require (
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.13.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.27.0
golang.org/x/term v0.18.0
gotest.tools/v3 v3.3.0
Expand Down Expand Up @@ -116,7 +117,6 @@ require (
github.com/xlab/treeprint v1.2.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/mod v0.16.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
Expand Down
49 changes: 49 additions & 0 deletions pkg/output/tui/bubbletee964.go
@@ -0,0 +1,49 @@
/*
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 (
"io"
"log"
"os"
)

// safeguardBubbletee964 will safeguard the io.Reader by returning a new
// io.Reader that will prevent the
// https://github.com/charmbracelet/bubbletea/issues/964 issue.
//
// TODO: Remove this function once the issue is resolved.
func safeguardBubbletee964(in io.Reader) io.Reader {
if in == nil {
return nil
}
if f, ok := in.(*os.File); ok {
if st, err := f.Stat(); err != nil {
log.Fatal("unexpected: ", err)
} else {
if !st.Mode().IsRegular() {
if st.Name() != os.DevNull {
log.Println("WARN: non-regular file given as input: ",
st.Name(), " (mode: ", st.Mode(),
"). Using `nil` as input.")
}
return nil
}
}
}
return in
}
4 changes: 2 additions & 2 deletions pkg/output/tui/progress.go
Expand Up @@ -89,7 +89,7 @@ func (b *BubbleProgress) With(fn func(ProgressControl) error) error {
defer b.stop()
return fn(b)
}()
return multierr.Combine(err, b.teaErr)
return multierr.Combine(err, b.err, b.teaErr)
}

func (b *BubbleProgress) Error(err error) {
Expand Down Expand Up @@ -243,7 +243,7 @@ func (b *BubbleProgress) start() {
b.prog = progress.New(progress.WithDefaultGradient())
out := b.OutOrStdout()
b.tea = tea.NewProgram(b,
tea.WithInput(b.InOrStdin()),
tea.WithInput(safeguardBubbletee964(b.InOrStdin())),
tea.WithOutput(out),
)
b.quitChan = make(chan struct{})
Expand Down
1 change: 0 additions & 1 deletion pkg/output/tui/progress_test.go
Expand Up @@ -33,7 +33,6 @@ import (
)

func TestProgress(t *testing.T) {
testing.Short()
ctx := context.TestContext(t)
prt := output.NewTestPrinter()
ctx = output.WithContext(ctx, prt)
Expand Down
2 changes: 1 addition & 1 deletion pkg/output/tui/spinner.go
Expand Up @@ -80,7 +80,7 @@ func (b *BubbleSpinner) start() {
)
out := b.OutOrStdout()
b.tea = tea.NewProgram(b,
tea.WithInput(b.InOrStdin()),
tea.WithInput(safeguardBubbletee964(b.InOrStdin())),
tea.WithOutput(out),
)
b.quitChan = make(chan struct{})
Expand Down

0 comments on commit d28712a

Please sign in to comment.