forked from getmillipede/millipede-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
millipede.go
292 lines (267 loc) · 6.89 KB
/
millipede.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Package millipede provides a framework for creating millipedes.
// millipede is designed to be easy to understand and write, the most simple
// application can be written as follow:
// func main() {
// fmt.Println(millipede.New(20))
// }
package millipede
import (
"bytes"
"fmt"
"math"
"strings"
"unicode/utf8"
"github.com/getmillipede/millipede-go/vendor/github.com/Sirupsen/logrus"
"github.com/getmillipede/millipede-go/vendor/github.com/kortschak/zalgo"
"github.com/getmillipede/millipede-go/vendor/github.com/mgutz/ansi"
)
// Millipede defines a millipede configuration
type Millipede struct {
// Size is the amount of feet pairs
Size uint64
// Reverse is the flag that indicates the direction (up/down)
Reverse bool
// Skin is the current millipede skin (template)
Skin string
// Opposite is the flag that indicates the direction (left/right)
Opposite bool
// Width is the width of the millipede (depending on its age and the food it consumes)
Width uint64
// Curve is the size of the curve
Curve uint64
// Chameleon is the flag that indicates the millipede share its environment color
Chameleon bool
// Rainbow is the flag that indicates the millipede live with care bears
Rainbow bool
// Zalgo is the flag that invoke the hive-mind representing chaos
Zalgo bool
// Step is the amount of steps done by the millipede (useful for animations)
Steps uint64
// PadRight is the flag that indicates the need to add spacing on the right of the output
PadRight bool
}
// Skin defines the different parts of a millipede body
type Skin struct {
// Head is used by the millipede to think about its life
Head string
// Pede are what make this arthropod so special
Pede string
// Reverse is the reverse skin of the millipede
Reverse *Skin
}
// String returns a string representing a millipede
func (m *Millipede) String() string {
paddingOffsets := []string{""}
if m.Curve > 0 {
for n := uint64(1); n < m.Curve*2; n++ {
size := int(math.Min(float64(n%(m.Curve*2)), float64(m.Curve*2-n%(m.Curve*2))))
paddingOffsets = append(paddingOffsets, strings.Repeat(" ", size))
}
// --opposite support
if m.Opposite {
paddingOffsets = append(paddingOffsets[m.Curve:], paddingOffsets[:m.Curve]...)
}
}
skins := map[string]Skin{
"default": {
Head: " ╚⊙ ⊙╝ ",
Pede: "╚═(███)═╝",
Reverse: &Skin{
Head: " ╔⊙ ⊙╗ ",
Pede: "╔═(███)═╗",
},
},
"frozen": {
Head: " ╚⊙ ⊙╝ ",
Pede: "╚═(❄❄❄)═╝",
Reverse: &Skin{
Head: " ╔⊙ ⊙╗ ",
Pede: "╔═(❄❄❄)═╗",
},
},
"corporate": {
Head: " ╚⊙ ⊙╝ ",
Pede: "╚═(©©©)═╝",
Reverse: &Skin{
Head: " ╔⊙ ⊙╗ ",
Pede: "╔═(©©©)═╗",
},
},
"love": {
Head: " ╚⊙ ⊙╝ ",
Pede: "╚═(♥♥♥)═╝",
Reverse: &Skin{
Head: " ╔⊙ ⊙╗ ",
Pede: "╔═(♥♥♥)═╗",
},
},
"musician": {
Head: " ╚⊙ ⊙╝ ",
Pede: "╚═(♫♩♬)═╝",
Reverse: &Skin{
Head: " ╔⊙ ⊙╗ ",
Pede: "╔═(♫♩♬)═╗",
},
},
"bocal": {
Head: " ╚⊙ ⊙╝ ",
Pede: "╚═(🐟🐟🐟)═╝",
Reverse: &Skin{
Head: " ╔⊙ ⊙╗ ",
Pede: "╔═(🐟🐟🐟)═╗",
},
},
"ascii": {
Head: " \\o o/ ",
Pede: "|=(###)=|",
Reverse: &Skin{
Head: " /o o\\ ",
Pede: "|=(###)=|",
},
},
"inception": {
Head: " 👀 ",
Pede: "╚═(🐛🐛🐛)═╝",
Reverse: &Skin{
Head: " 👀 ",
Pede: "╔═(🐛🐛🐛)═╗",
},
},
"humancentipede": {
Head: " 👀 ",
Pede: "╚═(😷😷😷)═╝",
Reverse: &Skin{
Head: " 👀 ",
Pede: "╔═(😷😷😷)═╗",
},
},
"finger": {
Head: " 👀 ",
Pede: "👈~~~ ~~~👉",
Reverse: &Skin{
Head: " 👀 ",
Pede: "👈~~~~~~~~👉",
},
},
}
// --skin support
skin := skins[m.Skin]
if skin.Head == "" {
logrus.Fatalf("no such skin: '%s'", m.Skin)
}
// --reverse support
if m.Reverse && skin.Reverse != nil && skin.Reverse.Head != "" {
skin = *skin.Reverse
}
// --width support
if m.Width < 3 {
logrus.Fatalf("millipede cannot have a witch < 3")
}
if m.Width > 3 {
w := utf8.RuneCountInString(skin.Head)
head := StringToRuneSlice(skin.Head)
skin.Head = string(head[:w/2]) + strings.Repeat(string(head[w/2:w/2+1]), int(m.Width-2)) + string(head[w/2+1:])
pede := StringToRuneSlice(skin.Pede)
skin.Pede = string(pede[:w/2]) + strings.Repeat(string(pede[w/2:w/2+1]), int(m.Width-2)) + string(pede[w/2+1:])
}
// build the millipede body
var body []string
if m.Curve > 0 {
body = []string{paddingOffsets[m.Steps%(m.Curve*2)] + strings.TrimRight(skin.Head, " ")}
} else {
body = []string{strings.TrimRight(skin.Head, " ")}
}
var x uint64
for x = 0; x < m.Size; x++ {
var line string
if m.Curve > 0 {
line = paddingOffsets[(m.Steps+x)%(m.Curve*2)] + skin.Pede
} else {
line = "" + skin.Pede
}
body = append(body, line)
}
// --reverse support
if m.Reverse {
for i, j := 0, len(body)-1; i < j; i, j = i+1, j-1 {
body[i], body[j] = body[j], body[i]
}
}
// --chameleon and --rainbow support
for idx, line := range body {
colors := []string{"red", "green", "yellow", "blue", "magenta", "cyan", "white"}
fgColor := ""
bgColor := ""
// --chameleon support
if m.Chameleon {
fgColor = "black"
}
// --rainbow
if m.Rainbow {
bgColor = colors[idx%len(colors)]
if m.Chameleon {
fgColor = bgColor
fgColor = "black"
}
}
if fgColor != "" || bgColor != "" {
paddingSize := len(line) - len(strings.TrimSpace(line))
line = strings.Repeat(" ", paddingSize) + ansi.Color(line[paddingSize:], fgColor+":"+bgColor)
}
if m.PadRight {
line = line + strings.Repeat(" ", int(m.Curve))
}
body[idx] = line
}
output := strings.Join(body, "\n")
// --zalgo support
if m.Zalgo {
buf := new(bytes.Buffer)
z := zalgo.NewCorrupter(buf)
z.Zalgo = func(n int, r rune, z *zalgo.Corrupter) bool {
if string(r) == " " || r == 10 {
z.Up = 0
z.Middle = 0
z.Down = 0
} else {
if z.Up == 0 {
z.Up = complex(0, 0.2)
z.Middle = complex(0, 0.2)
z.Down = complex(0.001, 0.3)
}
z.Up += 0.1
z.Middle += complex(0.1, 0.2)
z.Down += 0.1
}
return false
}
fmt.Fprintln(z, output)
output = buf.String()
}
return output
}
// New returns a millipede
func New(size uint64) *Millipede {
return &Millipede{
Size: size,
Reverse: false,
Skin: "default",
Opposite: false,
Width: 3,
Curve: 4,
Chameleon: false,
Rainbow: false,
Zalgo: false,
Steps: 0,
}
}
// StringToRuneSlice converts a string to a slice of runes
func StringToRuneSlice(input string) []rune {
output := make([]rune, utf8.RuneCountInString(input))
n := 0
for _, r := range input {
output[n] = r
n++
}
return output
}