-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
169 lines (150 loc) · 3.95 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
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
package main
import (
"fmt"
"io"
"os"
"strings"
"unicode"
"github.com/mattes/go-asciibot"
"github.com/spf13/pflag"
"github.com/xyproto/files"
"github.com/xyproto/rainbow"
)
const (
versionString = "botsay 1.4.1"
boxContentWidth = 42
)
// GFX is ASCII graphics as a string, and where to place it on the canvas
type GFX struct {
ascii string
x int
y int
}
// New creates a new GFX struct, with an ASCII art string and a position
func New(ascii string, x, y int) *GFX {
return &GFX{ascii, x, y}
}
// bubble will draw an ASCII bubble
func bubble(w, h int) string {
var (
sb strings.Builder
dashes = strings.Repeat("-", w-5)
)
sb.WriteString(" ." + dashes + ".\n")
for i := 0; i < (h - 2); i++ {
if i == 1 {
sb.WriteString("--<|")
} else {
sb.WriteString(" |")
}
sb.WriteString(strings.Repeat(" ", w-5) + "|\n")
}
sb.WriteString(" '" + dashes + "'\n")
return sb.String()
}
// render will combine several ASCII graphics layers (with a position each) into a single layer
func render(layers []*GFX) string {
maxWidth, maxHeight := 0, 0
for _, gfx := range layers {
gfxWidth, gfxHeight := Dimensions(gfx.ascii)
if gfx.x+gfxWidth > maxWidth {
maxWidth = gfx.x + gfxWidth
}
if gfx.y+gfxHeight > maxHeight {
maxHeight = gfx.y + gfxHeight
}
}
canvas := make([][]rune, maxHeight)
for i := range canvas {
canvas[i] = make([]rune, maxWidth)
for j := range canvas[i] {
canvas[i][j] = ' '
}
}
for _, gfx := range layers {
gfxLines := strings.Split(gfx.ascii, "\n")
for y, line := range gfxLines {
canvasY := gfx.y + y
if canvasY >= len(canvas) {
continue
}
for x, ch := range line {
canvasX := gfx.x + x
if canvasX >= len(canvas[canvasY]) {
continue
}
canvas[canvasY][canvasX] = ch
}
}
}
stringCanvas := make([]string, len(canvas))
for i, line := range canvas {
stringCanvas[i] = string(line)
}
return strings.Join(stringCanvas, "\n")
}
// botsay will generate ASCII graphics of the specified bot ID, and with a speech bubble
func botsay(msg string, botID string) string {
var layers []*GFX
trimmed := strings.TrimSpace(msg)
msgWidth := boxContentWidth
lineCount := strings.Count(trimmed, "\n") + 1
botASCII, _ := asciibot.Generate(botID)
layers = append(layers, New(botASCII, 1, 1))
sl := SplitWidthWords(trimmed, msgWidth)
boxX := 18
boxY := 1
if RuneLen(trimmed) > 0 {
layers = append(layers, New(bubble(min(msgWidth, RuneLen(trimmed))+7, len(sl)+lineCount+1), boxX, boxY))
for counter, s := range sl {
layers = append(layers, New(s, boxX+5, boxY+1+counter))
}
}
return strings.TrimRightFunc(render(layers), unicode.IsSpace) + "\n"
}
func main() {
var (
msg string
botID string
printID bool
rainbowMode bool
helpFlag bool
versionFlag bool
)
pflag.StringVarP(&botID, "id", "i", "", "Specify a custom bot ID to use for generating the ASCII art.")
pflag.BoolVarP(&printID, "print", "p", false, "Print the bot's ID after generating the ASCII art.")
pflag.BoolVarP(&rainbowMode, "color", "c", false, "Enable rainbow mode")
pflag.BoolVarP(&helpFlag, "help", "h", false, "Show this help message")
pflag.BoolVar(&versionFlag, "version", false, "Print the version and exit")
pflag.Parse()
if versionFlag {
fmt.Println(versionString)
return
}
if helpFlag {
pflag.Usage()
return
}
if botID == "" {
botID = asciibot.RandomID()
}
lenargs := len(pflag.Args())
// Read data from stdin if there is data waiting there, or if the last given argument is "-"
if files.DataReadyOnStdin() || (lenargs > 0 && pflag.Args()[lenargs-1] == "-") {
if input, err := io.ReadAll(os.Stdin); err == nil {
msg = string(input)
}
} else { // Set msg to the given arguments (if any)
msg = strings.Join(pflag.Args(), " ")
}
output := botsay(msg, botID)
if rainbowMode {
rw := rainbow.NewTruecolorWriter(3, 0.4, 10)
rw.Write([]byte(output + "\n"))
} else {
fmt.Println(output)
}
if printID {
fmt.Println("Bot ID:", botID)
}
}