2024-01-20 18:10:11 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/ui"
|
|
|
|
"github.com/restic/restic/internal/ui/termstatus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// setupTermstatus creates a new termstatus and reroutes globalOptions.{stdout,stderr} to it
|
|
|
|
// The returned function must be called to shut down the termstatus,
|
|
|
|
//
|
|
|
|
// Expected usage:
|
|
|
|
// ```
|
2024-01-20 18:12:36 +01:00
|
|
|
// term, cancel := setupTermstatus()
|
2024-01-20 18:10:11 +01:00
|
|
|
// defer cancel()
|
|
|
|
// // do stuff
|
|
|
|
// ```
|
2024-01-20 18:12:36 +01:00
|
|
|
func setupTermstatus() (*termstatus.Terminal, func()) {
|
2024-01-20 18:10:11 +01:00
|
|
|
var wg sync.WaitGroup
|
2024-01-20 18:12:36 +01:00
|
|
|
// only shutdown once cancel is called to ensure that no output is lost
|
|
|
|
cancelCtx, cancel := context.WithCancel(context.Background())
|
2024-01-20 18:10:11 +01:00
|
|
|
|
|
|
|
term := termstatus.New(globalOptions.stdout, globalOptions.stderr, globalOptions.Quiet)
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
term.Run(cancelCtx)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// use the termstatus for stdout/stderr
|
|
|
|
prevStdout, prevStderr := globalOptions.stdout, globalOptions.stderr
|
|
|
|
stdioWrapper := ui.NewStdioWrapper(term)
|
|
|
|
globalOptions.stdout, globalOptions.stderr = stdioWrapper.Stdout(), stdioWrapper.Stderr()
|
|
|
|
|
|
|
|
return term, func() {
|
|
|
|
// shutdown termstatus
|
|
|
|
globalOptions.stdout, globalOptions.stderr = prevStdout, prevStderr
|
|
|
|
cancel()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
}
|