diff --git a/src/ircutils.py b/src/ircutils.py index 55f7a1a99..ed3a961ae 100644 --- a/src/ircutils.py +++ b/src/ircutils.py @@ -199,38 +199,39 @@ def bold(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', + '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, } for (k, v) in mircColors.items(): - if v: # Ignore empty string for None. - i = int(v) - mircColors[v] = i - mircColors[i] = i + if k is not None: # Ignore empty string for None. + mircColors[v] = k def mircColor(s, fg=None, bg=None): """Returns s, with the appropriate mIRC color codes applied.""" if fg is None and bg is None: return s - elif bg is None: - return '\x03%s%s\x03' % (mircColors[fg], s) + fg = mircColors[fg] + if bg is None: + return '\x03%s%s\x03' % (fg, s) else: - return '\x03%s,%s%s\x03' % (mircColors[fg], mircColors[bg], s) + if isinstance(bg, str): + bg = mircColors[bg] + return '\x03%s,%s%s\x03' % (fg, bg, s) def isValidArgument(s): diff --git a/test/test_ircutils.py b/test/test_ircutils.py index 2cc176b21..c70f96a65 100644 --- a/test/test_ircutils.py +++ b/test/test_ircutils.py @@ -76,18 +76,22 @@ class FunctionsTestCase(unittest.TestCase): 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')) + 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 testMircColors(self): + # Make sure all (k, v) pairs are also (v, k) pairs. + for (k, v) in ircutils.mircColors.items(): + if k: + self.assertEqual(ircutils.mircColors[v], k) + + def testSafeArgument(self): s = 'I have been running for 9 seconds' bolds = ircutils.bold(s)