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"
17 return OSC + "2;" + title + BEL
20 def clear_screen(mode=2):
21 return CSI + str(mode) + "J"
24 def clear_line(mode=2):
25 return CSI + str(mode) + "K"
28 class AnsiCodes(object):
30 # the subclasses declare class attributes which are numbers.
31 # Upon instantiation we define instance attributes, which are the same
32 # as the class attributes but wrapped with the ANSI escape sequence
33 for name in dir(self):
34 if not name.startswith("_"):
35 value = getattr(self, name)
36 setattr(self, name, code_to_chars(value))
39 class AnsiCursor(object):
41 return CSI + str(n) + "A"
44 return CSI + str(n) + "B"
46 def FORWARD(self, n=1):
47 return CSI + str(n) + "C"
50 return CSI + str(n) + "D"
52 def POS(self, x=1, y=1):
53 return CSI + str(y) + ";" + str(x) + "H"
56 class AnsiFore(AnsiCodes):
67 # These are fairly well supported, but not part of the standard.
78 class AnsiBack(AnsiCodes):
89 # These are fairly well supported, but not part of the standard.
100 class AnsiStyle(AnsiCodes):
110 Cursor = AnsiCursor()