mailr15796 - in /1.3: ansi.py prompt/interpreter.py relax_errors.py relax_warnings.py


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by edward on April 23, 2012 - 12:36:
Author: bugman
Date: Mon Apr 23 12:36:12 2012
New Revision: 15796

URL: http://svn.gna.org/viewcvs/relax?rev=15796&view=rev
Log:
ANSI control characters are now suppressed on MS Windows.

The characters are useless in both the PowerShell and cmd.  Therefore the
ansi.enable_control_chars() helper function has been created to replace the 
sys.std*.isatty() calls
throughout relax.


Modified:
    1.3/ansi.py
    1.3/prompt/interpreter.py
    1.3/relax_errors.py
    1.3/relax_warnings.py

Modified: 1.3/ansi.py
URL: 
http://svn.gna.org/viewcvs/relax/1.3/ansi.py?rev=15796&r1=15795&r2=15796&view=diff
==============================================================================
--- 1.3/ansi.py (original)
+++ 1.3/ansi.py Mon Apr 23 12:36:12 2012
@@ -21,8 +21,11 @@
 
###############################################################################
 
 # Module docstring.
-"""Module containing ANSI escape sequences for colour terminal output."""
+"""Module containing ANSI escape sequences and helper functions for colour 
terminal output."""
 
+# Python module imports.
+import ctypes
+import sys
 
 # The relax prompt.
 relax_prompt = "\033[94m"
@@ -40,3 +43,28 @@
 end = "\033[0m"
 
 
+def enable_control_chars(stream=1):
+    """Helper function for determining if control characters should be 
printed to the IO streams.
+
+    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.
+
+
+    @keyword stream:    The stream to check.  The value of 0 corresponds to 
STDIN, 1 corresponds to STDOUT, and 2 corresponds to STDERR.
+    @type stream:       int
+    @return:            The answer of whether color and other control 
characters should be printed.
+    @rtype:             bool
+    """
+
+    # MS Windows, therefore always return False.
+    if hasattr(ctypes, 'windll'):
+        return False
+
+    # The STDIO streams.
+    if stream == 0:
+        return sys.stdin.isatty()
+    elif stream == 1:
+        return sys.stdout.isatty()
+    elif stream == 2:
+        return sys.stderr.isatty()
+    else:
+        return False

Modified: 1.3/prompt/interpreter.py
URL: 
http://svn.gna.org/viewcvs/relax/1.3/prompt/interpreter.py?rev=15796&r1=15795&r2=15796&view=diff
==============================================================================
--- 1.3/prompt/interpreter.py (original)
+++ 1.3/prompt/interpreter.py Mon Apr 23 12:36:12 2012
@@ -132,7 +132,7 @@
         self._exec_info = Exec_info()
 
         # The prompts (change the Python prompt, as well as the function 
print outs).
-        if sys.stdout.isatty():
+        if ansi.enable_control_chars(stream=1):
             self._exec_info.prompt_colour_on()
         else:
             self._exec_info.prompt_colour_off()
@@ -484,7 +484,7 @@
                 return
 
         # Coloured text.
-        if sys.stdout.isatty():
+        if ansi.enable_control_chars(stream=1):
             sys.stdout.write(ansi.script)
 
         # Print the script.
@@ -494,7 +494,7 @@
         
sys.stdout.write("----------------------------------------------------------------------------------------------------")
 
         # End coloured text.
-        if sys.stdout.isatty():
+        if ansi.enable_control_chars(stream=1):
             sys.stdout.write(ansi.end)
 
         # Terminating newline.

Modified: 1.3/relax_errors.py
URL: 
http://svn.gna.org/viewcvs/relax/1.3/relax_errors.py?rev=15796&r1=15795&r2=15796&view=diff
==============================================================================
--- 1.3/relax_errors.py (original)
+++ 1.3/relax_errors.py Mon Apr 23 12:36:12 2012
@@ -108,7 +108,7 @@
             save_state()
 
         # Modify the error message to include 'RelaxError' at the start 
(using coloured text if a TTY).
-        if sys.stderr.isatty():
+        if ansi.enable_control_chars(stream=2):
             return ("%sRelaxError: %s%s\n" % (ansi.relax_error, self.text, 
ansi.end))
         else:
             return ("RelaxError: %s\n" % self.text)

Modified: 1.3/relax_warnings.py
URL: 
http://svn.gna.org/viewcvs/relax/1.3/relax_warnings.py?rev=15796&r1=15795&r2=15796&view=diff
==============================================================================
--- 1.3/relax_warnings.py (original)
+++ 1.3/relax_warnings.py Mon Apr 23 12:36:12 2012
@@ -59,7 +59,7 @@
         message = tb + message
 
     # Text colouring
-    if sys.stderr.isatty():
+    if ansi.enable_control_chars(stream=2):
         # Strip the last newline, if it exists.
         if message[-1] == '\n':
             message = message[:-1]




Related Messages


Powered by MHonArc, Updated Mon Apr 23 12:40:02 2012