-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
cute.go
144 lines (127 loc) · 3.02 KB
/
cute.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 cute
import (
"fmt"
"os"
"strings"
)
/* new type : color */
type CuteColor string
/* list of available colors */
const (
ResetColor CuteColor = "\033[0m"
DefaultColor CuteColor = "\033[39m"
Black CuteColor = "\033[30m"
Red CuteColor = "\033[31m"
Green CuteColor = "\033[32m"
Yellow CuteColor = "\033[33m"
Blue CuteColor = "\033[34m"
Purple CuteColor = "\033[35m"
Cyan CuteColor = "\033[36m"
White CuteColor = "\033[37m"
BrightBlack CuteColor = "\033[90m"
BrightRed CuteColor = "\033[91m"
BrightGreen CuteColor = "\033[92m"
BrightYellow CuteColor = "\033[93m"
BrightBlue CuteColor = "\033[94m"
BrightPurple CuteColor = "\033[95m"
BrightCyan CuteColor = "\033[96m"
BrightWhite CuteColor = "\033[97m"
)
/* local variables */
var (
current_title_color CuteColor = BrightYellow
current_message_color CuteColor = BrightPurple
)
/* Change the title color */
func SetTitleColor(c CuteColor) {
current_title_color = c
}
/* Change the message color */
func SetMessageColor(c CuteColor) {
current_message_color = c
}
/* Printlns multi-lines */
func Printlns(title string, messages ...any) {
// set the color
colorize(current_title_color)
// print title
fmt.Println(drawTitle(title))
// set the color
colorize(current_message_color)
// print messages
for _, m := range messages {
fmt.Println(drawMessage(m))
}
// reset
colorize(ResetColor)
}
/* Println */
func Println(title string, messages ...any) {
// set the color
colorize(current_title_color)
// print title
fmt.Println(drawTitle(title))
// set the color
colorize(current_message_color)
// print messages
if len(messages) != 0 {
lines := fmt.Sprintln(messages...)
fmt.Println(drawMessage(lines))
}
// reset
colorize(ResetColor)
}
/* Println */
func Printf(title string, message string, params ...any) {
// set the color
colorize(current_title_color)
// print title
fmt.Println(drawTitle(title))
// set the color
colorize(current_message_color)
// print messages
fmt.Printf(drawMessage(message), params...)
// reset
colorize(ResetColor)
}
/* a cute panic like */
func Check(title string, err error) {
if err != nil {
// set red color for all
fmt.Printf("%v", BrightRed)
// print title
fmt.Println(drawTitle(title))
// print error message
fmt.Println("🗲", err.Error())
colorize(ResetColor)
os.Exit(1)
}
}
/* local draw title with box */
func drawTitle(title string) (box string) {
// box elements
topright := "╮"
topleft := "╭"
w := "─"
h := "│"
bottomright := "╯"
bottomleft := "╰"
// calculate the length of line
length := calculateLength(title)
// draw line
line := strings.Repeat(w, length+2)
// print title box
box = fmt.Sprintf("%v%v%v\n", topleft, line, topright)
box += fmt.Sprintf("%v %v %v\n", h, title, h)
box += fmt.Sprintf("%v%v%v", bottomleft, line, bottomright)
return
}
/* local draw message */
func drawMessage(message any) (msg string) {
msg = fmt.Sprintf("🭬 %v", message)
return
}
/* local: start coloring */
func colorize(color CuteColor) {
fmt.Printf("%v", color)
}