This repository has been archived by the owner on Apr 14, 2021. It is now read-only.
/
symbols.go
88 lines (75 loc) · 1.63 KB
/
symbols.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
package console
import (
"strings"
)
const (
checkX = "\u2718"
checkV = "\u2714"
checkQ = "?"
)
// Checkmark type is a string with some functions attached.
type Checkmark string
func (c Checkmark) String() string {
return string(c)
}
// Inverse returns the opposite of the given Checkmark:
// a red ✘ if the argument is a green ✔ and vice versa,
// a yellow ? otherwise.
func (c Checkmark) Inverse() Checkmark {
switch c {
case redX():
return greenV()
case greenV():
return redX()
}
return c
}
// IntToCheckmark returns a string with ansi color instruction
// characters:
// a red ✘ if the argument is zero,
// a green ✔ otherwise.
func IntToCheckmark(i int) string {
return Itoc(i).String()
}
// Itoc returns a Checkmark from an int.
func Itoc(i int) Checkmark {
if i == 0 {
return redX()
}
return greenV()
}
// Btoc returns a Checkmark from a boolean, red ✘ if the argument is false, a green ✔ otherwise.
func Btoc(b bool) Checkmark {
if b {
return greenV()
}
return redX()
}
// StringToCheckmark returns a string with ansi color instruction
// characters:
// a red ✘ if the argument is "0",
// a yellow ? if the argument is "",
// a green ✔ otherwise.
func StringToCheckmark(s string) string {
return Stoc(s).String()
}
// Stoc returns a Checkmark from a string.
func Stoc(s string) Checkmark {
str := strings.TrimSpace(s)
if str == "" {
return yellowQ()
} else if str == "0" {
return redX()
} else {
return greenV()
}
}
func redX() Checkmark {
return Checkmark(Red(checkX))
}
func greenV() Checkmark {
return Checkmark(Green(checkV))
}
func yellowQ() Checkmark {
return Checkmark(Yellow(checkQ))
}