1#!/usr/bin/python 2 3# Copyright (c) 2012 The Chromium OS Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7''' A mini class for terminal color ''' 8 9import re 10 11 12CSI = '\033[' # ANSI Control Sequence Introducer 13NORMAL = CSI + '0m' 14CODE = {'black': CSI + '1;30m', # color code for foreground color 15 'red': CSI + '1;31m', 16 'green': CSI + '1;32m', 17 'yellow': CSI + '1;33m', 18 'blue': CSI + '1;34m', 19 'magenta': CSI + '1;35m', 20 'cyan': CSI + '1;36m', 21 'white': CSI + '1;37m', 22 'default': CSI + '1;39m', 23 } 24 25 26def color_string(string, bgn_sym, end_sym, color): 27 ''' Insert color code for a bracketed substring in a given string ''' 28 cstring = re.sub(bgn_sym, CODE[color] + bgn_sym, string) 29 cstring = re.sub(end_sym, end_sym + NORMAL, cstring) 30 return cstring 31