plugins/tag: if there are too many tags to display, remove some of them

This commit is contained in:
Ryan Hitchman 2013-09-20 13:01:54 -07:00
parent 310863a2b9
commit e0518f6c2a
1 changed files with 13 additions and 3 deletions

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import random
import re import re
from util import hook from util import hook
@ -17,6 +18,15 @@ def munge(inp, munge_count=0):
break break
return inp return inp
def winnow(inputs, limit=400):
"remove random elements from the list until it's short enough"
combiner = lambda l: ', '.join(l)
suffix = ''
while len(combiner(inputs)) >= limit:
inputs.pop(random.randint(0, len(inputs) - 1))
suffix = ' ...'
return combiner(inputs) + suffix
def add_tag(db, chan, nick, subject): def add_tag(db, chan, nick, subject):
match = db.execute('select * from tag where lower(nick)=lower(?) and' match = db.execute('select * from tag where lower(nick)=lower(?) and'
@ -73,7 +83,7 @@ def get_nicks_by_tag(db, chan, subject):
nicks = [munge(x[0], 1) for x in nicks] nicks = [munge(x[0], 1) for x in nicks]
if not nicks: if not nicks:
return 'tag not found' return 'tag not found'
return 'nicks tagged "%s": ' % subject + ', '.join(nicks) return 'nicks tagged "%s": ' % subject + winnow(nicks)
@hook.command @hook.command
@ -105,8 +115,8 @@ def tag(inp, chan='', db=None):
if not tags: if not tags:
return get_nicks_by_tag(db, chan, inp) return get_nicks_by_tag(db, chan, inp)
else: else:
return 'tags for "%s": ' % munge(inp, 1) + ', '.join( return 'tags for "%s": ' % munge(inp, 1) + winnow([
tag[0] for tag in tags) tag[0] for tag in tags])
character_replacements = { character_replacements = {