Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 1 | # 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 | |
| 24 | This module handles terminal interaction including ANSI color codes. |
| 25 | """ |
| 26 | |
| 27 | class Color(object): |
| 28 | """Conditionally wraps text in ANSI color escape sequences.""" |
| 29 | BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8) |
| 30 | BOLD = -1 |
| 31 | COLOR_START = '\033[1;%dm' |
| 32 | BOLD_START = '\033[1m' |
| 33 | RESET = '\033[0m' |
| 34 | |
| 35 | def __init__(self, enabled=True): |
| 36 | """Create a new Color object, optionally disabling color output. |
| 37 | |
| 38 | Args: |
| 39 | enabled: True if color output should be enabled. If False then this |
| 40 | class will not add color codes at all. |
| 41 | """ |
| 42 | self._enabled = enabled |
| 43 | |
| 44 | def Start(self, color): |
| 45 | """Returns a start color code. |
| 46 | |
| 47 | Args: |
| 48 | color: Color to use, .e.g BLACK, RED, etc. |
| 49 | |
| 50 | Returns: |
| 51 | If color is enabled, returns an ANSI sequence to start the given color, |
| 52 | otherwise returns empty string |
| 53 | """ |
| 54 | if self._enabled: |
| 55 | return self.COLOR_START % (color + 30) |
| 56 | return '' |
| 57 | |
| 58 | def Stop(self): |
| 59 | """Retruns a stop color code. |
| 60 | |
| 61 | Returns: |
| 62 | If color is enabled, returns an ANSI color reset sequence, otherwise |
| 63 | returns empty string |
| 64 | """ |
| 65 | if self._enabled: |
| 66 | return self.RESET |
| 67 | return '' |
| 68 | |
| 69 | def Color(self, color, text): |
| 70 | """Returns text with conditionally added color escape sequences. |
| 71 | |
| 72 | Keyword arguments: |
| 73 | color: Text color -- one of the color constants defined in this class. |
| 74 | text: The text to color. |
| 75 | |
| 76 | Returns: |
| 77 | If self._enabled is False, returns the original text. If it's True, |
| 78 | returns text with color escape sequences based on the value of color. |
| 79 | """ |
| 80 | if not self._enabled: |
| 81 | return text |
| 82 | if color == self.BOLD: |
| 83 | start = self.BOLD_START |
| 84 | else: |
| 85 | start = self.COLOR_START % (color + 30) |
| 86 | return start + text + self.RESET |