blob: 963f2f891b1748ca08440d81f9ab6612b6053919 [file] [log] [blame]
Simon Glass26132882012-01-14 15:12:45 +00001# Copyright (c) 2011 The Chromium OS Authors.
2#
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02003# SPDX-License-Identifier: GPL-2.0+
Simon Glass26132882012-01-14 15:12:45 +00004#
5
6"""Terminal utilities
7
8This module handles terminal interaction including ANSI color codes.
9"""
10
Simon Glassa9f7edb2012-12-15 10:42:01 +000011import os
12import sys
13
14# Selection of when we want our output to be colored
15COLOR_IF_TERMINAL, COLOR_ALWAYS, COLOR_NEVER = range(3)
16
Simon Glass26132882012-01-14 15:12:45 +000017class Color(object):
Simon Glass381fad82014-08-28 09:43:34 -060018 """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 Glass26132882012-01-14 15:12:45 +000025
Simon Glass381fad82014-08-28 09:43:34 -060026 def __init__(self, colored=COLOR_IF_TERMINAL):
27 """Create a new Color object, optionally disabling color output.
Simon Glass26132882012-01-14 15:12:45 +000028
Simon Glass381fad82014-08-28 09:43:34 -060029 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 Glassb0cd3412014-08-28 09:43:35 -060033 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 Glass26132882012-01-14 15:12:45 +000039
Simon Glass381fad82014-08-28 09:43:34 -060040 def Start(self, color, bright=True):
41 """Returns a start color code.
Simon Glass26132882012-01-14 15:12:45 +000042
Simon Glass381fad82014-08-28 09:43:34 -060043 Args:
44 color: Color to use, .e.g BLACK, RED, etc.
Simon Glass26132882012-01-14 15:12:45 +000045
Simon Glass381fad82014-08-28 09:43:34 -060046 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 Glass26132882012-01-14 15:12:45 +000054
Simon Glass381fad82014-08-28 09:43:34 -060055 def Stop(self):
56 """Retruns a stop color code.
Simon Glass26132882012-01-14 15:12:45 +000057
Simon Glass381fad82014-08-28 09:43:34 -060058 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 Glass26132882012-01-14 15:12:45 +000065
Simon Glass381fad82014-08-28 09:43:34 -060066 def Color(self, color, text, bright=True):
67 """Returns text with conditionally added color escape sequences.
Simon Glass26132882012-01-14 15:12:45 +000068
Simon Glass381fad82014-08-28 09:43:34 -060069 Keyword arguments:
70 color: Text color -- one of the color constants defined in this
71 class.
72 text: The text to color.
Simon Glass26132882012-01-14 15:12:45 +000073
Simon Glass381fad82014-08-28 09:43:34 -060074 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