-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
99 lines (88 loc) · 2.29 KB
/
main.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
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/kr/text"
"github.com/mattes/go-asciibot"
)
func main() {
var maxWidth int
var botSeed string
flag.IntVar(&maxWidth, "width", 40, "Approximate line width (for wrapping).")
flag.StringVar(&botSeed, "bot", "", "5-character bot seed.")
flag.Parse()
PrintBubble(FlattenIntoLines(WrapParagraphs(maxWidth), maxWidth))
PrintBot(GenerateBot(botSeed))
}
func WrapParagraphs(lineLength int) (paragraphs []string) {
all, _ := ioutil.ReadAll(os.Stdin)
all = bytes.ReplaceAll(all, []byte{'\t'}, []byte(" "))
paragraphs = strings.Split(string(all), "\n\n")
for x := range paragraphs {
paragraphs[x] = text.Wrap(paragraphs[x], lineLength)
}
return paragraphs
}
func FlattenIntoLines(paragraphs []string, maxWidth int) (lines []string, lineLength int) {
if len(paragraphs) == 1 && len(paragraphs[0]) < maxWidth {
return append(lines, paragraphs[0]), len(paragraphs[0])
}
for p, paragraph := range paragraphs {
lines = append(lines, strings.Split(paragraph, "\n")...)
if p < len(paragraphs)-1 {
lines = append(lines, strings.Repeat(" ", lineLength))
}
}
max := 9
for _, line := range lines {
if len(line) > max {
max = len(line)
}
}
// fill in all lines with trailing spaces
for l := range lines {
lines[l] += strings.Repeat(" ", max-len(lines[l]))
}
// get rid of trailing blank lines
for strings.TrimSpace(lines[len(lines)-1]) == "" {
lines = lines[:len(lines)-2]
}
return lines, max
}
func PrintBubble(lines []string, lineLength int) {
fmt.Println(" " + strings.Repeat("_", lineLength+2))
for l, line := range lines {
if len(lines) == 1 {
fmt.Println("< " + line + " >")
} else if l == 0 {
fmt.Println("/ " + line + " \\")
} else if l == len(lines)-1 {
fmt.Println("\\ " + line + " /")
} else {
fmt.Println("| " + line + " |")
}
}
fmt.Println(" " + strings.Repeat("-", lineLength+2))
}
func GenerateBot(botSeed string) string {
if bot, err := asciibot.Generate(botSeed); err == nil {
return bot
} else {
return asciibot.Random()
}
}
func PrintBot(bot string) {
for x, line := range strings.Split(bot, "\n") {
if x == 0 {
fmt.Println(` \ ` + line)
} else if x == 1 {
fmt.Println(` \` + line)
} else {
fmt.Println(" " + line)
}
}
}