This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathleg.go
executable file
Β·144 lines (131 loc) Β· 3.09 KB
/
leg.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Package leg provides helpers to print the most common type of messages
// used in command line applications.
//
// For simplicity, parameters of the different functions are not validated.
// It is not fun to check for errors when printing messages. The benefits would
// not be worth it.
package leg
import (
"fmt"
"os"
"github.com/fatih/color"
)
// Head prints a simple application header/title.
//
// - `name`: Name of the project.
// - `icon`: Optional icon to display.
// - `version`: Include also the version.
func Head(name string, icon string, version string) {
if version != "" {
version = fmt.Sprintf("\t(v%s)", version)
}
fmt.Fprintf(
os.Stderr,
"\n\t%s %s\n%s\n\n",
icon,
color.New(color.Bold).SprintFunc()(name),
color.New(color.Faint).SprintFunc()(version),
)
}
// Info prints generic data.
//
// - `message`: String to print.
// - `scope`: Prefix to append.
func Info(message string, scope string) {
printLine(¶ms{
tag: "βΉ",
color: color.FgHiBlue,
message: message,
scope: scope,
})
}
// Prints a line with custom format to stderr.
func printLine(p *params) {
if p.scope != "" {
p.scope = fmt.Sprintf("[%s]", p.scope)
}
tagWithColor := color.New(p.color, color.Bold).SprintFunc()(p.tag)
fmt.Fprintf(os.Stderr, "%s %s %s\n", p.scope, tagWithColor, p.message)
}
// Parameters for `printLine` function.
type params struct {
// String to use as prefix (after scope).
tag string
// Color to use for the tag.
color color.Attribute
// String to print.
message string
// Prefix to append, optional.
scope string
}
// Success prints about a correct operation.
//
// - `message`: String to print.
// - `scope`: Prefix to append.
func Success(message string, scope string) {
printLine(¶ms{
tag: "β",
color: color.FgHiGreen,
message: message,
scope: scope,
})
}
// Warn prints about a not completely correct operation.
//
// - `message`: String to print.
// - `scope`: Prefix to append.
func Warn(message string, scope string) {
printLine(¶ms{
tag: "β ",
color: color.FgHiYellow,
message: message,
scope: scope,
})
}
// Error prints about an errored operation.
//
// - `message`: String to print.
// - `scope`: Prefix to append.
func Error(message string, scope string) {
printLine(¶ms{
tag: "β",
color: color.FgHiRed,
message: message,
scope: scope,
})
}
// Wait indicates a delay.
//
// - `message`: String to print.
// - `scope`: Prefix to append.
func Wait(message string, scope string) {
printLine(¶ms{
tag: "β¦",
color: color.FgHiMagenta,
message: message,
scope: scope,
})
}
// Done indicates something finished.
//
// - `message`: String to print.
// - `scope`: Prefix to append.
func Done(message string, scope string) {
printLine(¶ms{
tag: "β",
color: color.FgHiCyan,
message: message,
scope: scope,
})
}
// Remove deletes the actual line..
func Remove() error {
_, err := os.Stderr.WriteString("\r")
return err
}
// Result prints to the standard output.
//
// - `message`: String to print.
func Result(message string) {
fmt.Fprintln(os.Stderr, message+"\n")
}