1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """Module containing ANSI escape sequences and helper functions for colour terminal output."""
24
25
26 try:
27 import ctypes
28 except ImportError:
29 ctypes = None
30 import sys
31
32
33 relax_prompt = "\033[94m"
34
35
36 relax_error = "\033[31m"
37
38
39 relax_warning = "\033[33m"
40
41
42 script = "\033[36m"
43
44
45 end = "\033[0m"
46
47
49 """Helper function for determining if control characters should be printed to the IO streams.
50
51 This uses both the sys.std*.isatty() methods as well as the operating system. Control characters are only shown on GNU/Linux and Mac OS X (or technically they are disabled on MS Windows as both cmd and the PowerShell do not support the ANSI characters).
52
53
54 @keyword stream: The stream to check. The value of 0 corresponds to STDIN, 1 corresponds to STDOUT, and 2 corresponds to STDERR.
55 @type stream: int
56 @return: The answer of whether color and other control characters should be printed.
57 @rtype: bool
58 """
59
60
61 if hasattr(ctypes, 'windll'):
62 return False
63
64
65 if stream == 0:
66 if not hasattr(sys.stdin, 'isatty'):
67 return False
68 return sys.stdin.isatty()
69 elif stream == 1:
70 if not hasattr(sys.stdout, 'isatty'):
71 return False
72 return sys.stdout.isatty()
73 elif stream == 2:
74 if not hasattr(sys.stderr, 'isatty'):
75 return False
76 return sys.stderr.isatty()
77 else:
78 return False
79