88
99from cloudbot import hook
1010from cloudbot .util import database
11+ from cloudbot .util .pager import paginated_list
1112
12- search_pages = defaultdict (list )
13- search_page_indexes = {}
13+ search_pages = defaultdict (dict )
1414
1515table = Table (
1616 'grab' ,
@@ -43,42 +43,28 @@ def load_cache(db):
4343 grab_cache .setdefault (chan , {}).setdefault (name , []).append (quote )
4444
4545
46- def two_lines (bigstring , chan ):
47- """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."""
48- global search_pages
49- temp = bigstring .split ('\n ' )
50- for i in range (0 , len (temp ), 2 ):
51- search_pages [chan ].append ('\n ' .join (temp [i :i + 2 ]))
52- search_page_indexes [chan ] = 0
53- return search_pages [chan ][0 ]
54-
55-
56- def smart_truncate (content , length = 355 , suffix = '...\n ' ):
57- if len (content ) <= length :
58- return content
59- else :
60- return content [:length ].rsplit (' \u2022 ' , 1 )[0 ]+ suffix + content [:length ].rsplit (' \u2022 ' , 1 )[1 ] + smart_truncate (content [length :])
61-
62-
6346@hook .command ("moregrab" , autohelp = False )
64- def moregrab (text , chan ):
47+ def moregrab (text , chan , conn ):
6548 """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."""
66- if not search_pages [chan ]:
67- return "There are grabsearch pages to show."
49+ pages = search_pages [conn .name ].get (chan )
50+ if not pages :
51+ return "There are no grabsearch pages to show."
52+
6853 if text :
69- index = ""
7054 try :
7155 index = int (text )
7256 except ValueError :
7357 return "Please specify an integer value."
74- if abs (int (index )) > len (search_pages [chan ]) or index == 0 :
75- return "please specify a valid page number between 1 and {}." .format (len (search_pages [chan ]))
58+
59+ page = pages [index - 1 ]
60+ if page is None :
61+ return "Please specify a valid page number between 1 and {}." .format (len (pages ))
7662 else :
77- return "{}( page {}/{})" . format ( search_pages [ chan ][ index - 1 ], index , len ( search_pages [ chan ]))
63+ return page
7864 else :
79- search_page_indexes [ chan ] += 1
80- if search_page_indexes [ chan ] < len ( search_pages [ chan ]) :
81- return "{}( page {}/{})" . format ( search_pages [ chan ][ search_page_indexes [ chan ]], search_page_indexes [ chan ] + 1 , len ( search_pages [ chan ]))
65+ page = pages . next ()
66+ if page is not None :
67+ return page
8268 else :
8369 return "All pages have been shown you can specify a page number or do a new search."
8470
@@ -160,7 +146,7 @@ def lastgrab(text, chan, message):
160146 return "<{}> has never been grabbed." .format (text )
161147 if lgrab :
162148 quote = lgrab
163- message (format_grab (text , quote ),chan )
149+ message (format_grab (text , quote ), chan )
164150
165151
166152@hook .command ("grabrandom" , "grabr" , autohelp = False )
@@ -190,12 +176,9 @@ def grabrandom(text, chan, message):
190176
191177
192178@hook .command ("grabsearch" , "grabs" , autohelp = False )
193- def grabsearch (text , chan ):
179+ def grabsearch (text , chan , conn ):
194180 """.grabsearch <text> matches "text" against nicks or grab strings in the database"""
195- out = ""
196181 result = []
197- search_pages [chan ] = []
198- search_page_indexes [chan ] = 0
199182 try :
200183 quotes = grab_cache [chan ][text .lower ()]
201184 for grab in quotes :
@@ -208,17 +191,17 @@ def grabsearch(text, chan):
208191 if text .lower () in grab .lower ():
209192 result .append ((name , grab ))
210193 if result :
211- for grab in result :
212- name = grab [ 0 ]
194+ grabs = []
195+ for name , quote in result :
213196 if text .lower () == name :
214197 name = text
215- quote = grab [ 1 ]
216- out += "{} {} " . format ( format_grab ( name , quote ), u' \u2022 ' )
217- out = smart_truncate ( out )
218- out = out [: - 2 ]
219- out = two_lines ( out , chan )
220- if len ( search_pages [ chan ]) > 1 :
221- return "{}(page {}/{}) .moregrab" . format ( out , search_page_indexes [ chan ] + 1 , len ( search_pages [ chan ]))
222- return out
198+ grabs . append ( format_grab ( name , quote ))
199+ pager = paginated_list ( grabs )
200+ search_pages [ conn . name ][ chan ] = pager
201+ page = pager . next ()
202+ if len ( page ) > 1 :
203+ page [ - 1 ] += " .moregrab"
204+
205+ return page
223206 else :
224207 return "I couldn't find any matches for {}." .format (text )
0 commit comments