-
Notifications
You must be signed in to change notification settings - Fork 4
/
skins.go
342 lines (294 loc) · 7.06 KB
/
skins.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package millipede
import (
"fmt"
"strings"
"unicode/utf8"
)
// Direction constants
const (
DirectionUp = iota
DirectionDown
DirectionLeft
DirectionRight
)
// Skins is the global collection of registered skins
var Skins SkinCollection
//////////////////////////////
// SkinDirection defines a skin for a direction (up, down, left, right)
type SkinDirection struct {
// Head is used by the millipede to think about its life
Head string
// Pede are what make this arthropod so special
Pede []string
// Tail is what make a millipede looking like a snake
Tail string
}
// Skin defines the different parts of a millipede body
type Skin struct {
Up *SkinDirection
Down *SkinDirection
name string
currentDirection *SkinDirection
directionChecked bool
currentI int
width int
maxWidth int
}
// SkinCollection represents a collection of skins
type SkinCollection struct {
db map[string]Skin
}
func (sc *SkinCollection) AddSkin(name string, skin Skin) {
skin.name = name
sc.db[name] = skin
}
//////////////////////////////
func init() {
Skins.db = make(map[string]Skin)
Skins.AddSkin("default", Skin{
Up: &SkinDirection{
Head: " ╚⊙ ⊙╝ ",
Pede: []string{"╚═(███)═╝"},
},
Down: &SkinDirection{
Head: " ╔⊙ ⊙╗ ",
Pede: []string{"╔═(███)═╗"},
},
})
Skins.AddSkin("frozen", Skin{
Up: &SkinDirection{
Head: " ╚⊙ ⊙╝ ",
Pede: []string{"╚═(❄❄❄)═╝"},
Tail: " ❄ ",
},
Down: &SkinDirection{
Head: " ╔⊙ ⊙╗ ",
Pede: []string{"╔═(❄❄❄)═╗"},
Tail: " ❄ ",
},
})
Skins.AddSkin("diagonals", Skin{
Up: &SkinDirection{
Head: " ╚⊙ ⊙╝ ",
Pede: []string{"\\/(███)\\/", "/\\(███)/\\"},
},
Down: &SkinDirection{
Head: " ╔⊙ ⊙╗ ",
Pede: []string{"/\\(███)/\\", "\\/(███)\\/"},
},
})
Skins.AddSkin("corporate", Skin{
Up: &SkinDirection{
Head: " ╚⊙ ⊙╝ ",
Pede: []string{"╚═(©©©)═╝"},
},
Down: &SkinDirection{
Head: " ╔⊙ ⊙╗ ",
Pede: []string{"╔═(©©©)═╗"},
},
})
Skins.AddSkin("love", Skin{
Up: &SkinDirection{
Head: " ╚⊙ ⊙╝ ",
Pede: []string{"╚═(♥♥♥)═╝"},
},
Down: &SkinDirection{
Head: " ╔⊙ ⊙╗ ",
Pede: []string{"╔═(♥♥♥)═╗"},
},
})
Skins.AddSkin("musician", Skin{
Up: &SkinDirection{
Head: " ╚⊙ ⊙╝ ",
Pede: []string{"╚═(♫♩♬)═╝"},
},
Down: &SkinDirection{
Head: " ╔⊙ ⊙╗ ",
Pede: []string{"╔═(♫♩♬)═╗"},
},
})
Skins.AddSkin("bocal", Skin{
Up: &SkinDirection{
Head: " ╚⊙ ⊙╝ ",
Pede: []string{"╚═(🐟🐟🐟)═╝"},
},
Down: &SkinDirection{
Head: " ╔⊙ ⊙╗ ",
Pede: []string{"╔═(🐟🐟🐟)═╗"},
},
})
Skins.AddSkin("ascii", Skin{
Up: &SkinDirection{
Head: " \\o o/ ",
Pede: []string{"|=(###)=|"},
},
Down: &SkinDirection{
Head: " /o o\\ ",
Pede: []string{"|=(###)=|"},
},
})
Skins.AddSkin("inception", Skin{
Up: &SkinDirection{
Head: " 👀 ",
Pede: []string{"╚═(🐛🐛🐛)═╝"},
},
Down: &SkinDirection{
Head: " 👀 ",
Pede: []string{"╔═(🐛🐛🐛)═╗"},
},
})
Skins.AddSkin("humancentipede", Skin{
Up: &SkinDirection{
Head: " 👀 ",
Pede: []string{"╚═(😷😷😷)═╝"},
},
Down: &SkinDirection{
Head: " 👀 ",
Pede: []string{"╔═(😷😷😷)═╗"},
},
})
Skins.AddSkin("finger", Skin{
Up: &SkinDirection{
Head: " 👀 ",
Pede: []string{"👈~~~ ~~~👉"},
},
Down: &SkinDirection{
Head: " 👀 ",
Pede: []string{"👈~~~~~~~~👉"},
},
})
Skins.AddSkin("handy", Skin{
Up: &SkinDirection{
Head: " 👀 ",
Pede: []string{"╚═(👌👌👌)═╝"},
},
Down: &SkinDirection{
Head: " 👀 ",
Pede: []string{"╔═(👌👌👌)═╗"},
},
})
Skins.AddSkin("human", Skin{
Up: &SkinDirection{
Head: " ( ͡° ͜ʖ ͡°)",
Pede: []string{"👆 ......👆"},
},
Down: &SkinDirection{
Head: " ( ͡° ͜ʖ ͡°)",
Pede: []string{"👇 ......👇"},
},
})
}
//////////////////////////////
// GetByName returns a skin based on its name or an error
func (sc *SkinCollection) GetByName(name string) (*Skin, error) {
if skin, ok := sc.db[name]; ok {
return &skin, nil
}
return nil, fmt.Errorf("no such skin: '%s'", name)
}
func (s *Skin) adaptWidth(line string) string {
if s.width > s.maxWidth {
slice := stringToRuneSlice(line)
left := string(slice[:s.maxWidth/2])
middle := strings.Repeat(string(slice[s.maxWidth/2:s.maxWidth/2+1]), int(s.width-s.maxWidth+1))
right := string(slice[s.maxWidth/2+1:])
return left + middle + right
}
return line
}
// GetHead returns a the head of the millipede
func (s *Skin) GetHead() string {
s.checkDirection()
s.Width()
head := s.currentDirection.Head
head = s.adaptWidth(head)
return strings.TrimRight(head, " ")
}
// GetPede returns the pede of the millipede
func (s *Skin) GetPede(i int) string {
s.checkDirection()
s.Width()
pede := s.currentDirection.Pede[i%len(s.currentDirection.Pede)]
pede = s.adaptWidth(pede)
return strings.TrimRight(pede, " ")
}
// GetTail returns a the tail of the millipede
func (s *Skin) GetTail() string {
s.checkDirection()
if s.currentDirection.Tail == "" {
return ""
}
s.Width()
tail := s.currentDirection.Tail
tail = s.adaptWidth(tail)
return strings.TrimRight(tail, " ")
}
// NextPede returns GetPede with an incremental i
func (s *Skin) NextPede() string {
return s.GetPede(s.nextI())
}
func (s *Skin) nextI() int {
defer func() {
s.currentI++
}()
return s.currentI
}
// Width returns the max width of any element
func (s *Skin) Width() int {
s.checkDirection()
if s.maxWidth == 0 {
s.maxWidth = utf8.RuneCountInString(s.currentDirection.Head)
if s.currentDirection.Tail != "" {
tailWidth := utf8.RuneCountInString(s.currentDirection.Tail)
if tailWidth > s.maxWidth {
s.maxWidth = tailWidth
}
}
for _, pedeType := range s.currentDirection.Pede {
pedeLen := utf8.RuneCountInString(pedeType)
if pedeLen > s.maxWidth {
s.maxWidth = pedeLen
}
}
}
return s.maxWidth
}
func (s *Skin) checkDirection() {
if s.directionChecked {
return
}
if s.currentDirection == nil {
s.currentDirection = s.Up
}
s.directionChecked = true
}
func (s *Skin) reset() {
s.directionChecked = false
s.width = 0
}
// SetDirection sets the current direction of the skin
func (s *Skin) SetDirection(newDirection int) error {
s.reset()
switch newDirection {
case DirectionUp:
s.currentDirection = s.Up
case DirectionDown:
s.currentDirection = s.Down
default:
s.currentDirection = nil
return fmt.Errorf("invalid direction")
}
// s.checkDirection()
// s.Width()
return nil
}
// SetWidth set wanted width
func (s *Skin) SetWidth(width int) error {
if width < s.Width() {
s.width = s.Width()
//return fmt.Errorf("this skin minimum width is '%d'", s.Width())
return nil
}
s.width = width
return nil
}