-
Notifications
You must be signed in to change notification settings - Fork 41
/
util.go
210 lines (176 loc) · 4.36 KB
/
util.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package gcli
import (
"fmt"
"regexp"
"strings"
"github.com/gookit/color"
"github.com/gookit/gcli/v3/helper"
"github.com/gookit/goutil/goinfo"
)
/*************************************************************
* console log
*************************************************************/
var level2color = map[VerbLevel]color.Color{
VerbError: color.FgRed,
VerbWarn: color.FgYellow,
VerbInfo: color.FgGreen,
VerbDebug: color.FgCyan,
VerbCrazy: color.FgMagenta,
}
// Debugf print log message
func Debugf(format string, v ...any) {
logf(VerbDebug, format, v...)
}
// Logf print log message
func Logf(level VerbLevel, format string, v ...any) {
logf(level, format, v...)
}
// print log message
func logf(level VerbLevel, format string, v ...any) {
if gOpts.Verbose < level {
return
}
name := level2color[level].Render(level.Upper())
logAt := goinfo.GetCallerInfo(3)
color.Printf("GCli: [%s] [<gray>%s</>] %s \n", name, logAt, fmt.Sprintf(format, v...))
}
func defaultErrHandler(ctx *HookCtx) (stop bool) {
if errV := ctx.Get("err"); errV != nil {
if err, ok := errV.(error); ok {
color.Error.Tips(err.Error())
// fmt.Println(color.Red.Render("ERROR:"), err.Error())
}
}
return
}
func name2verbLevel(name string) VerbLevel {
switch strings.ToLower(name) {
case "quiet":
return VerbQuiet
case "error":
return VerbError
case "warn":
return VerbWarn
case "info":
return VerbInfo
case "debug":
return VerbDebug
case "crazy":
return VerbCrazy
}
// default level
return defaultVerb
}
/*************************************************************
* some helper methods
*************************************************************/
// Print messages
func Print(args ...any) {
color.Print(args...)
}
// Println messages
func Println(args ...any) {
color.Println(args...)
}
// Printf messages
func Printf(format string, args ...any) {
color.Printf(format, args...)
}
func panicf(format string, v ...any) {
panic(fmt.Sprintf("GCli: "+format, v...))
}
func aliasNameCheck(name string) {
if helper.IsGoodCmdName(name) {
return
}
panicf("alias name '%s' is invalid, must match: %s", name, helper.RegGoodCmdName)
}
// strictFormatArgs
// TODO mode:
//
// POSIX '-ab' will split to '-a -b', '--o' -> '-o'
// UNIX '-ab' will split to '-a b'
func strictFormatArgs(args []string) (fmtArgs []string) {
if len(args) == 0 {
return args
}
for _, arg := range args {
// if contains '=' append self
// TODO mode:
// '--test=x', '-t=x' , '-test=x', '-test'
if strings.ContainsRune(arg, '=') {
fmtArgs = append(fmtArgs, arg)
continue
}
// eg: --a ---name
if strings.HasPrefix(arg, "--") {
farg := strings.TrimLeft(arg, "-")
if rl := len(farg); rl == 1 { // fix: "--a" -> "-a"
arg = "-" + farg
} else if rl > 1 { // fix: "---name" -> "--name"
arg = "--" + farg
}
// TODO No change remain OR remove like "--" "---"
// maybe ...
} else if strings.HasPrefix(arg, "-") {
ln := len(arg)
// fix: "-abc" -> "-a -b -c"
if ln > 2 {
for _, s := range arg[1:] {
fmtArgs = append(fmtArgs, "-"+string(s))
}
continue
}
}
fmtArgs = append(fmtArgs, arg)
}
return fmtArgs
}
// flags parser is flag#FlagSet.Parse(), so:
// - if args like: "arg0 arg1 --opt", will parse fail
// - if args convert to: "--opt arg0 arg1", can correctly parse
func moveArgumentsToEnd(args []string) []string {
if len(args) < 2 {
return args
}
var argEnd int
for i, arg := range args {
// strop on the first option
if strings.IndexByte(arg, '-') == 0 {
argEnd = i
break
}
}
// the first is an option
if argEnd == -1 {
return args
}
return append(args[argEnd:], args[0:argEnd]...)
}
func splitPath2names(path string) []string {
var names []string
path = strings.TrimSpace(path)
if path != "" {
if strings.ContainsRune(path, ':') { // command ID
names = strings.Split(path, CommandSep)
} else if strings.ContainsRune(path, ' ') { // command path
names = strings.Split(path, " ")
} else {
names = []string{path}
}
}
return names
}
// regex: "`[\w ]+`"
// regex: "`.+`"
var codeReg = regexp.MustCompile("`" + `.+` + "`")
// convert "`keywords`" to "<mga>keywords</>"
func wrapColor2string(s string) string {
if strings.ContainsRune(s, '`') {
s = codeReg.ReplaceAllStringFunc(s, func(code string) string {
code = strings.Trim(code, "`")
return color.WrapTag(code, "mga")
})
}
return s
}