1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2007-2013 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.
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 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.
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).
40 from salome.kernel import termcolor
41 if termcolor.canDisplayColor(sys.stdout):
42 print termcolor.makeColoredMessage("Hello world!", termcolor.BLUE)
48 # Constants for color codes
50 """Default color for the terminal"""
52 """Bold text and brighter colors"""
56 """Black foreground"""
60 """Green foreground"""
62 """Yellow foreground"""
66 """Purple foreground"""
70 """White foreground"""
72 """Black background"""
76 """Green background"""
78 """Yellow background"""
82 """Purple background"""
86 """White background"""
88 # Constants for common colored text
90 """Black text (equivalent to BLACK_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)"""
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)"""
104 """White text (equivalent to WHITE_FG)"""
107 def canDisplayColor(stream):
109 Return True if the stream can display colored text, False otherwise.
111 return hasattr(stream, "isatty") and stream.isatty()
113 def getControlSequence(color):
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.
119 for i in range(len(color)):
126 def makeColoredMessage(message, color):
128 Return a string that can be used to display the message in parameter with
131 return (getControlSequence(color) +
133 getControlSequence(DEFAULT))
135 def TEST_termcolor():
136 """Test function for termcolor module."""
138 if not canDisplayColor(sys.stdout):
139 print "Standard output does not support colors."
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'])
148 # Main function only used to test the module
149 if __name__ == "__main__":