Module ansi
[hide private]
[frames] | no frames]

Source Code for Module ansi

 1  ############################################################################### 
 2  #                                                                             # 
 3  # Copyright (C) 2012 Edward d'Auvergne                                        # 
 4  #                                                                             # 
 5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
 6  #                                                                             # 
 7  # This program is free software: you can redistribute it and/or modify        # 
 8  # it under the terms of the GNU General Public License as published by        # 
 9  # the Free Software Foundation, either version 3 of the License, or           # 
10  # (at your option) any later version.                                         # 
11  #                                                                             # 
12  # This program is distributed in the hope that it will be useful,             # 
13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
15  # GNU General Public License for more details.                                # 
16  #                                                                             # 
17  # You should have received a copy of the GNU General Public License           # 
18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
19  #                                                                             # 
20  ############################################################################### 
21   
22  # Module docstring. 
23  """Module containing ANSI escape sequences and helper functions for colour terminal output.""" 
24   
25  # Dependencies. 
26  import dep_check 
27   
28  # Python module imports. 
29  if dep_check.ctypes_module: 
30      import ctypes 
31  else: 
32      ctypes = None 
33  import sys 
34   
35  # The relax prompt. 
36  relax_prompt = "\033[94m" 
37   
38  # RelaxErrors. 
39  relax_error = "\033[31m" 
40   
41  # RelaxWarnings. 
42  relax_warning = "\033[33m" 
43   
44  # Script printout. 
45  script = "\033[36m" 
46   
47  # The terminating sequence. 
48  end = "\033[0m" 
49   
50   
51 -def enable_control_chars(stream=1):
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 # MS Windows, therefore always return False. 64 if hasattr(ctypes, 'windll'): 65 return False 66 67 # The STDIO streams. 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