/
out.go
54 lines (43 loc) · 1.72 KB
/
out.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
Package out provides a simple interface to terminal outputting. Suitable for terminal
applications.
View sample screenshot at https://github.com/GochoMugo/go-out/blob/master/screenshot.png
All the exported functions, unless noted otherwise, format the message string
using Printf. Also, color codes, as expected by colorstring (https://github.com/mitchellh/colorstring),
found within the message are replaced with the respective colors. For example `"hello, [red] mars"`
will have the "[red]" replaced with color red.
*/
package out
import (
"io"
"os"
"github.com/mitchellh/colorstring"
)
const marker = "[magenta] >>> "
// wrap wraps a message with the designated color codes
func printout(out io.Writer, message string, colorcode string, args ...interface{}) (n int, err error) {
return colorstring.Fprintf(out, marker+"["+colorcode+"]"+message+"\n", args...)
}
// Success outputs a success message
func Success(message string, args ...interface{}) (n int, err error) {
return printout(os.Stdout, message, "green", args...)
}
// Error outputs an error message
func Error(message string, args ...interface{}) (n int, err error) {
return printout(os.Stderr, message, "red", args...)
}
// Warn outputs a warning message
func Warn(message string, args ...interface{}) (n int, err error) {
return printout(os.Stderr, message, "yellow", args...)
}
// Info outputs a informatory message
func Info(message string, args ...interface{}) (n int, err error) {
return printout(os.Stdout, message, "blue", args...)
}
// Debug outputs a debug message if ${DEBUG} is truthy
func Debug(message string, args ...interface{}) (n int, err error) {
if os.Getenv("DEBUG") != "" {
return printout(os.Stderr, message, "cyan", args...)
}
return 0, nil
}