Skip to content

Commit 84cd4ad

Browse files
committed
Switch grab.py to use the new generic pager
1 parent feec4fa commit 84cd4ad

1 file changed

Lines changed: 28 additions & 44 deletions

File tree

plugins/grab.py

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import random
2-
32
from collections import defaultdict
3+
44
from sqlalchemy import Table, Column, String
5+
56
from cloudbot import hook
67
from cloudbot.util import database
8+
from cloudbot.util.pager import paginated_list
79

8-
search_pages = defaultdict(list)
9-
search_page_indexes = {}
10+
search_pages = defaultdict(dict)
1011

1112
table = Table(
1213
'grab',
@@ -33,42 +34,28 @@ def load_cache(db):
3334
grab_cache.setdefault(chan, {}).setdefault(name, []).append(quote)
3435

3536

36-
def two_lines(bigstring, chan):
37-
"""Receives a string with new lines. Groups the string into a list of strings with up to 3 new lines per string element. Returns first string element then stores the remaining list in search_pages."""
38-
global search_pages
39-
temp = bigstring.split('\n')
40-
for i in range(0, len(temp), 2):
41-
search_pages[chan].append('\n'.join(temp[i:i+2]))
42-
search_page_indexes[chan] = 0
43-
return search_pages[chan][0]
44-
45-
46-
def smart_truncate(content, length=355, suffix='...\n'):
47-
if len(content) <= length:
48-
return content
49-
else:
50-
return content[:length].rsplit(' \u2022 ', 1)[0]+ suffix + content[:length].rsplit(' \u2022 ', 1)[1] + smart_truncate(content[length:])
51-
52-
5337
@hook.command("moregrab", autohelp=False)
54-
def moregrab(text, chan):
38+
def moregrab(text, chan, conn):
5539
"""if a grab search has lots of results the results are pagintated. If the most recent search is paginated the pages are stored for retreival. If no argument is given the next page will be returned else a page number can be specified."""
56-
if not search_pages[chan]:
57-
return "There are grabsearch pages to show."
40+
pages = search_pages[conn.name][chan]
41+
if not pages:
42+
return "There are no grabsearch pages to show."
43+
5844
if text:
59-
index = ""
6045
try:
6146
index = int(text)
6247
except ValueError:
6348
return "Please specify an integer value."
64-
if abs(int(index)) > len(search_pages[chan]) or index == 0:
65-
return "please specify a valid page number between 1 and {}.".format(len(search_pages[chan]))
49+
50+
page = pages[index - 1]
51+
if page is None:
52+
return "Please specify a valid page number between 1 and {}.".format(len(pages))
6653
else:
67-
return "{}(page {}/{})".format(search_pages[chan][index-1], index, len(search_pages[chan]))
54+
return page
6855
else:
69-
search_page_indexes[chan] += 1
70-
if search_page_indexes[chan] < len(search_pages[chan]):
71-
return "{}(page {}/{})".format(search_pages[chan][search_page_indexes[chan]], search_page_indexes[chan] + 1, len(search_pages[chan]))
56+
page = pages.next()
57+
if page is not None:
58+
return page
7259
else:
7360
return "All pages have been shown you can specify a page number or do a new search."
7461

@@ -165,12 +152,9 @@ def grabrandom(text, chan, message):
165152

166153

167154
@hook.command("grabsearch", "grabs", autohelp=False)
168-
def grabsearch(text, chan):
155+
def grabsearch(text, chan, conn):
169156
""".grabsearch <text> matches "text" against nicks or grab strings in the database"""
170-
out = ""
171157
result = []
172-
search_pages[chan] = []
173-
search_page_indexes[chan] = 0
174158
try:
175159
quotes = grab_cache[chan][text.lower()]
176160
for grab in quotes:
@@ -183,17 +167,17 @@ def grabsearch(text, chan):
183167
if text.lower() in grab.lower():
184168
result.append((name, grab))
185169
if result:
186-
for grab in result:
187-
name = grab[0]
170+
grabs = []
171+
for name, quote in result:
188172
if text.lower() == name:
189173
name = text
190-
quote = grab[1]
191-
out += "{} {} ".format(format_grab(name, quote), u'\u2022')
192-
out = smart_truncate(out)
193-
out = out[:-2]
194-
out = two_lines(out, chan)
195-
if len(search_pages[chan]) > 1:
196-
return "{}(page {}/{}) .moregrab".format(out, search_page_indexes[chan] + 1, len(search_pages[chan]))
197-
return out
174+
grabs.append(format_grab(name, quote))
175+
pager = paginated_list(grabs)
176+
search_pages[conn.name][chan] = pager
177+
page = pager.next()
178+
if len(page) > 1:
179+
page[-1] += " .moregrab"
180+
181+
return page
198182
else:
199183
return "I couldn't find any matches for {}.".format(text)

0 commit comments

Comments
 (0)