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 27bbd01435SSimon Glassimport os 28bbd01435SSimon Glassimport sys 29bbd01435SSimon Glass 30bbd01435SSimon Glass# Selection of when we want our output to be colored 31bbd01435SSimon GlassCOLOR_IF_TERMINAL, COLOR_ALWAYS, COLOR_NEVER = range(3) 32bbd01435SSimon 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 37*43bca004SSimon Glass BRIGHT_START = '\033[1;%dm' 38*43bca004SSimon Glass NORMAL_START = '\033[22;%dm' 390d24de9dSSimon Glass BOLD_START = '\033[1m' 400d24de9dSSimon Glass RESET = '\033[0m' 410d24de9dSSimon Glass 42bbd01435SSimon Glass def __init__(self, colored=COLOR_IF_TERMINAL): 430d24de9dSSimon Glass """Create a new Color object, optionally disabling color output. 440d24de9dSSimon Glass 450d24de9dSSimon Glass Args: 460d24de9dSSimon Glass enabled: True if color output should be enabled. If False then this 470d24de9dSSimon Glass class will not add color codes at all. 480d24de9dSSimon Glass """ 49bbd01435SSimon Glass self._enabled = (colored == COLOR_ALWAYS or 50bbd01435SSimon Glass (colored == COLOR_IF_TERMINAL and os.isatty(sys.stdout.fileno()))) 510d24de9dSSimon Glass 52*43bca004SSimon Glass def Start(self, color, bright=True): 530d24de9dSSimon Glass """Returns a start color code. 540d24de9dSSimon Glass 550d24de9dSSimon Glass Args: 560d24de9dSSimon Glass color: Color to use, .e.g BLACK, RED, etc. 570d24de9dSSimon Glass 580d24de9dSSimon Glass Returns: 590d24de9dSSimon Glass If color is enabled, returns an ANSI sequence to start the given color, 600d24de9dSSimon Glass otherwise returns empty string 610d24de9dSSimon Glass """ 620d24de9dSSimon Glass if self._enabled: 63*43bca004SSimon Glass base = self.BRIGHT_START if bright else self.NORMAL_START 64*43bca004SSimon Glass return base % (color + 30) 650d24de9dSSimon Glass return '' 660d24de9dSSimon Glass 670d24de9dSSimon Glass def Stop(self): 680d24de9dSSimon Glass """Retruns a stop color code. 690d24de9dSSimon Glass 700d24de9dSSimon Glass Returns: 710d24de9dSSimon Glass If color is enabled, returns an ANSI color reset sequence, otherwise 720d24de9dSSimon Glass returns empty string 730d24de9dSSimon Glass """ 740d24de9dSSimon Glass if self._enabled: 750d24de9dSSimon Glass return self.RESET 760d24de9dSSimon Glass return '' 770d24de9dSSimon Glass 78*43bca004SSimon Glass def Color(self, color, text, bright=True): 790d24de9dSSimon Glass """Returns text with conditionally added color escape sequences. 800d24de9dSSimon Glass 810d24de9dSSimon Glass Keyword arguments: 820d24de9dSSimon Glass color: Text color -- one of the color constants defined in this class. 830d24de9dSSimon Glass text: The text to color. 840d24de9dSSimon Glass 850d24de9dSSimon Glass Returns: 860d24de9dSSimon Glass If self._enabled is False, returns the original text. If it's True, 870d24de9dSSimon Glass returns text with color escape sequences based on the value of color. 880d24de9dSSimon Glass """ 890d24de9dSSimon Glass if not self._enabled: 900d24de9dSSimon Glass return text 910d24de9dSSimon Glass if color == self.BOLD: 920d24de9dSSimon Glass start = self.BOLD_START 930d24de9dSSimon Glass else: 94*43bca004SSimon Glass base = self.BRIGHT_START if bright else self.NORMAL_START 95*43bca004SSimon Glass start = base % (color + 30) 960d24de9dSSimon Glass return start + text + self.RESET 97