Skip to content

Commit

Permalink
[terminal] 'Error', 'Warn' and 'Info' now accept custom message objects
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Nov 10, 2023
1 parent 163c401 commit 061b120
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions terminal/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package terminal
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"errors"
"fmt"
)

Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down
36 changes: 27 additions & 9 deletions terminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...))
}
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 061b120

Please sign in to comment.