termstatus: allow retrieving the underlying writer

This is intended for special cases where it must be guaranteed that the
output on stdout exactly matches what was written to the io.Writer.
This commit is contained in:
Michael Eischer
2025-09-15 21:04:31 +02:00
parent 03600ca509
commit 94b19d64be
3 changed files with 21 additions and 0 deletions

View File

@@ -1,5 +1,9 @@
package ui
import "io"
var _ Terminal = &MockTerminal{}
type MockTerminal struct {
Output []string
Errors []string
@@ -20,3 +24,7 @@ func (m *MockTerminal) SetStatus(lines []string) {
func (m *MockTerminal) CanUpdateStatus() bool {
return true
}
func (m *MockTerminal) OutputRaw() io.Writer {
return nil
}

View File

@@ -1,5 +1,7 @@
package ui
import "io"
// Terminal is used to write messages and display status lines which can be
// updated. See termstatus.Terminal for a concrete implementation.
type Terminal interface {
@@ -7,4 +9,5 @@ type Terminal interface {
Error(line string)
SetStatus(lines []string)
CanUpdateStatus() bool
OutputRaw() io.Writer
}

View File

@@ -12,8 +12,11 @@ import (
"golang.org/x/text/width"
"github.com/restic/restic/internal/terminal"
"github.com/restic/restic/internal/ui"
)
var _ ui.Terminal = &Terminal{}
// Terminal is used to write messages and display status lines which can be
// updated. When the output is redirected to a file, the status lines are not
// printed.
@@ -83,6 +86,13 @@ func (t *Terminal) CanUpdateStatus() bool {
return t.canUpdateStatus
}
// 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.
func (t *Terminal) OutputRaw() io.Writer {
return t.wr
}
// Run updates the screen. It should be run in a separate goroutine. When
// ctx is cancelled, the status lines are cleanly removed.
func (t *Terminal) Run(ctx context.Context) {