1 # Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
3 This module generates ANSI character codes to printing colors to terminals.
4 See: http://en.wikipedia.org/wiki/ANSI_escape_code
12 def code_to_chars(code):
13 return CSI + str(code) + 'm'
16 return OSC + '2;' + title + BEL
18 def clear_screen(mode=2):
19 return CSI + str(mode) + 'J'
21 def clear_line(mode=2):
22 return CSI + str(mode) + 'K'
25 class AnsiCodes(object):
27 # the subclasses declare class attributes which are numbers.
28 # Upon instantiation we define instance attributes, which are the same
29 # as the class attributes but wrapped with the ANSI escape sequence
30 for name in dir(self):
31 if not name.startswith('_'):
32 value = getattr(self, name)
33 setattr(self, name, code_to_chars(value))
36 class AnsiCursor(object):
38 return CSI + str(n) + 'A'
40 return CSI + str(n) + 'B'
41 def FORWARD(self, n=1):
42 return CSI + str(n) + 'C'
44 return CSI + str(n) + 'D'
45 def POS(self, x=1, y=1):
46 return CSI + str(y) + ';' + str(x) + 'H'
49 class AnsiFore(AnsiCodes):
60 # These are fairly well supported, but not part of the standard.
71 class AnsiBack(AnsiCodes):
82 # These are fairly well supported, but not part of the standard.
93 class AnsiStyle(AnsiCodes):
102 Cursor = AnsiCursor()