]>
jfr.im git - z_archive/twitter.git/blob - twitter/ansi.py
2 Support for ANSI colours in command-line client.
8 ansi reset colour (ansi value)
10 .. data:: COLOURS_NAMED
11 dict of colour names mapped to their ansi value
13 .. data:: COLOURS_MIDS
14 A list of ansi values for Mid Spectrum Colours
22 COLOURS_NAMED
= dict(zip(
23 ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'],
24 [str(x
) for x
in range(30, 38)]
27 colour
for name
, colour
in COLOURS_NAMED
.items()
28 if name
not in ('black', 'white')
31 class AnsiColourException(Exception):
32 ''' Exception while processing ansi colours '''
35 class ColourMap(object):
37 Object that allows for mapping strings to ansi colour values.
39 def __init__(self
, colors
=COLOURS_MIDS
):
40 ''' uses the list of ansi `colors` values to initialize the map '''
42 self
._colourIter
= itertools
.cycle(colors
)
44 def colourFor(self
, string
):
46 Returns an ansi colour value given a `string`.
47 The same ansi colour value is always returned for the same string
49 if not self
._cmap
.has_key(string
):
50 self
._cmap
[string
] = self
._colourIter
.next()
51 return self
._cmap
[string
]
53 def cmdColour(colour
):
55 Return the ansi cmd colour (i.e. escape sequence)
56 for the ansi `colour` value
58 return ESC
+ "[" + colour
+ "m"
61 ''' Returns the ansi cmd colour for a RESET '''
62 return cmdColour(RESET
)
64 def cmdColourNamed(colour
):
65 ''' Return the ansi cmdColour for a given named `colour` '''
67 return cmdColour(COLOURS_NAMED
[colour
])
69 raise AnsiColourException('Unknown Colour %s' %(colour))