-
Notifications
You must be signed in to change notification settings - Fork 10
/
clarke.go
117 lines (111 loc) · 3.75 KB
/
clarke.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
// apcore is a server framework for implementing an ActivityPub application.
// Copyright (C) 2019 Cory Slep
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package framework
import (
"strings"
)
const (
// Clarke says something shorter than usual.
// Line 1 usable range is [86:138] (len=52)
// Line 2 usable range is [156:209] (len=53)
// Line 3 usable range is [227:280] (len=53)
clarkeShort = ` ______________________________________________________
\__/ / \
\------(oo) / |
|| (__) < |
||w--|| \-------------------------------------------------------/`
// Clarke says something longer.
// Lines 1 - 3 are the same as above
// Line 4 usable range is [298:351] (len=53)
clarkeLongBegin = ` ______________________________________________________
\__/ / \
\------(oo) / |
|| (__) < |
||w--|| | |`
// Line usable range is [15:68] (len=53)
clarkeLongMiddle = ` | |`
clarkeLongEnd = ` \-------------------------------------------------------/`
)
func replace(input, replace string, offset int) string {
in := []byte(input)
repl := []byte(replace)
return string(append(in[:offset],
append(repl,
in[offset+len(repl):]...)...))
}
func ClarkeSays(moo string) string {
moo = strings.TrimSpace(strings.ReplaceAll(moo, "\n", " "))
words := strings.Split(moo, " ")
lines := make([][]string, 0, 1)
var line []string
var length int
for _, word := range words {
maxlen := 53
if len(lines) == 0 {
maxlen = 52
}
sl := 0
if len(line) > 0 {
sl = 1
}
if length+len(word)+sl > maxlen {
lines = append(lines, line)
line = []string{word}
length = len(word)
} else {
line = append(line, word)
length += len(word) + sl
}
}
lines = append(lines, line)
var s string
switch len(lines) {
case 1:
// Middle line
s = clarkeShort
s = replace(s, strings.Join(lines[0], " "), 156)
case 2:
// Middle and bottom line
s = clarkeShort
s = replace(s, strings.Join(lines[0], " "), 156)
s = replace(s, strings.Join(lines[1], " "), 226)
case 3:
// Top, middle and bottom line
s = clarkeShort
s = replace(s, strings.Join(lines[0], " "), 86)
s = replace(s, strings.Join(lines[1], " "), 156)
s = replace(s, strings.Join(lines[2], " "), 227)
default:
// Long paragraph.
s = clarkeLongBegin
s = replace(s, strings.Join(lines[0], " "), 86)
s = replace(s, strings.Join(lines[1], " "), 156)
s = replace(s, strings.Join(lines[2], " "), 227)
s = replace(s, strings.Join(lines[3], " "), 298)
if len(lines) > 4 {
for i := 4; i < len(lines); i++ {
m := clarkeLongMiddle
m = replace(m, strings.Join(lines[i], " "), 15)
s += "\n"
s += m
}
}
s += "\n"
s += clarkeLongEnd
}
s += "\n"
return s
}