10d24de9dSSimon Glass# Copyright (c) 2011 The Chromium OS Authors. 20d24de9dSSimon Glass# 30d24de9dSSimon Glass# See file CREDITS for list of people who contributed to this 40d24de9dSSimon Glass# project. 50d24de9dSSimon Glass# 60d24de9dSSimon Glass# This program is free software; you can redistribute it and/or 70d24de9dSSimon Glass# modify it under the terms of the GNU General Public License as 80d24de9dSSimon Glass# published by the Free Software Foundation; either version 2 of 90d24de9dSSimon Glass# the License, or (at your option) any later version. 100d24de9dSSimon Glass# 110d24de9dSSimon Glass# This program is distributed in the hope that it will be useful, 120d24de9dSSimon Glass# but WITHOUT ANY WARRANTY; without even the implied warranty of 130d24de9dSSimon Glass# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 140d24de9dSSimon Glass# GNU General Public License for more details. 150d24de9dSSimon Glass# 160d24de9dSSimon Glass# You should have received a copy of the GNU General Public License 170d24de9dSSimon Glass# along with this program; if not, write to the Free Software 180d24de9dSSimon Glass# Foundation, Inc., 59 Temple Place, Suite 330, Boston, 190d24de9dSSimon Glass# MA 02111-1307 USA 200d24de9dSSimon Glass# 210d24de9dSSimon Glass 220d24de9dSSimon Glass"""Terminal utilities 230d24de9dSSimon Glass 240d24de9dSSimon GlassThis module handles terminal interaction including ANSI color codes. 250d24de9dSSimon Glass""" 260d24de9dSSimon Glass 27*bbd01435SSimon Glassimport os 28*bbd01435SSimon Glassimport sys 29*bbd01435SSimon Glass 30*bbd01435SSimon Glass# Selection of when we want our output to be colored 31*bbd01435SSimon GlassCOLOR_IF_TERMINAL, COLOR_ALWAYS, COLOR_NEVER = range(3) 32*bbd01435SSimon Glass 330d24de9dSSimon Glassclass Color(object): 340d24de9dSSimon Glass """Conditionally wraps text in ANSI color escape sequences.""" 350d24de9dSSimon Glass BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8) 360d24de9dSSimon Glass BOLD = -1 370d24de9dSSimon Glass COLOR_START = '\033[1;%dm' 380d24de9dSSimon Glass BOLD_START = '\033[1m' 390d24de9dSSimon Glass RESET = '\033[0m' 400d24de9dSSimon Glass 41*bbd01435SSimon Glass def __init__(self, colored=COLOR_IF_TERMINAL): 420d24de9dSSimon Glass """Create a new Color object, optionally disabling color output. 430d24de9dSSimon Glass 440d24de9dSSimon Glass Args: 450d24de9dSSimon Glass enabled: True if color output should be enabled. If False then this 460d24de9dSSimon Glass class will not add color codes at all. 470d24de9dSSimon Glass """ 48*bbd01435SSimon Glass self._enabled = (colored == COLOR_ALWAYS or 49*bbd01435SSimon Glass (colored == COLOR_IF_TERMINAL and os.isatty(sys.stdout.fileno()))) 500d24de9dSSimon Glass 510d24de9dSSimon Glass def Start(self, color): 520d24de9dSSimon Glass """Returns a start color code. 530d24de9dSSimon Glass 540d24de9dSSimon Glass Args: 550d24de9dSSimon Glass color: Color to use, .e.g BLACK, RED, etc. 560d24de9dSSimon Glass 570d24de9dSSimon Glass Returns: 580d24de9dSSimon Glass If color is enabled, returns an ANSI sequence to start the given color, 590d24de9dSSimon Glass otherwise returns empty string 600d24de9dSSimon Glass """ 610d24de9dSSimon Glass if self._enabled: 620d24de9dSSimon Glass return self.COLOR_START % (color + 30) 630d24de9dSSimon Glass return '' 640d24de9dSSimon Glass 650d24de9dSSimon Glass def Stop(self): 660d24de9dSSimon Glass """Retruns a stop color code. 670d24de9dSSimon Glass 680d24de9dSSimon Glass Returns: 690d24de9dSSimon Glass If color is enabled, returns an ANSI color reset sequence, otherwise 700d24de9dSSimon Glass returns empty string 710d24de9dSSimon Glass """ 720d24de9dSSimon Glass if self._enabled: 730d24de9dSSimon Glass return self.RESET 740d24de9dSSimon Glass return '' 750d24de9dSSimon Glass 760d24de9dSSimon Glass def Color(self, color, text): 770d24de9dSSimon Glass """Returns text with conditionally added color escape sequences. 780d24de9dSSimon Glass 790d24de9dSSimon Glass Keyword arguments: 800d24de9dSSimon Glass color: Text color -- one of the color constants defined in this class. 810d24de9dSSimon Glass text: The text to color. 820d24de9dSSimon Glass 830d24de9dSSimon Glass Returns: 840d24de9dSSimon Glass If self._enabled is False, returns the original text. If it's True, 850d24de9dSSimon Glass returns text with color escape sequences based on the value of color. 860d24de9dSSimon Glass """ 870d24de9dSSimon Glass if not self._enabled: 880d24de9dSSimon Glass return text 890d24de9dSSimon Glass if color == self.BOLD: 900d24de9dSSimon Glass start = self.BOLD_START 910d24de9dSSimon Glass else: 920d24de9dSSimon Glass start = self.COLOR_START % (color + 30) 930d24de9dSSimon Glass return start + text + self.RESET 94