-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathend.go
63 lines (53 loc) · 1.18 KB
/
end.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
package prompts
type StaticEnder struct {
Formatter
Symbol string
}
type FunctionalEnder struct {
Function func(int) (string, Formatter)
}
func MakeStaticEnder(symbol string) *StaticEnder {
return &StaticEnder{Symbol: symbol}
}
func (e *StaticEnder) WithFormatter(f Formatter) *StaticEnder {
e.Formatter = f
return e
}
func MakeFunctionalEnder(f func(int) (string, Formatter)) *FunctionalEnder {
return &FunctionalEnder{Function: f}
}
func (e *StaticEnder) End(int) Element {
if e.Formatter == nil {
return Element{
Output: e.Symbol,
Length: len(e.Symbol),
}
}
return Element{
Output: e.Format(e.Symbol),
Length: len(e.Symbol),
}
}
func (e *FunctionalEnder) End(exitCode int) Element {
s, f := e.Function(exitCode)
if f == nil {
return Element{
Output: s,
Length: len(s),
}
}
return Element{
Output: f.Format(s),
Length: len(s),
}
}
func MakeBinaryEnder(successSymbol string, successFormat Formatter, failSymbol string, failFormat Formatter) *FunctionalEnder {
return &FunctionalEnder{
Function: func(exitCode int) (string, Formatter) {
if exitCode == 0 {
return successSymbol, successFormat
}
return failSymbol, failFormat
},
}
}