Back to index

salome-kernel  6.5.0
termcolor.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 #
00003 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00004 #
00005 # This library is free software; you can redistribute it and/or
00006 # modify it under the terms of the GNU Lesser General Public
00007 # License as published by the Free Software Foundation; either
00008 # version 2.1 of the License.
00009 #
00010 # This library is distributed in the hope that it will be useful,
00011 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 # Lesser General Public License for more details.
00014 #
00015 # You should have received a copy of the GNU Lesser General Public
00016 # License along with this library; if not, write to the Free Software
00017 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00018 #
00019 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00020 #
00021 #  Author : Renaud Barate (EDF R&D)
00022 #  Date   : August 2009
00023 #
00024 """
00025 This module provides utility functions to display colored text in the
00026 terminal. It is based on ISO 6429 standard that defines control codes to
00027 change characters representation in color-capable ASCII terminals.
00028 
00029 In this module, colors are represented as lists of codes, so they can be added
00030 to obtain special effects (e.g. RED + GREEN_BG to display red text on green
00031 background). Several constants are defined for the most usual codes to
00032 facilitate the use of colors, but it is also possible to define colors
00033 directly from the corresponding code from ISO 6429 standard. In fact it is
00034 even necessary for less usual codes that don't have an associated constant
00035 (e.g. PURPLE + ['09'] can be used to display a crossed-out purple text).
00036 
00037 Example::
00038 
00039     import sys
00040     from salome.kernel import termcolor
00041     if termcolor.canDisplayColor(sys.stdout):
00042         print termcolor.makeColoredMessage("Hello world!", termcolor.BLUE)
00043     else:
00044         print "Hello world!"
00045 
00046 """
00047 
00048 # Constants for color codes
00049 DEFAULT    = ['00']
00050 """Default color for the terminal"""
00051 BOLD       = ['01']
00052 """Bold text and brighter colors"""
00053 UNDERLINED = ['04']
00054 """Underlined text"""
00055 BLACK_FG   = ['30']
00056 """Black foreground"""
00057 RED_FG     = ['31']
00058 """Red foreground"""
00059 GREEN_FG   = ['32']
00060 """Green foreground"""
00061 YELLOW_FG  = ['33']
00062 """Yellow foreground"""
00063 BLUE_FG    = ['34']
00064 """Blue foreground"""
00065 PURPLE_FG  = ['35']
00066 """Purple foreground"""
00067 CYAN_FG    = ['36']
00068 """Cyan foreground"""
00069 WHITE_FG   = ['37']
00070 """White foreground"""
00071 BLACK_BG   = ['40']
00072 """Black background"""
00073 RED_BG     = ['41']
00074 """Red background"""
00075 GREEN_BG   = ['42']
00076 """Green background"""
00077 YELLOW_BG  = ['43']
00078 """Yellow background"""
00079 BLUE_BG    = ['44']
00080 """Blue background"""
00081 PURPLE_BG  = ['45']
00082 """Purple background"""
00083 CYAN_BG    = ['46']
00084 """Cyan background"""
00085 WHITE_BG   = ['47']
00086 """White background"""
00087 
00088 # Constants for common colored text
00089 BLACK      = BLACK_FG
00090 """Black text (equivalent to BLACK_FG)"""
00091 RED        = BOLD + RED_FG
00092 """Red text (equivalent to BOLD + RED_FG)"""
00093 GREEN      = BOLD + GREEN_FG
00094 """Green text (equivalent to BOLD + GREEN_FG)"""
00095 YELLOW     = BOLD + YELLOW_FG
00096 """Yellow text (equivalent to BOLD + YELLOW_FG)"""
00097 BLUE       = BOLD + BLUE_FG
00098 """Blue text (equivalent to BOLD + BLUE_FG)"""
00099 PURPLE     = BOLD + PURPLE_FG
00100 """Purple text (equivalent to BOLD + PURPLE_FG)"""
00101 CYAN       = BOLD + CYAN_FG
00102 """Cyan text (equivalent to BOLD + CYAN_FG)"""
00103 WHITE      = WHITE_FG
00104 """White text (equivalent to WHITE_FG)"""
00105 
00106 
00107 def canDisplayColor(stream):
00108     """
00109     Return True if the stream can display colored text, False otherwise.
00110     """
00111     return hasattr(stream, "isatty") and stream.isatty()
00112 
00113 def getControlSequence(color):
00114     """
00115     Return the control sequence for the color in parameter, i.e. the string
00116     telling the terminal to display the following text in the given color.
00117     """
00118     seq = "\x1b["
00119     for i in range(len(color)):
00120         seq += color[i]
00121         if i < len(color)-1:
00122             seq += ";"
00123     seq += "m"
00124     return seq
00125 
00126 def makeColoredMessage(message, color):
00127     """
00128     Return a string that can be used to display the message in parameter with
00129     the given color.
00130     """
00131     return (getControlSequence(color) +
00132             str(message) +
00133             getControlSequence(DEFAULT))
00134 
00135 def TEST_termcolor():
00136     """Test function for termcolor module."""
00137     import sys
00138     if not canDisplayColor(sys.stdout):
00139         print "Standard output does not support colors."
00140         return
00141     print makeColoredMessage("This message must appear in blue.", BLUE)
00142     print makeColoredMessage("This message must appear in red on green " +
00143                              "background.", RED + GREEN_BG)
00144     print makeColoredMessage("This message must appear in magenta and " +
00145                              "crossed-out.", PURPLE + ['09'])
00146 
00147 
00148 # Main function only used to test the module
00149 if __name__ == "__main__":
00150     TEST_termcolor()