-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
cowsay.go
164 lines (148 loc) · 3.65 KB
/
cowsay.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
package cowsay
import (
"io/ioutil"
"math/rand"
"os"
"path"
"path/filepath"
"sort"
"strings"
"time"
)
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
// Say to return cowsay string.
func Say(phrase string, options ...Option) (string, error) {
cow, err := New(options...)
if err != nil {
return "", err
}
return cow.Say(phrase)
}
// LocationType indicates the type of COWPATH.
type LocationType int
const (
// InBinary indicates the COWPATH in binary.
InBinary LocationType = iota
// InDirectory indicates the COWPATH in your directory.
InDirectory
)
// CowPath is information of the COWPATH.
type CowPath struct {
// Name is name of the COWPATH.
// If you specified `COWPATH=/foo/bar`, Name is `/foo/bar`.
Name string
// CowFiles are name of the cowfile which are trimmed ".cow" suffix.
CowFiles []string
// LocationType is the type of COWPATH
LocationType LocationType
}
// Lookup will look for the target cowfile in the specified path.
// If it exists, it returns the cowfile information and true value.
func (c *CowPath) Lookup(target string) (*CowFile, bool) {
for _, cowfile := range c.CowFiles {
if cowfile == target {
return &CowFile{
Name: cowfile,
BasePath: c.Name,
LocationType: c.LocationType,
}, true
}
}
return nil, false
}
// CowFile is information of the cowfile.
type CowFile struct {
// Name is name of the cowfile.
Name string
// BasePath is the path which the cowpath is in.
BasePath string
// LocationType is the type of COWPATH
LocationType LocationType
}
// ReadAll reads the cowfile content.
// If LocationType is InBinary, the file read from binary.
// otherwise reads from file system.
func (c *CowFile) ReadAll() ([]byte, error) {
if c.LocationType == InBinary {
// go embed is used "/" separator
joinedPath := path.Join(c.BasePath, c.Name+".cow")
return Asset(joinedPath)
}
joinedPath := filepath.Join(c.BasePath, c.Name+".cow")
return ioutil.ReadFile(joinedPath)
}
// Cows to get list of cows
func Cows() ([]*CowPath, error) {
cowPaths, err := cowsFromCowPath()
if err != nil {
return nil, err
}
cowPaths = append(cowPaths, &CowPath{
Name: "cows",
CowFiles: CowsInBinary(),
LocationType: InBinary,
})
return cowPaths, nil
}
func cowsFromCowPath() ([]*CowPath, error) {
cowPaths := make([]*CowPath, 0)
cowPath := os.Getenv("COWPATH")
if cowPath == "" {
return cowPaths, nil
}
paths := splitPath(cowPath)
for _, path := range paths {
dirEntries, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
path := &CowPath{
Name: path,
CowFiles: []string{},
LocationType: InDirectory,
}
for _, entry := range dirEntries {
name := entry.Name()
if strings.HasSuffix(name, ".cow") {
name = strings.TrimSuffix(name, ".cow")
path.CowFiles = append(path.CowFiles, name)
}
}
sort.Strings(path.CowFiles)
cowPaths = append(cowPaths, path)
}
return cowPaths, nil
}
// GetCow to get cow's ascii art
func (cow *Cow) GetCow() (string, error) {
src, err := cow.typ.ReadAll()
if err != nil {
return "", err
}
r := strings.NewReplacer(
"\\\\", "\\",
"\\@", "@",
"\\$", "$",
"$eyes", cow.eyes,
"${eyes}", cow.eyes,
"$tongue", cow.tongue,
"${tongue}", cow.tongue,
"$thoughts", string(cow.thoughts),
"${thoughts}", string(cow.thoughts),
)
newsrc := r.Replace(string(src))
separate := strings.Split(newsrc, "\n")
mow := make([]string, 0, len(separate))
for _, line := range separate {
if strings.Contains(line, "$the_cow = <<EOC") || strings.HasPrefix(line, "##") {
continue
}
if strings.HasPrefix(line, "EOC") {
break
}
mow = append(mow, line)
}
return strings.Join(mow, "\n"), nil
}