2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-29 16:23:59 +00:00

ui: Simplify stdio wrapper

The StdioWrapper type is really just a pair of io.WriteClosers, so
remove it in favor of a function that returns two of those. Test
coverage increases because the removed code was not tested.
This commit is contained in:
greatroar 2024-05-21 10:19:14 +02:00
parent 7f439a9c34
commit 0b56214473
2 changed files with 5 additions and 29 deletions

View File

@ -31,8 +31,7 @@ func setupTermstatus() (*termstatus.Terminal, func()) {
// use the termstatus for stdout/stderr // use the termstatus for stdout/stderr
prevStdout, prevStderr := globalOptions.stdout, globalOptions.stderr prevStdout, prevStderr := globalOptions.stdout, globalOptions.stderr
stdioWrapper := ui.NewStdioWrapper(term) globalOptions.stdout, globalOptions.stderr = ui.WrapStdio(term)
globalOptions.stdout, globalOptions.stderr = stdioWrapper.Stdout(), stdioWrapper.Stderr()
return term, func() { return term, func() {
// shutdown termstatus // shutdown termstatus

View File

@ -7,33 +7,10 @@ import (
"github.com/restic/restic/internal/ui/termstatus" "github.com/restic/restic/internal/ui/termstatus"
) )
// StdioWrapper provides stdout and stderr integration with termstatus. // WrapStdio returns line-buffering replacements for os.Stdout and os.Stderr.
type StdioWrapper struct { // On Close, the remaining bytes are written, followed by a line break.
stdout *lineWriter func WrapStdio(term *termstatus.Terminal) (stdout, stderr io.WriteCloser) {
stderr *lineWriter return newLineWriter(term.Print), newLineWriter(term.Error)
}
// NewStdioWrapper initializes a new stdio wrapper that can be used in place of
// os.Stdout or os.Stderr.
func NewStdioWrapper(term *termstatus.Terminal) *StdioWrapper {
return &StdioWrapper{
stdout: newLineWriter(term.Print),
stderr: newLineWriter(term.Error),
}
}
// Stdout returns a writer that is line buffered and can be used in place of
// os.Stdout. On Close(), the remaining bytes are written, followed by a line
// break.
func (w *StdioWrapper) Stdout() io.WriteCloser {
return w.stdout
}
// Stderr returns a writer that is line buffered and can be used in place of
// os.Stderr. On Close(), the remaining bytes are written, followed by a line
// break.
func (w *StdioWrapper) Stderr() io.WriteCloser {
return w.stderr
} }
type lineWriter struct { type lineWriter struct {