deduplicate termstatus setup

This commit is contained in:
Michael Eischer
2025-09-14 17:21:30 +02:00
parent 1ae2d08d1b
commit 3410808dcf
68 changed files with 335 additions and 450 deletions

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"io"
"strings"
"sync"
"github.com/restic/restic/internal/terminal"
"github.com/restic/restic/internal/ui"
@@ -46,6 +47,34 @@ type fder interface {
Fd() uintptr
}
// Setup creates a new termstatus.
// The returned function must be called to shut down the termstatus,
//
// Expected usage:
// ```
// term, cancel := termstatus.Setup(os.stdout, os.stderr, false)
// defer cancel()
// // do stuff
// ```
func Setup(stdout, stderr io.Writer, quiet bool) (*Terminal, func()) {
var wg sync.WaitGroup
// only shutdown once cancel is called to ensure that no output is lost
cancelCtx, cancel := context.WithCancel(context.Background())
term := New(stdout, stderr, quiet)
wg.Add(1)
go func() {
defer wg.Done()
term.Run(cancelCtx)
}()
return term, func() {
// shutdown termstatus
cancel()
wg.Wait()
}
}
// New returns a new Terminal for wr. A goroutine is started to update the
// terminal. It is terminated when ctx is cancelled. When wr is redirected to
// a file (e.g. via shell output redirection) or is just an io.Writer (not the