From 7f439a9c346fe175db1b02aeef2b19c5c63d73a4 Mon Sep 17 00:00:00 2001 From: greatroar <61184462+greatroar@users.noreply.github.com> Date: Tue, 21 May 2024 09:54:42 +0200 Subject: [PATCH 1/3] ui: Inline lineWriter into StdioWrapper --- internal/ui/stdio_wrapper.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/ui/stdio_wrapper.go b/internal/ui/stdio_wrapper.go index 42f4cc545..6566ab67c 100644 --- a/internal/ui/stdio_wrapper.go +++ b/internal/ui/stdio_wrapper.go @@ -37,14 +37,14 @@ func (w *StdioWrapper) Stderr() io.WriteCloser { } type lineWriter struct { - buf *bytes.Buffer + buf bytes.Buffer print func(string) } var _ io.WriteCloser = &lineWriter{} func newLineWriter(print func(string)) *lineWriter { - return &lineWriter{buf: bytes.NewBuffer(nil), print: print} + return &lineWriter{print: print} } func (w *lineWriter) Write(data []byte) (n int, err error) { From 0b56214473759b8f820b7d7ff7ab0e1ac64dd80f Mon Sep 17 00:00:00 2001 From: greatroar <61184462+greatroar@users.noreply.github.com> Date: Tue, 21 May 2024 10:19:14 +0200 Subject: [PATCH 2/3] 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. --- cmd/restic/termstatus.go | 3 +-- internal/ui/stdio_wrapper.go | 31 ++++--------------------------- 2 files changed, 5 insertions(+), 29 deletions(-) diff --git a/cmd/restic/termstatus.go b/cmd/restic/termstatus.go index cf3cd82ee..633e4521c 100644 --- a/cmd/restic/termstatus.go +++ b/cmd/restic/termstatus.go @@ -31,8 +31,7 @@ func setupTermstatus() (*termstatus.Terminal, func()) { // use the termstatus for stdout/stderr prevStdout, prevStderr := globalOptions.stdout, globalOptions.stderr - stdioWrapper := ui.NewStdioWrapper(term) - globalOptions.stdout, globalOptions.stderr = stdioWrapper.Stdout(), stdioWrapper.Stderr() + globalOptions.stdout, globalOptions.stderr = ui.WrapStdio(term) return term, func() { // shutdown termstatus diff --git a/internal/ui/stdio_wrapper.go b/internal/ui/stdio_wrapper.go index 6566ab67c..f78533601 100644 --- a/internal/ui/stdio_wrapper.go +++ b/internal/ui/stdio_wrapper.go @@ -7,33 +7,10 @@ import ( "github.com/restic/restic/internal/ui/termstatus" ) -// StdioWrapper provides stdout and stderr integration with termstatus. -type StdioWrapper struct { - stdout *lineWriter - stderr *lineWriter -} - -// 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 +// WrapStdio returns line-buffering replacements for os.Stdout and os.Stderr. +// On Close, the remaining bytes are written, followed by a line break. +func WrapStdio(term *termstatus.Terminal) (stdout, stderr io.WriteCloser) { + return newLineWriter(term.Print), newLineWriter(term.Error) } type lineWriter struct { From 66d03c797e44a8885b40e6790168348ba4d36cc6 Mon Sep 17 00:00:00 2001 From: greatroar <61184462+greatroar@users.noreply.github.com> Date: Tue, 21 May 2024 10:51:05 +0200 Subject: [PATCH 3/3] ui, termstatus: Move WrapStdio Saves some imports. ui still needs to import ui/termstatus from message.go. --- cmd/restic/termstatus.go | 3 +-- internal/ui/{ => termstatus}/stdio_wrapper.go | 6 ++---- internal/ui/{ => termstatus}/stdio_wrapper_test.go | 2 +- 3 files changed, 4 insertions(+), 7 deletions(-) rename internal/ui/{ => termstatus}/stdio_wrapper.go (86%) rename internal/ui/{ => termstatus}/stdio_wrapper_test.go (98%) diff --git a/cmd/restic/termstatus.go b/cmd/restic/termstatus.go index 633e4521c..c0e9a045b 100644 --- a/cmd/restic/termstatus.go +++ b/cmd/restic/termstatus.go @@ -4,7 +4,6 @@ import ( "context" "sync" - "github.com/restic/restic/internal/ui" "github.com/restic/restic/internal/ui/termstatus" ) @@ -31,7 +30,7 @@ func setupTermstatus() (*termstatus.Terminal, func()) { // use the termstatus for stdout/stderr prevStdout, prevStderr := globalOptions.stdout, globalOptions.stderr - globalOptions.stdout, globalOptions.stderr = ui.WrapStdio(term) + globalOptions.stdout, globalOptions.stderr = termstatus.WrapStdio(term) return term, func() { // shutdown termstatus diff --git a/internal/ui/stdio_wrapper.go b/internal/ui/termstatus/stdio_wrapper.go similarity index 86% rename from internal/ui/stdio_wrapper.go rename to internal/ui/termstatus/stdio_wrapper.go index f78533601..233610ba3 100644 --- a/internal/ui/stdio_wrapper.go +++ b/internal/ui/termstatus/stdio_wrapper.go @@ -1,15 +1,13 @@ -package ui +package termstatus import ( "bytes" "io" - - "github.com/restic/restic/internal/ui/termstatus" ) // WrapStdio returns line-buffering replacements for os.Stdout and os.Stderr. // On Close, the remaining bytes are written, followed by a line break. -func WrapStdio(term *termstatus.Terminal) (stdout, stderr io.WriteCloser) { +func WrapStdio(term *Terminal) (stdout, stderr io.WriteCloser) { return newLineWriter(term.Print), newLineWriter(term.Error) } diff --git a/internal/ui/stdio_wrapper_test.go b/internal/ui/termstatus/stdio_wrapper_test.go similarity index 98% rename from internal/ui/stdio_wrapper_test.go rename to internal/ui/termstatus/stdio_wrapper_test.go index b95d9180d..1e214f1f4 100644 --- a/internal/ui/stdio_wrapper_test.go +++ b/internal/ui/termstatus/stdio_wrapper_test.go @@ -1,4 +1,4 @@ -package ui +package termstatus import ( "strings"