-
Notifications
You must be signed in to change notification settings - Fork 5
/
artty.go
206 lines (167 loc) · 3.65 KB
/
artty.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
package artty
import (
"html"
"io"
"net/http"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"github.com/mjwhitta/artty/art"
"github.com/mjwhitta/errors"
hl "github.com/mjwhitta/hilighter"
"github.com/mjwhitta/where"
)
// BruceSchneier will parse the HTML response from
// https://schneierfacts.com and return the provided "fact".
func BruceSchneier() string {
var b []byte
var e error
var r *http.Response
var w int
if r, e = http.Get("https://schneierfacts.com"); e != nil {
return ""
}
defer r.Body.Close()
if b, e = io.ReadAll(r.Body); e != nil {
return ""
}
for _, m := range bsRegex.FindAllStringSubmatch(string(b), -1) {
w, _ = TermSize()
return hl.Wrap(
w,
strings.ReplaceAll(html.UnescapeString(m[1]), "\n", " "),
)
}
return ""
}
func buildRegex(
match string,
exclude string,
) (*regexp.Regexp, *regexp.Regexp, error) {
var e error
var excluded *regexp.Regexp
var matched *regexp.Regexp
if match != "" {
if matched, e = regexp.Compile(match); e != nil {
e = errors.Newf("invalid regex %s: %w", match, e)
return nil, nil, e
}
}
if exclude != "" {
if excluded, e = regexp.Compile(exclude); e != nil {
e = errors.Newf("invalid regex %s: %w", exclude, e)
return nil, nil, e
}
}
return matched, excluded, nil
}
// DevExcuse will parse the HTML response from
// http://developerexcuses.com and return the provided excuse.
func DevExcuse() string {
var b []byte
var e error
var r *http.Response
var w int
if r, e = http.Get("http://developerexcuses.com"); e != nil {
return ""
}
defer r.Body.Close()
if b, e = io.ReadAll(r.Body); e != nil {
return ""
}
for _, m := range devRegex.FindAllStringSubmatch(string(b), -1) {
w, _ = TermSize()
return hl.Wrap(
w,
strings.ReplaceAll(html.UnescapeString(m[1]), "\n", " "),
)
}
return ""
}
// Filter will apply match and exclude filters and return a list of
// art that fits the specified width and height.
func Filter(
match string,
exclude string,
w int,
h int,
) ([]string, error) {
var e error
var excluded *regexp.Regexp
var keep []string
var matched *regexp.Regexp
if matched, excluded, e = buildRegex(match, exclude); e != nil {
return []string{}, e
}
for _, name := range Cache.List() {
if (matched != nil) && !matched.MatchString(name) {
continue
}
if (excluded != nil) && excluded.MatchString(name) {
continue
}
if (w == 0) && (h == 0) {
keep = append(keep, name)
} else {
if (Cache.GetHeightOf(name) <= h) &&
(Cache.GetWidthOf(name) <= w) {
keep = append(keep, name)
}
}
}
return keep, nil
}
// Fortune will return the output from the fortune command if it is
// installed.
func Fortune() string {
var e error
var f string
var o []byte
f = where.Is("fortune")
if f == "" {
return f
}
if o, e = exec.Command(f, "-s").Output(); e != nil {
return ""
}
return strings.TrimSpace(string(o))
}
// Get will return the Art matching the provided name.
func Get(name string) (*art.Art, error) {
var a *art.Art
var e error
switch name {
case "none":
a, _ = art.New()
default:
if a, e = art.New(Cache.GetFileOf(name)); e != nil {
e = errors.Newf("failed to get Art %s: %w", name, e)
return nil, e
}
}
return a, nil
}
// TermSize will return the size of the terminal.
func TermSize() (w int, h int) {
var c *exec.Cmd
var e error
var o []byte
var size []string
if where.Is("stty") == "" {
return
}
c = exec.Command(where.Is("stty"), "size")
c.Stdin = os.Stdin
if o, e = c.Output(); e != nil {
return
}
size = strings.Split(strings.TrimSpace(string(o)), " ")
if len(size) != 2 {
return
}
h, _ = strconv.Atoi(size[0])
w, _ = strconv.Atoi(size[1])
return
}