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.                                     # 
 6  #                                                                             # 
 7  # relax 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 2 of the License, or           # 
10  # (at your option) any later version.                                         # 
11  #                                                                             # 
12  # relax 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 relax; if not, write to the Free Software                        # 
19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
20  #                                                                             # 
21  ############################################################################### 
22   
23  # Module docstring. 
24  """Module containing ANSI escape sequences and helper functions for colour terminal output.""" 
25   
26  # Python module imports. 
27  import ctypes 
28  import sys 
29   
30  # The relax prompt. 
31  relax_prompt = "\033[94m" 
32   
33  # RelaxErrors. 
34  relax_error = "\033[31m" 
35   
36  # RelaxWarnings. 
37  relax_warning = "\033[33m" 
38   
39  # Script printout. 
40  script = "\033[36m" 
41   
42  # The terminating sequence. 
43  end = "\033[0m" 
44   
45   
46 -def enable_control_chars(stream=1):
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 # MS Windows, therefore always return False. 59 if hasattr(ctypes, 'windll'): 60 return False 61 62 # The STDIO streams. 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