Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium OS Authors. |
| 2 | # |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 3 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 4 | # |
| 5 | |
| 6 | """Terminal utilities |
| 7 | |
| 8 | This module handles terminal interaction including ANSI color codes. |
| 9 | """ |
| 10 | |
Simon Glass | bbd0143 | 2012-12-15 10:42:01 +0000 | [diff] [blame] | 11 | import os |
| 12 | import sys |
| 13 | |
| 14 | # Selection of when we want our output to be colored |
| 15 | COLOR_IF_TERMINAL, COLOR_ALWAYS, COLOR_NEVER = range(3) |
| 16 | |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 17 | class Color(object): |
| 18 | """Conditionally wraps text in ANSI color escape sequences.""" |
| 19 | BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8) |
| 20 | BOLD = -1 |
Simon Glass | 43bca00 | 2012-12-15 10:42:02 +0000 | [diff] [blame] | 21 | BRIGHT_START = '\033[1;%dm' |
| 22 | NORMAL_START = '\033[22;%dm' |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 23 | BOLD_START = '\033[1m' |
| 24 | RESET = '\033[0m' |
| 25 | |
Simon Glass | bbd0143 | 2012-12-15 10:42:01 +0000 | [diff] [blame] | 26 | def __init__(self, colored=COLOR_IF_TERMINAL): |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 27 | """Create a new Color object, optionally disabling color output. |
| 28 | |
| 29 | Args: |
| 30 | enabled: True if color output should be enabled. If False then this |
| 31 | class will not add color codes at all. |
| 32 | """ |
Simon Glass | bbd0143 | 2012-12-15 10:42:01 +0000 | [diff] [blame] | 33 | self._enabled = (colored == COLOR_ALWAYS or |
| 34 | (colored == COLOR_IF_TERMINAL and os.isatty(sys.stdout.fileno()))) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 35 | |
Simon Glass | 43bca00 | 2012-12-15 10:42:02 +0000 | [diff] [blame] | 36 | def Start(self, color, bright=True): |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 37 | """Returns a start color code. |
| 38 | |
| 39 | Args: |
| 40 | color: Color to use, .e.g BLACK, RED, etc. |
| 41 | |
| 42 | Returns: |
| 43 | If color is enabled, returns an ANSI sequence to start the given color, |
| 44 | otherwise returns empty string |
| 45 | """ |
| 46 | if self._enabled: |
Simon Glass | 43bca00 | 2012-12-15 10:42:02 +0000 | [diff] [blame] | 47 | base = self.BRIGHT_START if bright else self.NORMAL_START |
| 48 | return base % (color + 30) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 49 | return '' |
| 50 | |
| 51 | def Stop(self): |
| 52 | """Retruns a stop color code. |
| 53 | |
| 54 | Returns: |
| 55 | If color is enabled, returns an ANSI color reset sequence, otherwise |
| 56 | returns empty string |
| 57 | """ |
| 58 | if self._enabled: |
Simon Glass | 43bca00 | 2012-12-15 10:42:02 +0000 | [diff] [blame] | 59 | return self.RESET |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 60 | return '' |
| 61 | |
Simon Glass | 43bca00 | 2012-12-15 10:42:02 +0000 | [diff] [blame] | 62 | def Color(self, color, text, bright=True): |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 63 | """Returns text with conditionally added color escape sequences. |
| 64 | |
| 65 | Keyword arguments: |
| 66 | color: Text color -- one of the color constants defined in this class. |
| 67 | text: The text to color. |
| 68 | |
| 69 | Returns: |
| 70 | If self._enabled is False, returns the original text. If it's True, |
| 71 | returns text with color escape sequences based on the value of color. |
| 72 | """ |
| 73 | if not self._enabled: |
Simon Glass | 43bca00 | 2012-12-15 10:42:02 +0000 | [diff] [blame] | 74 | return text |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 75 | if color == self.BOLD: |
Simon Glass | 43bca00 | 2012-12-15 10:42:02 +0000 | [diff] [blame] | 76 | start = self.BOLD_START |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 77 | else: |
Simon Glass | 43bca00 | 2012-12-15 10:42:02 +0000 | [diff] [blame] | 78 | base = self.BRIGHT_START if bright else self.NORMAL_START |
| 79 | start = base % (color + 30) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 80 | return start + text + self.RESET |