Added a field command.

This commit is contained in:
Jeremy Fincher 2004-08-10 15:24:01 +00:00
parent 975f69894b
commit e908b14c64
2 changed files with 41 additions and 0 deletions

View File

@ -159,6 +159,38 @@ class Format(callbacks.Privmsg):
(first, second) = privmsgs.getArgs(args, required=2)
irc.reply(first+second)
def cut(self, irc, msg, args):
"""<size> <text>
Cuts <text> down to <size> by chopping off the rightmost characters in
excess of <size>. If <size> is a negative number, it chops that many
characters off the end of <text>.
"""
(size, text) = privmsgs.getArgs(args, required=2)
try:
size = int(size)
except ValueError:
irc.error('%r is not a valid integer.' % size)
irc.reply(text[:size])
def field(self, irc, msg, args):
"""<number> <text>
Returns the <number>th space-separated field of <text>. I.e., if text
is "foo bar baz" and <number> is 2, "bar" is returned.
"""
(number, text) = privmsgs.getArgs(args, required=2)
try:
index = int(number)
if index > 0:
index -= 1
except ValueError:
irc.error('%r is not a valid integer.' % number)
try:
irc.reply(text.split()[index])
except IndexError:
irc.error('That\'s not a valid field.')
def format(self, irc, msg, args):
"""<format string> [<arg> ...]

View File

@ -70,4 +70,13 @@ class UtilitiesTestCase(PluginTestCase):
def testConcat(self):
self.assertResponse('concat foo bar baz', 'foobar baz')
def testCut(self):
self.assertResponse('cut 5 abcdefgh', 'abcde')
self.assertResponse('cut 5 abcd', 'abcd')
self.assertResponse('cut -1 abcde', 'abcd')
def testField(self):
self.assertResponse('field 2 foo bar baz', 'bar')
self.assertResponse('field -1 foo bar baz', 'baz')
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: