Salome HOME
Update copyrights 2014.
[modules/kernel.git] / src / KERNEL_PY / kernel / termcolor.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21 #  Author : Renaud Barate (EDF R&D)
22 #  Date   : August 2009
23 #
24 """
25 This module provides utility functions to display colored text in the
26 terminal. It is based on ISO 6429 standard that defines control codes to
27 change characters representation in color-capable ASCII terminals.
28
29 In this module, colors are represented as lists of codes, so they can be added
30 to obtain special effects (e.g. RED + GREEN_BG to display red text on green
31 background). Several constants are defined for the most usual codes to
32 facilitate the use of colors, but it is also possible to define colors
33 directly from the corresponding code from ISO 6429 standard. In fact it is
34 even necessary for less usual codes that don't have an associated constant
35 (e.g. PURPLE + ['09'] can be used to display a crossed-out purple text).
36
37 Example::
38
39     import sys
40     from salome.kernel import termcolor
41     if termcolor.canDisplayColor(sys.stdout):
42         print termcolor.makeColoredMessage("Hello world!", termcolor.BLUE)
43     else:
44         print "Hello world!"
45
46 """
47
48 # Constants for color codes
49 DEFAULT    = ['00']
50 """Default color for the terminal"""
51 BOLD       = ['01']
52 """Bold text and brighter colors"""
53 UNDERLINED = ['04']
54 """Underlined text"""
55 BLACK_FG   = ['30']
56 """Black foreground"""
57 RED_FG     = ['31']
58 """Red foreground"""
59 GREEN_FG   = ['32']
60 """Green foreground"""
61 YELLOW_FG  = ['33']
62 """Yellow foreground"""
63 BLUE_FG    = ['34']
64 """Blue foreground"""
65 PURPLE_FG  = ['35']
66 """Purple foreground"""
67 CYAN_FG    = ['36']
68 """Cyan foreground"""
69 WHITE_FG   = ['37']
70 """White foreground"""
71 BLACK_BG   = ['40']
72 """Black background"""
73 RED_BG     = ['41']
74 """Red background"""
75 GREEN_BG   = ['42']
76 """Green background"""
77 YELLOW_BG  = ['43']
78 """Yellow background"""
79 BLUE_BG    = ['44']
80 """Blue background"""
81 PURPLE_BG  = ['45']
82 """Purple background"""
83 CYAN_BG    = ['46']
84 """Cyan background"""
85 WHITE_BG   = ['47']
86 """White background"""
87
88 # Constants for common colored text
89 BLACK      = BLACK_FG
90 """Black text (equivalent to BLACK_FG)"""
91 RED        = BOLD + RED_FG
92 """Red text (equivalent to BOLD + RED_FG)"""
93 GREEN      = BOLD + GREEN_FG
94 """Green text (equivalent to BOLD + GREEN_FG)"""
95 YELLOW     = BOLD + YELLOW_FG
96 """Yellow text (equivalent to BOLD + YELLOW_FG)"""
97 BLUE       = BOLD + BLUE_FG
98 """Blue text (equivalent to BOLD + BLUE_FG)"""
99 PURPLE     = BOLD + PURPLE_FG
100 """Purple text (equivalent to BOLD + PURPLE_FG)"""
101 CYAN       = BOLD + CYAN_FG
102 """Cyan text (equivalent to BOLD + CYAN_FG)"""
103 WHITE      = WHITE_FG
104 """White text (equivalent to WHITE_FG)"""
105
106
107 def canDisplayColor(stream):
108     """
109     Return True if the stream can display colored text, False otherwise.
110     """
111     return hasattr(stream, "isatty") and stream.isatty()
112
113 def getControlSequence(color):
114     """
115     Return the control sequence for the color in parameter, i.e. the string
116     telling the terminal to display the following text in the given color.
117     """
118     seq = "\x1b["
119     for i in range(len(color)):
120         seq += color[i]
121         if i < len(color)-1:
122             seq += ";"
123     seq += "m"
124     return seq
125
126 def makeColoredMessage(message, color):
127     """
128     Return a string that can be used to display the message in parameter with
129     the given color.
130     """
131     return (getControlSequence(color) +
132             str(message) +
133             getControlSequence(DEFAULT))
134
135 def TEST_termcolor():
136     """Test function for termcolor module."""
137     import sys
138     if not canDisplayColor(sys.stdout):
139         print "Standard output does not support colors."
140         return
141     print makeColoredMessage("This message must appear in blue.", BLUE)
142     print makeColoredMessage("This message must appear in red on green " +
143                              "background.", RED + GREEN_BG)
144     print makeColoredMessage("This message must appear in magenta and " +
145                              "crossed-out.", PURPLE + ['09'])
146
147
148 # Main function only used to test the module
149 if __name__ == "__main__":
150     TEST_termcolor()