-
Notifications
You must be signed in to change notification settings - Fork 0
/
pokesay.go
93 lines (80 loc) · 1.65 KB
/
pokesay.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
package pokesay
import (
"bytes"
"github.com/TaigaMikami/Pokesay/balloon"
"github.com/TaigaMikami/Pokesay/img2xterm"
"image"
"io/ioutil"
"math/rand"
"net/http"
"time"
)
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
func Say(options ...Option) (string, error) {
poke, err := NewPoke(options...)
if err != nil {
return "", err
}
//pokemon, err := poke.GetPoke()
pokemon, err := poke.GetPokeWeb()
if err != nil {
return "", err
}
balloon, err := poke.GetBalloon()
if err != nil {
return "", err
}
return balloon + pokemon, nil
}
//func (poke *Poke) GetPoke() (string, error) {
// file, _ := os.Open(poke.typ)
// img, _, err := image.Decode(file)
// if err != nil {
// return "", err
// }
// pokemon := img2xterm.Img2xterm(img)
// return pokemon, nil
//}
func (poke *Poke)GetPokeWeb() (string, error) {
resp, err := http.Get(poke.typ)
if err != nil {
return "", err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
img, _, err := image.Decode(bytes.NewReader(b))
if err != nil {
return "", err
}
pokemon := img2xterm.Img2xterm(img)
return pokemon, nil
}
func (poke *Poke) GetBalloon() (string, error) {
var thoughts string
if poke.thinking {
thoughts = ` o
o
`
} else {
thoughts = ` \
\
`
}
var s []string
s = append(s, poke.phrase)
inputs := balloon.ReadInput(s)
width := balloon.MaxWidth(inputs)
messages := balloon.SetPadding(inputs, width)
balloon := balloon.ConstructBalloon(messages, width)
return balloon + thoughts, nil
}
func pickPoke() string {
pokes := PokemonNames()
n := len(pokes)
return pokes[rand.Intn(n)]
}