From e908b14c644fa562514201c0acce46b08da7dd8f Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Tue, 10 Aug 2004 15:24:01 +0000 Subject: [PATCH] Added a field command. --- plugins/Format.py | 32 ++++++++++++++++++++++++++++++++ test/test_Format.py | 9 +++++++++ 2 files changed, 41 insertions(+) diff --git a/plugins/Format.py b/plugins/Format.py index b97617e73..792f4eb46 100644 --- a/plugins/Format.py +++ b/plugins/Format.py @@ -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): + """ + + Cuts down to by chopping off the rightmost characters in + excess of . If is a negative number, it chops that many + characters off the end of . + """ + (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): + """ + + Returns the th space-separated field of . I.e., if text + is "foo bar baz" and 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): """ [ ...] diff --git a/test/test_Format.py b/test/test_Format.py index 2c6480de3..10b82f8b0 100644 --- a/test/test_Format.py +++ b/test/test_Format.py @@ -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: