Package lib :: Module ansi
[hide private]
[frames] | no frames]

Source Code for Module lib.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  # Python module imports. 
26  try: 
27      import ctypes 
28  except ImportError: 
29      ctypes = None 
30  import sys 
31   
32  # The relax prompt. 
33  relax_prompt = "\033[94m" 
34   
35  # RelaxErrors. 
36  relax_error = "\033[31m" 
37   
38  # RelaxWarnings. 
39  relax_warning = "\033[33m" 
40   
41  # Script printout. 
42  script = "\033[36m" 
43   
44  # The terminating sequence. 
45  end = "\033[0m" 
46   
47   
48 -def enable_control_chars(stream=1):
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 # MS Windows, therefore always return False. 61 if hasattr(ctypes, 'windll'): 62 return False 63 64 # The STDIO streams. 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