diff --git a/ui.go b/ui.go index 33c1263..fb120a2 100644 --- a/ui.go +++ b/ui.go @@ -27,20 +27,34 @@ type Ui interface { // Output is called for normal standard output. Output(string) + // Outputf calls Output with a format specifier. + Outputf(string, ...interface{}) + // Info is called for information related to the previous output. // In general this may be the exact same as Output, but this gives // Ui implementors some flexibility with output formats. Info(string) + // Infof calls Info with a format specifier. + Infof(string, ...interface{}) + // Error is used for any error messages that might appear on standard // error. Error(string) + // Errorf calls Error with a format specifier. + Errorf(string, ...interface{}) + // Warn is used for any warning messages that might appear on standard // error. Warn(string) + + // Warnf calls Warn with a format specifier. + Warnf(string, ...interface{}) } +var _ Ui = &BasicUi{} + // BasicUi is an implementation of Ui that just outputs to the given // writer. This UI is not threadsafe by default, but you can wrap it // in a ConcurrentUi to make it safe. @@ -116,19 +130,37 @@ func (u *BasicUi) Error(message string) { fmt.Fprint(w, "\n") } +func (u *BasicUi) Errorf(format string, a ...interface{}) { + u.Error(fmt.Sprintf(format, a...)) +} + func (u *BasicUi) Info(message string) { u.Output(message) } +func (u *BasicUi) Infof(format string, a ...interface{}) { + u.Info(fmt.Sprintf(format, a...)) +} + func (u *BasicUi) Output(message string) { fmt.Fprint(u.Writer, message) fmt.Fprint(u.Writer, "\n") } +func (u *BasicUi) Outputf(format string, a ...interface{}) { + u.Output(fmt.Sprintf(format, a...)) +} + func (u *BasicUi) Warn(message string) { u.Error(message) } +func (u *BasicUi) Warnf(format string, a ...interface{}) { + u.Warn(fmt.Sprintf(format, a...)) +} + +var _ Ui = &PrefixedUi{} + // PrefixedUi is an implementation of Ui that prefixes messages. type PrefixedUi struct { AskPrefix string @@ -164,6 +196,10 @@ func (u *PrefixedUi) Error(message string) { u.Ui.Error(message) } +func (u *PrefixedUi) Errorf(format string, a ...interface{}) { + u.Error(fmt.Sprintf(format, a...)) +} + func (u *PrefixedUi) Info(message string) { if message != "" { message = fmt.Sprintf("%s%s", u.InfoPrefix, message) @@ -172,6 +208,10 @@ func (u *PrefixedUi) Info(message string) { u.Ui.Info(message) } +func (u *PrefixedUi) Infof(format string, a ...interface{}) { + u.Info(fmt.Sprintf(format, a...)) +} + func (u *PrefixedUi) Output(message string) { if message != "" { message = fmt.Sprintf("%s%s", u.OutputPrefix, message) @@ -180,6 +220,10 @@ func (u *PrefixedUi) Output(message string) { u.Ui.Output(message) } +func (u *PrefixedUi) Outputf(format string, a ...interface{}) { + u.Output(fmt.Sprintf(format, a...)) +} + func (u *PrefixedUi) Warn(message string) { if message != "" { message = fmt.Sprintf("%s%s", u.WarnPrefix, message) @@ -187,3 +231,7 @@ func (u *PrefixedUi) Warn(message string) { u.Ui.Warn(message) } + +func (u *PrefixedUi) Warnf(format string, a ...interface{}) { + u.Warn(fmt.Sprintf(format, a...)) +} diff --git a/ui_colored.go b/ui_colored.go index e3d5131..e2e499c 100644 --- a/ui_colored.go +++ b/ui_colored.go @@ -21,6 +21,8 @@ var ( UiColorCyan = UiColor{36, false} ) +var _ Ui = &ColoredUi{} + // ColoredUi is a Ui implementation that colors its output according // to the given color schemes for the given type of output. type ColoredUi struct { @@ -43,18 +45,34 @@ func (u *ColoredUi) Output(message string) { u.Ui.Output(u.colorize(message, u.OutputColor)) } +func (u *ColoredUi) Outputf(format string, a ...interface{}) { + u.Output(fmt.Sprintf(format, a...)) +} + func (u *ColoredUi) Info(message string) { u.Ui.Info(u.colorize(message, u.InfoColor)) } +func (u *ColoredUi) Infof(format string, a ...interface{}) { + u.Info(fmt.Sprintf(format, a...)) +} + func (u *ColoredUi) Error(message string) { u.Ui.Error(u.colorize(message, u.ErrorColor)) } +func (u *ColoredUi) Errorf(format string, a ...interface{}) { + u.Error(fmt.Sprintf(format, a...)) +} + func (u *ColoredUi) Warn(message string) { u.Ui.Warn(u.colorize(message, u.WarnColor)) } +func (u *ColoredUi) Warnf(format string, a ...interface{}) { + u.Warn(fmt.Sprintf(format, a...)) +} + func (u *ColoredUi) colorize(message string, color UiColor) string { if color.Code == -1 { return message diff --git a/ui_concurrent.go b/ui_concurrent.go index b4f4dbf..8639d29 100644 --- a/ui_concurrent.go +++ b/ui_concurrent.go @@ -1,6 +1,7 @@ package cli import ( + "fmt" "sync" ) @@ -32,6 +33,10 @@ func (u *ConcurrentUi) Error(message string) { u.Ui.Error(message) } +func (u *ConcurrentUi) Errorf(format string, a ...interface{}) { + u.Error(fmt.Sprintf(format, a...)) +} + func (u *ConcurrentUi) Info(message string) { u.l.Lock() defer u.l.Unlock() @@ -39,6 +44,10 @@ func (u *ConcurrentUi) Info(message string) { u.Ui.Info(message) } +func (u *ConcurrentUi) Infof(format string, a ...interface{}) { + u.Info(fmt.Sprintf(format, a...)) +} + func (u *ConcurrentUi) Output(message string) { u.l.Lock() defer u.l.Unlock() @@ -46,9 +55,17 @@ func (u *ConcurrentUi) Output(message string) { u.Ui.Output(message) } +func (u *ConcurrentUi) Outputf(format string, a ...interface{}) { + u.Output(fmt.Sprintf(format, a...)) +} + func (u *ConcurrentUi) Warn(message string) { u.l.Lock() defer u.l.Unlock() u.Ui.Warn(message) } + +func (u *ConcurrentUi) Warnf(format string, a ...interface{}) { + u.Warn(fmt.Sprintf(format, a...)) +} diff --git a/ui_mock.go b/ui_mock.go index c467728..670ef8a 100644 --- a/ui_mock.go +++ b/ui_mock.go @@ -40,10 +40,14 @@ func (u *MockUi) Error(message string) { fmt.Fprint(u.ErrorWriter, "\n") } +func (u *MockUi) Errorf(format string, a ...interface{}) {} + func (u *MockUi) Info(message string) { u.Output(message) } +func (u *MockUi) Infof(format string, a ...interface{}) {} + func (u *MockUi) Output(message string) { u.once.Do(u.init) @@ -51,6 +55,8 @@ func (u *MockUi) Output(message string) { fmt.Fprint(u.OutputWriter, "\n") } +func (u *MockUi) Outputf(format string, a ...interface{}) {} + func (u *MockUi) Warn(message string) { u.once.Do(u.init) @@ -58,6 +64,8 @@ func (u *MockUi) Warn(message string) { fmt.Fprint(u.ErrorWriter, "\n") } +func (u *MockUi) Warnf(format string, a ...interface{}) {} + func (u *MockUi) init() { u.ErrorWriter = new(bytes.Buffer) u.OutputWriter = new(bytes.Buffer)