2024-01-20 18:40:22 +01:00
|
|
|
package progress
|
|
|
|
|
|
2024-04-14 11:44:13 +02:00
|
|
|
import "testing"
|
|
|
|
|
|
2024-01-20 18:40:22 +01:00
|
|
|
// A Printer can can return a new counter or print messages
|
|
|
|
|
// at different log levels.
|
|
|
|
|
// It must be safe to call its methods from concurrent goroutines.
|
|
|
|
|
type Printer interface {
|
2025-09-21 16:32:00 +02:00
|
|
|
// NewCounter returns a new progress counter. It is not shown if --quiet or --json is specified.
|
2024-01-20 18:40:22 +01:00
|
|
|
NewCounter(description string) *Counter
|
2025-09-21 16:32:00 +02:00
|
|
|
// NewCounterTerminalOnly returns a new progress counter that is only shown if stdout points to a
|
|
|
|
|
// terminal. It is not shown if --quiet or --json is specified.
|
2025-09-13 23:17:15 +02:00
|
|
|
NewCounterTerminalOnly(description string) *Counter
|
2024-01-20 18:40:22 +01:00
|
|
|
|
2025-09-21 16:32:00 +02:00
|
|
|
// E reports an error. This message is always printed to stderr.
|
|
|
|
|
// Appends a newline if not present.
|
2024-01-20 18:40:22 +01:00
|
|
|
E(msg string, args ...interface{})
|
2025-09-21 16:32:00 +02:00
|
|
|
// S prints a message, this is should only be used for very important messages
|
|
|
|
|
// that are not errors. The message is even printed if --quiet is specified.
|
|
|
|
|
// Appends a newline if not present.
|
2025-03-23 17:46:04 +01:00
|
|
|
S(msg string, args ...interface{})
|
2025-09-14 17:58:52 +02:00
|
|
|
// PT prints a message if verbosity >= 1 (neither --quiet nor --verbose is specified)
|
|
|
|
|
// and stdout points to a terminal.
|
|
|
|
|
// This is used for informational messages.
|
|
|
|
|
PT(msg string, args ...interface{})
|
2025-09-21 16:32:00 +02:00
|
|
|
// P prints a message if verbosity >= 1 (neither --quiet nor --verbose is specified),
|
|
|
|
|
// this is used for normal messages which are not errors. Appends a newline if not present.
|
2024-01-20 18:40:22 +01:00
|
|
|
P(msg string, args ...interface{})
|
2025-09-21 16:32:00 +02:00
|
|
|
// V prints a message if verbosity >= 2 (equivalent to --verbose), this is used for
|
|
|
|
|
// verbose messages. Appends a newline if not present.
|
2024-01-20 18:40:22 +01:00
|
|
|
V(msg string, args ...interface{})
|
2025-09-21 16:32:00 +02:00
|
|
|
// VV prints a message if verbosity >= 3 (equivalent to --verbose=2), this is used for
|
|
|
|
|
// debug messages. Appends a newline if not present.
|
2024-01-20 18:40:22 +01:00
|
|
|
VV(msg string, args ...interface{})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NoopPrinter discards all messages
|
|
|
|
|
type NoopPrinter struct{}
|
|
|
|
|
|
|
|
|
|
var _ Printer = (*NoopPrinter)(nil)
|
|
|
|
|
|
2024-01-20 21:58:28 +01:00
|
|
|
func (*NoopPrinter) NewCounter(_ string) *Counter {
|
2024-01-20 18:40:22 +01:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-13 23:17:15 +02:00
|
|
|
func (*NoopPrinter) NewCounterTerminalOnly(_ string) *Counter {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-20 21:58:28 +01:00
|
|
|
func (*NoopPrinter) E(_ string, _ ...interface{}) {}
|
2024-01-20 18:40:22 +01:00
|
|
|
|
2025-03-23 17:46:04 +01:00
|
|
|
func (*NoopPrinter) S(_ string, _ ...interface{}) {}
|
|
|
|
|
|
2025-09-14 17:58:52 +02:00
|
|
|
func (*NoopPrinter) PT(_ string, _ ...interface{}) {}
|
|
|
|
|
|
2024-01-20 21:58:28 +01:00
|
|
|
func (*NoopPrinter) P(_ string, _ ...interface{}) {}
|
2024-01-20 18:40:22 +01:00
|
|
|
|
2024-01-20 21:58:28 +01:00
|
|
|
func (*NoopPrinter) V(_ string, _ ...interface{}) {}
|
2024-01-20 18:40:22 +01:00
|
|
|
|
2024-01-20 21:58:28 +01:00
|
|
|
func (*NoopPrinter) VV(_ string, _ ...interface{}) {}
|
2024-04-14 11:44:13 +02:00
|
|
|
|
|
|
|
|
// TestPrinter prints messages during testing
|
|
|
|
|
type TestPrinter struct {
|
|
|
|
|
t testing.TB
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewTestPrinter(t testing.TB) *TestPrinter {
|
|
|
|
|
return &TestPrinter{
|
|
|
|
|
t: t,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var _ Printer = (*TestPrinter)(nil)
|
|
|
|
|
|
|
|
|
|
func (p *TestPrinter) NewCounter(_ string) *Counter {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-13 23:17:15 +02:00
|
|
|
func (p *TestPrinter) NewCounterTerminalOnly(_ string) *Counter {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-14 11:44:13 +02:00
|
|
|
func (p *TestPrinter) E(msg string, args ...interface{}) {
|
|
|
|
|
p.t.Logf("error: "+msg, args...)
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-23 17:46:04 +01:00
|
|
|
func (p *TestPrinter) S(msg string, args ...interface{}) {
|
|
|
|
|
p.t.Logf("stdout: "+msg, args...)
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-14 17:58:52 +02:00
|
|
|
func (p *TestPrinter) PT(msg string, args ...interface{}) {
|
|
|
|
|
p.t.Logf("stdout(terminal): "+msg, args...)
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-14 11:44:13 +02:00
|
|
|
func (p *TestPrinter) P(msg string, args ...interface{}) {
|
|
|
|
|
p.t.Logf("print: "+msg, args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *TestPrinter) V(msg string, args ...interface{}) {
|
|
|
|
|
p.t.Logf("verbose: "+msg, args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *TestPrinter) VV(msg string, args ...interface{}) {
|
|
|
|
|
p.t.Logf("verbose2: "+msg, args...)
|
|
|
|
|
}
|