From 061b1207b1e10fcf1c81d43fdab8d221b66caf6f Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Fri, 10 Nov 2023 15:16:18 +0300 Subject: [PATCH] [terminal] 'Error', 'Warn' and 'Info' now accept custom message objects --- CHANGELOG.md | 5 +++-- terminal/example_test.go | 9 +++++++++ terminal/terminal.go | 36 +++++++++++++++++++++++++++--------- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 962cec4b..58962501 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,10 @@ ### 12.84.0 -* `[errutil]` Added method `Errors.First()` -* `[errutil]` Added method `Errors.Get()` +* `[errutil]` Added method `Errors.First` +* `[errutil]` Added method `Errors.Get` * `[fmtutil/table]` Added short form of align flags +* `[terminal]` `Error`, `Warn` and `Info` now accept custom message objects * `[errutil]` Added more usage examples ### 12.83.2 diff --git a/terminal/example_test.go b/terminal/example_test.go index 15b6684b..8e9a6c7b 100644 --- a/terminal/example_test.go +++ b/terminal/example_test.go @@ -8,6 +8,7 @@ package terminal // ////////////////////////////////////////////////////////////////////////////////// // import ( + "errors" "fmt" ) @@ -112,6 +113,10 @@ func ExampleError() { // Print red text to stderr Error("Error while sending data to %s", "https://example.com") + + // Print message from error struct + err := errors.New("My error") + Error(err) } func ExampleWarn() { @@ -120,6 +125,10 @@ func ExampleWarn() { // Print yellow text to stderr Warn("Warning file %s is not found", "/home/john/test.txt") + + // Print message from error struct + err := errors.New("My warning") + Warn(err) } func ExampleInfo() { diff --git a/terminal/terminal.go b/terminal/terminal.go index 6f6002dd..2ac14fe9 100644 --- a/terminal/terminal.go +++ b/terminal/terminal.go @@ -162,29 +162,35 @@ func PrintActionStatus(status int) { } // Error prints error message -func Error(message string, args ...any) { +func Error(message any, args ...any) { + msg := formatMessage(message) + if len(args) == 0 { - fmtc.Fprintf(os.Stderr, ErrorPrefix+ErrorColorTag+"%s{!}\n", message) + fmtc.Fprintf(os.Stderr, ErrorPrefix+ErrorColorTag+"%s{!}\n", msg) } else { - fmtc.Fprintf(os.Stderr, ErrorPrefix+ErrorColorTag+"%s{!}\n", fmt.Sprintf(message, args...)) + fmtc.Fprintf(os.Stderr, ErrorPrefix+ErrorColorTag+"%s{!}\n", fmt.Sprintf(msg, args...)) } } // Warn prints warning message -func Warn(message string, args ...any) { +func Warn(message any, args ...any) { + msg := formatMessage(message) + if len(args) == 0 { - fmtc.Fprintf(os.Stderr, WarnPrefix+WarnColorTag+"%s{!}\n", message) + fmtc.Fprintf(os.Stderr, WarnPrefix+WarnColorTag+"%s{!}\n", msg) } else { - fmtc.Fprintf(os.Stderr, WarnPrefix+WarnColorTag+"%s{!}\n", fmt.Sprintf(message, args...)) + fmtc.Fprintf(os.Stderr, WarnPrefix+WarnColorTag+"%s{!}\n", fmt.Sprintf(msg, args...)) } } // Info prints info message -func Info(message string, args ...any) { +func Info(message any, args ...any) { + msg := formatMessage(message) + if len(args) == 0 { - fmtc.Fprintf(os.Stdout, InfoPrefix+InfoColorTag+"%s{!}\n", message) + fmtc.Fprintf(os.Stdout, InfoPrefix+InfoColorTag+"%s{!}\n", msg) } else { - fmtc.Fprintf(os.Stdout, InfoPrefix+InfoColorTag+"%s{!}\n", fmt.Sprintf(message, args...)) + fmtc.Fprintf(os.Stdout, InfoPrefix+InfoColorTag+"%s{!}\n", fmt.Sprintf(msg, args...)) } } @@ -228,6 +234,18 @@ func PrintWarnMessage(message string, args ...any) { // ////////////////////////////////////////////////////////////////////////////////// // +// formatMessage formats message based on it type +func formatMessage(message any) string { + switch m := message.(type) { + case string: + return m + case error: + return m.Error() + } + + return fmt.Sprint(message) +} + // getMask returns mask for password func getMask(message string) string { var masking string