blob: 8fad06eb550c14bc30ad0360845b6e74228a8045 [file] [log] [blame]
Simon Glass0d24de92012-01-14 15:12:45 +00001# Copyright (c) 2011 The Chromium OS Authors.
2#
3# See file CREDITS for list of people who contributed to this
4# project.
5#
6# This program is free software; you can redistribute it and/or
7# modify it under the terms of the GNU General Public License as
8# published by the Free Software Foundation; either version 2 of
9# the License, or (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19# MA 02111-1307 USA
20#
21
22"""Terminal utilities
23
24This module handles terminal interaction including ANSI color codes.
25"""
26
Simon Glassbbd01432012-12-15 10:42:01 +000027import os
28import sys
29
30# Selection of when we want our output to be colored
31COLOR_IF_TERMINAL, COLOR_ALWAYS, COLOR_NEVER = range(3)
32
Simon Glass0d24de92012-01-14 15:12:45 +000033class Color(object):
34 """Conditionally wraps text in ANSI color escape sequences."""
35 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
36 BOLD = -1
37 COLOR_START = '\033[1;%dm'
38 BOLD_START = '\033[1m'
39 RESET = '\033[0m'
40
Simon Glassbbd01432012-12-15 10:42:01 +000041 def __init__(self, colored=COLOR_IF_TERMINAL):
Simon Glass0d24de92012-01-14 15:12:45 +000042 """Create a new Color object, optionally disabling color output.
43
44 Args:
45 enabled: True if color output should be enabled. If False then this
46 class will not add color codes at all.
47 """
Simon Glassbbd01432012-12-15 10:42:01 +000048 self._enabled = (colored == COLOR_ALWAYS or
49 (colored == COLOR_IF_TERMINAL and os.isatty(sys.stdout.fileno())))
Simon Glass0d24de92012-01-14 15:12:45 +000050
51 def Start(self, color):
52 """Returns a start color code.
53
54 Args:
55 color: Color to use, .e.g BLACK, RED, etc.
56
57 Returns:
58 If color is enabled, returns an ANSI sequence to start the given color,
59 otherwise returns empty string
60 """
61 if self._enabled:
62 return self.COLOR_START % (color + 30)
63 return ''
64
65 def Stop(self):
66 """Retruns a stop color code.
67
68 Returns:
69 If color is enabled, returns an ANSI color reset sequence, otherwise
70 returns empty string
71 """
72 if self._enabled:
73 return self.RESET
74 return ''
75
76 def Color(self, color, text):
77 """Returns text with conditionally added color escape sequences.
78
79 Keyword arguments:
80 color: Text color -- one of the color constants defined in this class.
81 text: The text to color.
82
83 Returns:
84 If self._enabled is False, returns the original text. If it's True,
85 returns text with color escape sequences based on the value of color.
86 """
87 if not self._enabled:
88 return text
89 if color == self.BOLD:
90 start = self.BOLD_START
91 else:
92 start = self.COLOR_START % (color + 30)
93 return start + text + self.RESET