-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
msg_text_builder.go
105 lines (91 loc) · 1.96 KB
/
msg_text_builder.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
package lark
import (
"fmt"
)
// textElemType of a text buf
type textElemType int
type textElem struct {
elemType textElemType
content string
}
const (
// MsgText text only message
msgText textElemType = iota
// MsgAt @somebody
msgAt
// MsgAtAll @all
msgAtAll
// msgSpace space
msgSpace
)
// MsgTextBuilder for build text buf
type MsgTextBuilder struct {
buf []textElem
}
// NewTextBuilder creates a text builder
func NewTextBuilder() *MsgTextBuilder {
return &MsgTextBuilder{
buf: make([]textElem, 0),
}
}
// Text add simple texts
func (tb *MsgTextBuilder) Text(text ...interface{}) *MsgTextBuilder {
elem := textElem{
elemType: msgText,
content: fmt.Sprint(text...),
}
tb.buf = append(tb.buf, elem)
return tb
}
// Textln add simple texts with a newline
func (tb *MsgTextBuilder) Textln(text ...interface{}) *MsgTextBuilder {
elem := textElem{
elemType: msgText,
content: fmt.Sprintln(text...),
}
tb.buf = append(tb.buf, elem)
return tb
}
// Textf add texts with format
func (tb *MsgTextBuilder) Textf(textFmt string, text ...interface{}) *MsgTextBuilder {
elem := textElem{
elemType: msgText,
content: fmt.Sprintf(textFmt, text...),
}
tb.buf = append(tb.buf, elem)
return tb
}
// Mention @somebody
func (tb *MsgTextBuilder) Mention(userID string) *MsgTextBuilder {
elem := textElem{
elemType: msgAt,
content: fmt.Sprintf("<at user_id=\"%s\">@user</at>", userID),
}
tb.buf = append(tb.buf, elem)
return tb
}
// MentionAll @all
func (tb *MsgTextBuilder) MentionAll() *MsgTextBuilder {
elem := textElem{
elemType: msgAtAll,
content: "<at user_id=\"all\">@all</at>",
}
tb.buf = append(tb.buf, elem)
return tb
}
// Clear all message
func (tb *MsgTextBuilder) Clear() {
tb.buf = make([]textElem, 0)
}
// Render message
func (tb *MsgTextBuilder) Render() string {
var text string
for _, msg := range tb.buf {
text += msg.content
}
return text
}
// Len returns buf len
func (tb MsgTextBuilder) Len() int {
return len(tb.buf)
}