forked from Code-Hex/Neo-cowsay
/
cli.go
292 lines (266 loc) 路 6.11 KB
/
cli.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 cli
import (
"bufio"
cryptorand "crypto/rand"
"errors"
"fmt"
"io"
"math"
"math/big"
"math/rand"
"os"
"strconv"
"strings"
"time"
"github.com/Rid/hapesay/cmd/v2/internal/super"
cowsay "github.com/Rid/hapesay/v2"
"github.com/Rid/hapesay/v2/decoration"
"github.com/Code-Hex/go-wordwrap"
"github.com/jessevdk/go-flags"
"github.com/ktr0731/go-fuzzyfinder"
"github.com/mattn/go-colorable"
)
func init() {
// safely set the seed globally so we generate random ids. Tries to use a
// crypto seed before falling back to time.
var seed int64
cryptoseed, err := cryptorand.Int(cryptorand.Reader, big.NewInt(math.MaxInt64))
if err != nil {
// This should not happen, but worst-case fallback to time-based seed.
seed = time.Now().UnixNano()
} else {
seed = cryptoseed.Int64()
}
rand.Seed(seed)
}
// options struct for parse command line arguments
type options struct {
Help bool `short:"h"`
Eyes string `short:"e"`
Tongue string `short:"T"`
Width int `short:"W"`
Borg bool `short:"b"`
Dead bool `short:"d"`
Greedy bool `short:"g"`
Paranoia bool `short:"p"`
Stoned bool `short:"s"`
Tired bool `short:"t"`
Wired bool `short:"w"`
Youthful bool `short:"y"`
List bool `short:"l"`
NewLine bool `short:"n"`
File string `short:"f"`
Bold bool `long:"bold"`
Super bool `long:"super"`
Random bool `long:"random"`
Rainbow bool `long:"rainbow"`
Aurora bool `long:"aurora"`
}
// CLI prepare for running command-line.
type CLI struct {
Version string
Thinking bool
stderr io.Writer
stdout io.Writer
stdin io.Reader
}
func (c *CLI) program() string {
if c.Thinking {
return "cowthink"
}
return "cowsay"
}
// Run runs command-line.
func (c *CLI) Run(argv []string) int {
if c.stderr == nil {
c.stderr = os.Stderr
}
if c.stdout == nil {
c.stdout = colorable.NewColorableStdout()
}
if c.stdin == nil {
c.stdin = os.Stdin
}
if err := c.mow(argv); err != nil {
fmt.Fprintf(c.stderr, "%s: %s\n", c.program(), err.Error())
return 1
}
return 0
}
// mow will parsing for cowsay command line arguments and invoke cowsay.
func (c *CLI) mow(argv []string) error {
var opts options
args, err := c.parseOptions(&opts, argv)
if err != nil {
return err
}
if opts.List {
cowPaths, err := cowsay.Cows()
if err != nil {
return err
}
for _, cowPath := range cowPaths {
if cowPath.LocationType == cowsay.InBinary {
fmt.Fprintf(c.stdout, "Cow files in binary:\n")
} else {
fmt.Fprintf(c.stdout, "Cow files in %s:\n", cowPath.Name)
}
fmt.Fprintln(c.stdout, wordwrap.WrapString(strings.Join(cowPath.CowFiles, " "), 80))
fmt.Fprintln(c.stdout)
}
return nil
}
if err := c.mowmow(&opts, args); err != nil {
return err
}
return nil
}
func (c *CLI) parseOptions(opts *options, argv []string) ([]string, error) {
p := flags.NewParser(opts, flags.None)
args, err := p.ParseArgs(argv)
if err != nil {
return nil, err
}
if opts.Help {
c.stdout.Write(c.usage())
os.Exit(0)
}
return args, nil
}
func (c *CLI) usage() []byte {
year := strconv.Itoa(time.Now().Year())
return []byte(c.program() + ` version ` + c.Version + `, (c) ` + year + ` codehex
Usage: ` + c.program() + ` [-bdgpstwy] [-h] [-e eyes] [-f cowfile] [--random]
[-l] [-n] [-T tongue] [-W wrapcolumn]
[--bold] [--rainbow] [--aurora] [--super] [message]
Original Author: (c) 1999 Tony Monroe
`)
}
func (c *CLI) generateOptions(opts *options) []cowsay.Option {
o := make([]cowsay.Option, 0, 8)
if opts.File == "-" {
cows := cowList()
idx, _ := fuzzyfinder.Find(cows, func(i int) string {
return cows[i]
})
opts.File = cows[idx]
}
o = append(o, cowsay.Type(opts.File))
if c.Thinking {
o = append(o,
cowsay.Thinking(),
cowsay.Thoughts('o'),
)
}
if opts.Random {
o = append(o, cowsay.Random())
}
if opts.Eyes != "" {
o = append(o, cowsay.Eyes(opts.Eyes))
}
if opts.Tongue != "" {
o = append(o, cowsay.Tongue(opts.Tongue))
}
if opts.Width > 0 {
o = append(o, cowsay.BallonWidth(uint(opts.Width)))
}
if opts.NewLine {
o = append(o, cowsay.DisableWordWrap())
}
return selectFace(opts, o)
}
func cowList() []string {
cows, err := cowsay.Cows()
if err != nil {
return cowsay.CowsInBinary()
}
list := make([]string, 0)
for _, cow := range cows {
list = append(list, cow.CowFiles...)
}
return list
}
func (c *CLI) phrase(opts *options, args []string) string {
if len(args) > 0 {
return strings.Join(args, " ")
}
lines := make([]string, 0, 40)
scanner := bufio.NewScanner(c.stdin)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return strings.Join(lines, "\n")
}
func (c *CLI) mowmow(opts *options, args []string) error {
phrase := c.phrase(opts, args)
o := c.generateOptions(opts)
if opts.Super {
return super.RunSuperCow(phrase, opts.Bold, o...)
}
say, err := cowsay.Say(phrase, o...)
if err != nil {
var notfound *cowsay.NotFound
if errors.As(err, ¬found) {
return fmt.Errorf("could not find %s cowfile", notfound.Cowfile)
}
return err
}
options := make([]decoration.Option, 0)
if opts.Bold {
options = append(options, decoration.WithBold())
}
if opts.Rainbow {
options = append(options, decoration.WithRainbow())
}
if opts.Aurora {
options = append(options, decoration.WithAurora(rand.Intn(256)))
}
w := decoration.NewWriter(c.stdout, options...)
fmt.Fprintln(w, say)
return nil
}
func selectFace(opts *options, o []cowsay.Option) []cowsay.Option {
switch {
case opts.Borg:
o = append(o,
cowsay.Eyes("=="),
cowsay.Tongue(" "),
)
case opts.Dead:
o = append(o,
cowsay.Eyes("xx"),
cowsay.Tongue("U "),
)
case opts.Greedy:
o = append(o,
cowsay.Eyes("$$"),
cowsay.Tongue(" "),
)
case opts.Paranoia:
o = append(o,
cowsay.Eyes("@@"),
cowsay.Tongue(" "),
)
case opts.Stoned:
o = append(o,
cowsay.Eyes("**"),
cowsay.Tongue("U "),
)
case opts.Tired:
o = append(o,
cowsay.Eyes("--"),
cowsay.Tongue(" "),
)
case opts.Wired:
o = append(o,
cowsay.Eyes("OO"),
cowsay.Tongue(" "),
)
case opts.Youthful:
o = append(o,
cowsay.Eyes(".."),
cowsay.Tongue(" "),
)
}
return o
}