Added swap.

This commit is contained in:
Jeremy Fincher 2004-08-27 07:06:59 +00:00
parent 9c73c57089
commit 7e22b2b78d
2 changed files with 44 additions and 1 deletions

View File

@ -139,7 +139,7 @@ class Topic(callbacks.Privmsg):
else:
return True
def _topicNumber(self, irc, n, topics=None):
def _topicNumber(self, irc, n, topics=None, normalize=False):
try:
n = int(n)
if not n:
@ -148,6 +148,10 @@ class Topic(callbacks.Privmsg):
n -= 1
if topics is not None:
topics[n]
if normalize:
assert topics, 'Can\'t normalize without topics.'
if n < 0:
n += len(topics)
return n
except (ValueError, IndexError):
irc.error('That\'s not a valid topic number.', Raise=True)
@ -380,6 +384,27 @@ class Topic(callbacks.Privmsg):
irc.error('There are no more undos for %s.' % channel)
undo = privmsgs.channel(undo)
def swap(self, irc, msg, args, channel):
"""[<channel>] <first topic number> <second topic number>
Swaps the order of the first topic number and the second topic number.
<channel> is only necessary if the message isn't sent in the channel
itself.
"""
self._canChangeTopic(irc, channel)
(first, second) = privmsgs.getArgs(args, required=2)
topics = self._splitTopic(irc.state.getTopic(channel), channel)
first = self._topicNumber(irc, first, topics, normalize=True)
second = self._topicNumber(irc, second, topics, normalize=True)
if first == second:
irc.error('I refuse to swap the same topic with itself.')
return
t = topics[first]
topics[first] = topics[second]
topics[second] = t
self._sendTopics(irc, channel, topics)
swap = privmsgs.channel(swap)
def default(self, irc, msg, args, channel):
"""[<channel>]

View File

@ -130,6 +130,24 @@ class TopicTestCase(ChannelPluginTestCase, PluginDocumentation):
self.assertResponse('topic undo', '')
finally:
conf.supybot.plugins.Topic.format.setValue(original)
def testSwap(self):
try:
original = conf.supybot.plugins.Topic.format()
conf.supybot.plugins.Topic.format.setValue('$topic')
self.assertResponse('topic set ""', '')
self.assertResponse('topic add foo', 'foo')
self.assertResponse('topic add bar', 'foo || bar')
self.assertResponse('topic add baz', 'foo || bar || baz')
self.assertResponse('topic swap 1 2', 'bar || foo || baz')
self.assertResponse('topic swap 1 -1', 'baz || foo || bar')
self.assertError('topic swap -1 -1')
self.assertError('topic swap 2 -2')
self.assertError('topic swap 1 -3')
self.assertError('topic swap -2 2')
self.assertError('topic swap -3 1')
finally:
conf.supybot.plugins.Topic.format.setValue(original)
def testDefault(self):
self.assertError('topic default')