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 import dep_check
27
28
29 if dep_check.ctypes_module:
30 import ctypes
31 else:
32 ctypes = None
33 import sys
34
35
36 relax_prompt = "\033[94m"
37
38
39 relax_error = "\033[31m"
40
41
42 relax_warning = "\033[33m"
43
44
45 script = "\033[36m"
46
47
48 end = "\033[0m"
49
50
52 """Helper function for determining if control characters should be printed to the IO streams.
53
54 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).
55
56
57 @keyword stream: The stream to check. The value of 0 corresponds to STDIN, 1 corresponds to STDOUT, and 2 corresponds to STDERR.
58 @type stream: int
59 @return: The answer of whether color and other control characters should be printed.
60 @rtype: bool
61 """
62
63
64 if hasattr(ctypes, 'windll'):
65 return False
66
67
68 if stream == 0:
69 if not hasattr(sys.stdin, 'isatty'):
70 return False
71 return sys.stdin.isatty()
72 elif stream == 1:
73 if not hasattr(sys.stdout, 'isatty'):
74 return False
75 return sys.stdout.isatty()
76 elif stream == 2:
77 if not hasattr(sys.stderr, 'isatty'):
78 return False
79 return sys.stderr.isatty()
80 else:
81 return False
82