replace globalOptions.stdout with termstatus.OutputWriter

This commit is contained in:
Michael Eischer
2025-09-20 23:06:28 +02:00
parent c293736841
commit 76b2cdd4fb
29 changed files with 79 additions and 85 deletions

View File

@@ -40,6 +40,10 @@ func (m *MockTerminal) ReadPassword(_ context.Context, _ string) (string, error)
return "password", nil
}
func (m *MockTerminal) OutputWriter() io.Writer {
return nil
}
func (m *MockTerminal) OutputRaw() io.Writer {
return nil
}

View File

@@ -16,9 +16,12 @@ type Terminal interface {
SetStatus(lines []string)
// CanUpdateStatus returns true if the terminal can update the status lines.
CanUpdateStatus() bool
InputRaw() io.ReadCloser
InputIsTerminal() bool
ReadPassword(ctx context.Context, prompt string) (string, error)
OutputWriter() io.Writer
// OutputRaw returns the output writer. Should only be used if there is no
// other option. Must not be used in combination with Print, Error, SetStatus
// or any other method that writes to the terminal.

View File

@@ -30,6 +30,9 @@ type Terminal struct {
outputIsTerminal bool
canUpdateStatus bool
outputWriter io.WriteCloser
outputWriterOnce sync.Once
// will be closed when the goroutine which runs Run() terminates, so it'll
// yield a default value immediately
closed chan struct{}
@@ -73,6 +76,9 @@ func Setup(stdin io.ReadCloser, stdout, stderr io.Writer, quiet bool) (*Terminal
}()
return term, func() {
if term.outputWriter != nil {
_ = term.outputWriter.Close()
}
// shutdown termstatus
cancel()
wg.Wait()
@@ -158,6 +164,13 @@ func (t *Terminal) CanUpdateStatus() bool {
return t.canUpdateStatus
}
func (t *Terminal) OutputWriter() io.Writer {
t.outputWriterOnce.Do(func() {
t.outputWriter = newLineWriter(t.Print)
})
return t.outputWriter
}
// OutputRaw returns the output writer. Should only be used if there is no
// other option. Must not be used in combination with Print, Error, SetStatus
// or any other method that writes to the terminal.

View File

@@ -6,12 +6,6 @@ import (
"sync"
)
// WrapStdout returns line-buffering replacements for os.Stdout.
// On Close, the remaining bytes are written, followed by a line break.
func WrapStdout(term *Terminal) (stdout io.WriteCloser) {
return newLineWriter(term.Print)
}
type lineWriter struct {
m sync.Mutex
buf bytes.Buffer