Skip to content

Commit 2cbfbf4

Browse files
committed
Add text chunking function
1 parent eec9798 commit 2cbfbf4

3 files changed

Lines changed: 19 additions & 20 deletions

File tree

cloudbot/util/formatting.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,19 @@ def truncate(content, length=100, suffix='...'):
168168
strip_colors = strip_irc
169169

170170

171+
def chunk_str(content, length=420):
172+
"""
173+
Chunks a string into smaller strings of given length. Returns chunks.
174+
:rtype list
175+
"""
176+
def chunk(c, l):
177+
while c:
178+
out = (c+' ')[:l].rsplit(' ', 1)[0]
179+
c = c[len(out):].strip()
180+
yield out
181+
return list(chunk(content, length))
182+
183+
171184
def pluralize(num=0, text=''):
172185
"""
173186
Takes a number and a string, and pluralizes that string using the number and combines the results.

plugins/books.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def books(text):
1818
"""books <query> -- Searches Google Books for <query>."""
1919
if not dev_key:
2020
return "This command requires a Google Developers Console API key."
21+
formatting.HTMLTextExtractor
2122

2223
json = requests.get(book_search_api, params={"q": text, "key": dev_key}).json()
2324

plugins/help.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import re
44

55
from cloudbot import hook
6+
from cloudbot.util import formatting
67

78

89
@asyncio.coroutine
@@ -37,13 +38,7 @@ def help_command(text, conn, bot, notice, has_permission):
3738
else:
3839
notice("Unknown command '{}'".format(searching_for))
3940
else:
40-
41-
# list of lines to send to the user
42-
lines = []
43-
# current line, containing words to join with " "
44-
current_line = []
45-
# current line length, to count how long the current line will be when joined with " "
46-
current_line_length = 0
41+
commands = []
4742

4843
for plugin in sorted(set(bot.plugin_manager.commands.values()), key=attrgetter("name")):
4944
# use set to remove duplicate commands (from multiple aliases), and sorted to sort by name
@@ -62,22 +57,12 @@ def help_command(text, conn, bot, notice, has_permission):
6257

6358
# add the command to lines sent
6459
command = plugin.name
65-
added_length = len(command) + 2 # + 2 to account for space and comma
6660

67-
if current_line_length + added_length > 450:
68-
# if line limit is reached, add line to lines, and reset
69-
lines.append(", ".join(current_line) + ",")
70-
current_line = []
71-
current_line_length = 0
61+
commands.append(command)
7262

73-
current_line.append(command)
74-
current_line_length += added_length
75-
76-
if current_line:
77-
# make sure to include the last line
78-
lines.append(", ".join(current_line))
63+
# list of lines to send to the user
64+
lines = formatting.chunk_str("Here's a list of commands you can use: " + ", ". join(commands))
7965

80-
notice("Here's a list of commands you can use:")
8166
for line in lines:
8267
notice(line)
8368
notice("For detailed help, use {}help <command>, without the brackets.".format(conn.config["command_prefix"]))

0 commit comments

Comments
 (0)