Updated to make mircColors an IrcDict, and clean up the limplementation of mircColor function.

This commit is contained in:
Jeremy Fincher 2004-07-22 22:30:24 +00:00
parent 3cb0cf82d6
commit a4386a6798
2 changed files with 42 additions and 44 deletions

View File

@ -269,50 +269,22 @@ def underline(s):
"""Returns the string s, underlined.""" """Returns the string s, underlined."""
return '\x1F%s\x1F' % s return '\x1F%s\x1F' % s
mircColors = { # Definition of mircColors dictionary moved below because it became an IrcDict.
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,
}
# Offer a reverse mapping from integers to their associated colors.
for (k, v) in mircColors.items():
if k is not None: # Ignore empty string for None.
mircColors[v] = k
def mircColor(s, fg=None, bg=None): def mircColor(s, fg=None, bg=None):
"""Returns s with the appropriate mIRC color codes applied.""" """Returns s with the appropriate mIRC color codes applied."""
if fg is None and bg is None: if fg is None and bg is None:
return s return s
if isinstance(fg, int): elif bg is None:
fg = mircColors[fg] # Convert to string, just in case. fg = mircColors[str(fg)]
if isinstance(bg, int): return '\x03%s%s\x03' % (fg.zfill(2), s)
bg = mircColors[bg] # Convert to string, just in case. elif fg is None:
if fg is None or isinstance(fg, str): bg = mircColors[str(bg)]
fg = mircColors[fg] return '\x03,%s%s\x03' % (bg.zfill(2), s)
if bg is None:
fg = str(fg).zfill(2)
return '\x03%s%s\x03' % (fg, s)
else: else:
# We don't need to zfill fg here because the comma delimits it. fg = mircColors[str(fg)]
if isinstance(bg, str): bg = mircColors[str(bg)]
bg = mircColors[bg] # No need to zfill fg because the comma delimits.
bg = str(bg).zfill(2) return '\x03%s,%s%s\x03' % (fg, bg.zfill(2), s)
return '\x03%s,%s%s\x03' % (fg, bg, s)
def canonicalColor(s, bg=False, shift=0): def canonicalColor(s, bg=False, shift=0):
"""Assigns an (fg, bg) canonical color pair to a string based on its hash """Assigns an (fg, bg) canonical color pair to a string based on its hash
@ -553,6 +525,31 @@ class IrcSet(sets.Set):
return (self.__class__, (list(self),)) return (self.__class__, (list(self),))
mircColors = IrcDict({
'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',
})
# We'll map integers to their string form so mircColor is simpler.
for (k, v) in mircColors.items():
if k is not None: # Ignore empty string for None.
sv = str(v)
mircColors[sv] = sv
if __name__ == '__main__': if __name__ == '__main__':
import sys, doctest import sys, doctest
doctest.testmod(sys.modules['__main__']) doctest.testmod(sys.modules['__main__'])

View File

@ -117,11 +117,12 @@ class FunctionsTestCase(SupyTestCase):
self.assertEqual('\x036,07foo\x03', self.assertEqual('\x036,07foo\x03',
ircutils.mircColor(s, bg='orange', fg='purple')) ircutils.mircColor(s, bg='orange', fg='purple'))
def testMircColors(self): # Commented out because we don't map numbers to colors anymore.
# Make sure all (k, v) pairs are also (v, k) pairs. ## def testMircColors(self):
for (k, v) in ircutils.mircColors.items(): ## # Make sure all (k, v) pairs are also (v, k) pairs.
if k: ## for (k, v) in ircutils.mircColors.items():
self.assertEqual(ircutils.mircColors[v], k) ## if k:
## self.assertEqual(ircutils.mircColors[v], k)
def testStripBold(self): def testStripBold(self):
self.assertEqual(ircutils.stripBold(ircutils.bold('foo')), 'foo') self.assertEqual(ircutils.stripBold(ircutils.bold('foo')), 'foo')