From 0e16b70f8ba65950cb5ef850de73a554fb55d3a6 Mon Sep 17 00:00:00 2001 From: Daniel DiPaolo Date: Wed, 23 Jul 2003 05:29:16 +0000 Subject: [PATCH] * added mircColor() function to ircutils as well as mircColors dict * added test cases for this function * implemented nick coloring for Relay --- plugins/Relay.py | 14 ++++++++++---- src/ircutils.py | 29 +++++++++++++++++++++++++++++ test/test_ircutils.py | 17 +++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/plugins/Relay.py b/plugins/Relay.py index 1097d4414..c07616587 100644 --- a/plugins/Relay.py +++ b/plugins/Relay.py @@ -213,7 +213,7 @@ class Relay(callbacks.Privmsg): if abbreviation != self.abbreviations[realIrc]: Channel = otherIrc.state.channels[channel] usersS = ', '.join([s for s in Channel.users if s.strip()!='']) - users.append('\x02%s\x02: %s' % (abbreviation, usersS)) + users.append('%s: %s' % (ircutils.bold(abbreviation), usersS)) irc.reply(msg, '; '.join(users)) def relaywhois(self, irc, msg, args): @@ -260,11 +260,17 @@ class Relay(callbacks.Privmsg): replyIrc.reply(replyMsg, s) def _formatPrivmsg(self, nick, abbreviation, msg): + # colorize nicks + color_index = (hash(nick) % 14) + 2 + color = ircutils._colors.keys()[color_index] if ircmsgs.isAction(msg): - return '* \x02%s\x02@%s %s' % \ - (nick, abbreviation, ircmsgs.unAction(msg)) + return '* %s@%s %s' % \ + (ircutils.mircColor(nick, color), abbreviation, + ircmsgs.unAction(msg)) else: - return '<\x02%s\x02@%s> %s' % (nick, abbreviation, msg.args[1]) + return '<%s@%s> %s' % \ + (ircutils.mircColor(nick, color), abbreviation, + msg.args[1]) def doPrivmsg(self, irc, msg): callbacks.Privmsg.doPrivmsg(self, irc, msg) diff --git a/src/ircutils.py b/src/ircutils.py index 5b441430d..bc19187e0 100644 --- a/src/ircutils.py +++ b/src/ircutils.py @@ -197,6 +197,35 @@ def bold(s): """Returns the string s, bolded.""" return "\x02%s\x02" % s +mircColors = {None: '', + 'white': '0', + 'black': '1', + 'blue': '2', + 'green': '3', + 'red': '4', + 'brown': '5', + 'purple': '6', + 'orange': '7', + 'yellow': '8', + 'light green': '9', + 'teal': '10', + 'light blue': '11', + 'dark blue': '12', + 'pink': '13', + 'dark grey': '14', + 'light grey': '15'} + +def mircColor(s, fg=None, bg=None): + """Returns the string s, with the appropriate mIRC color codes applied.""" + if fg is None and bg is None: + return s + else: + if bg is None: + return "\x03%s%s\x03" % (mircColors[fg], s) + else: + return "\x03%s,%s%s\x03" % (mircColors[fg], mircColors[bg], s) + + def isValidArgument(s): """Returns if s is strictly a valid argument for an IRC message.""" return '\r' not in s and '\n' not in s and '\x00' not in s diff --git a/test/test_ircutils.py b/test/test_ircutils.py index c53c18749..2cc176b21 100644 --- a/test/test_ircutils.py +++ b/test/test_ircutils.py @@ -70,6 +70,23 @@ class FunctionsTestCase(unittest.TestCase): s = ircutils.bold('foo') self.assertEqual(s[0], '\x02') self.assertEqual(s[-1], '\x02') + + def testMircColor(self): + # No colors provided should return the same string + s = 'foo' + self.assertEqual(s, ircutils.mircColor(s)) + # Test positional args + self.assertEqual('\x030foo\x03', + ircutils.mircColor(s, 'white')) + self.assertEqual('\x031,2foo\x03', + ircutils.mircColor(s, 'black', 'blue')) + self.assertEqual('\x03,3foo\x03', + ircutils.mircColor(s, None, 'green')) + # Test keyword args + self.assertEqual('\x034foo\x03', ircutils.mircColor(s, fg='red')) + self.assertEqual('\x03,5foo\x03', ircutils.mircColor(s, bg='brown')) + self.assertEqual('\x036,7foo\x03', + ircutils.mircColor(s, bg='orange', fg='purple')) def testSafeArgument(self): s = 'I have been running for 9 seconds'