Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium OS Authors. |
| 2 | # |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 3 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 2613288 | 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 | a9f7edb | 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 | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 17 | class Color(object): |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 18 | """Conditionally wraps text in ANSI color escape sequences.""" |
| 19 | BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8) |
| 20 | BOLD = -1 |
| 21 | BRIGHT_START = '\033[1;%dm' |
| 22 | NORMAL_START = '\033[22;%dm' |
| 23 | BOLD_START = '\033[1m' |
| 24 | RESET = '\033[0m' |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 25 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 26 | def __init__(self, colored=COLOR_IF_TERMINAL): |
| 27 | """Create a new Color object, optionally disabling color output. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 28 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 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 | b0cd341 | 2014-08-28 09:43:35 -0600 | [diff] [blame^] | 33 | try: |
| 34 | self._enabled = (colored == COLOR_ALWAYS or |
| 35 | (colored == COLOR_IF_TERMINAL and |
| 36 | os.isatty(sys.stdout.fileno()))) |
| 37 | except: |
| 38 | self._enabled = False |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 39 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 40 | def Start(self, color, bright=True): |
| 41 | """Returns a start color code. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 42 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 43 | Args: |
| 44 | color: Color to use, .e.g BLACK, RED, etc. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 45 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 46 | Returns: |
| 47 | If color is enabled, returns an ANSI sequence to start the given |
| 48 | color, otherwise returns empty string |
| 49 | """ |
| 50 | if self._enabled: |
| 51 | base = self.BRIGHT_START if bright else self.NORMAL_START |
| 52 | return base % (color + 30) |
| 53 | return '' |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 54 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 55 | def Stop(self): |
| 56 | """Retruns a stop color code. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 57 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 58 | Returns: |
| 59 | If color is enabled, returns an ANSI color reset sequence, |
| 60 | otherwise returns empty string |
| 61 | """ |
| 62 | if self._enabled: |
| 63 | return self.RESET |
| 64 | return '' |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 65 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 66 | def Color(self, color, text, bright=True): |
| 67 | """Returns text with conditionally added color escape sequences. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 68 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 69 | Keyword arguments: |
| 70 | color: Text color -- one of the color constants defined in this |
| 71 | class. |
| 72 | text: The text to color. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 73 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 74 | Returns: |
| 75 | If self._enabled is False, returns the original text. If it's True, |
| 76 | returns text with color escape sequences based on the value of |
| 77 | color. |
| 78 | """ |
| 79 | if not self._enabled: |
| 80 | return text |
| 81 | if color == self.BOLD: |
| 82 | start = self.BOLD_START |
| 83 | else: |
| 84 | base = self.BRIGHT_START if bright else self.NORMAL_START |
| 85 | start = base % (color + 30) |
| 86 | return start + text + self.RESET |