1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # Author : Renaud Barate (EDF R&D)
25 ## \defgroup termcolor termcolor
28 # This module provides utility functions to display colored text in the
29 # terminal. It is based on ISO 6429 standard that defines control codes to
30 # change characters representation in color-capable ASCII terminals.
32 # In this module, colors are represented as lists of codes, so they can be added
33 # to obtain special effects (e.g. RED + GREEN_BG to display red text on green
34 # background). Several constants are defined for the most usual codes to
35 # facilitate the use of colors, but it is also possible to define colors
36 # directly from the corresponding code from ISO 6429 standard. In fact it is
37 # even necessary for less usual codes that don't have an associated constant
38 # (e.g. PURPLE + ['09'] can be used to display a crossed-out purple text).
42 # from salome.kernel import termcolor
43 # if termcolor.canDisplayColor(sys.stdout):
44 # print termcolor.makeColoredMessage("Hello world!", termcolor.BLUE)
46 # print "Hello world!"
51 This module provides utility functions to display colored text in the
52 terminal. It is based on ISO 6429 standard that defines control codes to
53 change characters representation in color-capable ASCII terminals.
55 In this module, colors are represented as lists of codes, so they can be added
56 to obtain special effects (e.g. RED + GREEN_BG to display red text on green
57 background). Several constants are defined for the most usual codes to
58 facilitate the use of colors, but it is also possible to define colors
59 directly from the corresponding code from ISO 6429 standard. In fact it is
60 even necessary for less usual codes that don't have an associated constant
61 (e.g. PURPLE + ['09'] can be used to display a crossed-out purple text).
66 from salome.kernel import termcolor
67 if termcolor.canDisplayColor(sys.stdout):
68 print termcolor.makeColoredMessage("Hello world!", termcolor.BLUE)
74 # Constants for color codes
75 ## Default color for the terminal
78 """Default color for the terminal"""
79 ## Bold text and brighter colors
82 """Bold text and brighter colors"""
90 """Black foreground"""
98 """Green foreground"""
102 """Yellow foreground"""
106 """Blue foreground"""
110 """Purple foreground"""
114 """Cyan foreground"""
118 """White foreground"""
122 """Black background"""
130 """Green background"""
134 """Yellow background"""
138 """Blue background"""
142 """Purple background"""
146 """Cyan background"""
150 """White background"""
152 # Constants for common colored text
153 ## Black text (equivalent to BLACK_FG)
156 """Black text (equivalent to BLACK_FG)"""
157 ## Red text (equivalent to BOLD + RED_FG)
160 """Red text (equivalent to BOLD + RED_FG)"""
161 ## Green text (equivalent to BOLD + GREEN_FG)
163 GREEN = BOLD + GREEN_FG
164 """Green text (equivalent to BOLD + GREEN_FG)"""
165 ## Yellow text (equivalent to BOLD + YELLOW_FG)
167 YELLOW = BOLD + YELLOW_FG
168 """Yellow text (equivalent to BOLD + YELLOW_FG)"""
169 ## Blue text (equivalent to BOLD + BLUE_FG)
171 BLUE = BOLD + BLUE_FG
172 """Blue text (equivalent to BOLD + BLUE_FG)"""
173 ## Purple text (equivalent to BOLD + PURPLE_FG)
175 PURPLE = BOLD + PURPLE_FG
176 """Purple text (equivalent to BOLD + PURPLE_FG)"""
177 ## Cyan text (equivalent to BOLD + CYAN_FG)
179 CYAN = BOLD + CYAN_FG
180 """Cyan text (equivalent to BOLD + CYAN_FG)"""
181 ## White text (equivalent to WHITE_FG)
184 """White text (equivalent to WHITE_FG)"""
186 ## Return True if the stream can display colored text, False otherwise.
188 def canDisplayColor(stream):
190 Return True if the stream can display colored text, False otherwise.
192 return hasattr(stream, "isatty") and stream.isatty()
194 ## Return the control sequence for the color in parameter, i.e. the string
195 # telling the terminal to display the following text in the given color.
197 def getControlSequence(color):
199 Return the control sequence for the color in parameter, i.e. the string
200 telling the terminal to display the following text in the given color.
203 for i in range(len(color)):
210 ## Return a string that can be used to display the message in parameter with
213 def makeColoredMessage(message, color):
215 Return a string that can be used to display the message in parameter with
218 return (getControlSequence(color) +
220 getControlSequence(DEFAULT))
222 ## Test function for termcolor module.
224 def TEST_termcolor():
225 """Test function for termcolor module."""
227 if not canDisplayColor(sys.stdout):
228 print "Standard output does not support colors."
230 print makeColoredMessage("This message must appear in blue.", BLUE)
231 print makeColoredMessage("This message must appear in red on green " +
232 "background.", RED + GREEN_BG)
233 print makeColoredMessage("This message must appear in magenta and " +
234 "crossed-out.", PURPLE + ['09'])
237 # Main function only used to test the module
238 if __name__ == "__main__":