This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcow.go
79 lines (68 loc) · 1.33 KB
/
cow.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
package main
import (
"fmt"
"io/ioutil"
"sort"
"strings"
)
type Cow struct {
Name string
Size string
Art []string // TerminalArt
ArtSize int
// cache
width int
}
// Width return the length of the terminal art
func (c *Cow) Width() int {
if c.width == 0 {
for _, line := range c.Art {
// "[38" is ansi escape command(set forground color).
cc := strings.Count(line, "[38") + strings.Count(line, " ")
if c.width < cc {
c.width = cc
}
}
}
return c.width
}
// Height return the count of lines
func (c *Cow) Height() int {
return len(c.Art)
}
// NewCow creates the struct of Cow
func NewCow(name string, size string) (cow *Cow, err error) {
file := name + "-" + size + ".cow"
f, err := Assets.Open(file)
if err != nil {
return nil, fmt.Errorf("Could not load the character. [name=%s, size=%s]", name, size)
}
data, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
cow = &Cow{
Name: name,
Size: size,
Art: strings.Split(string(data), "\n"),
ArtSize: len(data),
}
return cow, nil
}
func ListCows() []string {
list := make([]string, 0, 5)
for _, cow := range Assets.Files {
p := strings.Split(cow.Name(), "-")
m := false
for _, l := range list {
if l == p[0] {
m = true
}
}
if !m {
list = append(list, p[0])
}
}
sort.Strings(list)
return list
}