1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 """Module containing ANSI escape sequences and helper functions for colour terminal output."""
25
26
27 import ctypes
28 import sys
29
30
31 relax_prompt = "\033[94m"
32
33
34 relax_error = "\033[31m"
35
36
37 relax_warning = "\033[33m"
38
39
40 script = "\033[36m"
41
42
43 end = "\033[0m"
44
45
47 """Helper function for determining if control characters should be printed to the IO streams.
48
49 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).
50
51
52 @keyword stream: The stream to check. The value of 0 corresponds to STDIN, 1 corresponds to STDOUT, and 2 corresponds to STDERR.
53 @type stream: int
54 @return: The answer of whether color and other control characters should be printed.
55 @rtype: bool
56 """
57
58
59 if hasattr(ctypes, 'windll'):
60 return False
61
62
63 if stream == 0:
64 if not hasattr(sys.stdin, 'isatty'):
65 return False
66 return sys.stdin.isatty()
67 elif stream == 1:
68 if not hasattr(sys.stdout, 'isatty'):
69 return False
70 return sys.stdout.isatty()
71 elif stream == 2:
72 if not hasattr(sys.stderr, 'isatty'):
73 return False
74 return sys.stderr.isatty()
75 else:
76 return False
77