-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord-split.py
executable file
·58 lines (43 loc) · 1.17 KB
/
discord-split.py
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
#!/usr/bin/env python3
import sys
from io import StringIO
MAX_MESSAGE_LEN = 2000
class StringBuilder:
__slots__ = (
"buffer",
"length",
)
def __init__(self):
self.buffer = StringIO()
self.length = 0
def append(self, s):
self.length += self.buffer.write(s)
def __len__(self):
return self.length
def __str__(self):
return self.buffer.getvalue()
def split_message(text):
parts = []
part = StringBuilder()
# Split by line, preserving newlines
for line in text.splitlines(True):
if len(part) + len(line) > MAX_MESSAGE_LEN:
# This part is max size, move to the next one
parts.append(str(part))
part = StringBuilder()
# Add new line to buffer
part.append(line)
# Finish up the final part
parts.append(str(part))
# Return parts
return parts
if __name__ == "__main__":
if len(sys.argv) > 1:
text = sys.argv[1]
else:
text = sys.stdin.read()
parts = split_message(text)
for i, part in enumerate(parts):
print(part)
if i < len(parts) - 1:
print("[ --------- ]\n")