~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_termcolor.py

  • Committer: Tarmac
  • Author(s): Vincent Ladeuil
  • Date: 2017-01-30 14:42:05 UTC
  • mfrom: (6620.1.1 trunk)
  • Revision ID: tarmac-20170130144205-r8fh2xpmiuxyozpv
Merge  2.7 into trunk including fix for bug #1657238 [r=vila]

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
from __future__ import absolute_import
 
18
 
 
19
import os
 
20
import sys
 
21
 
 
22
 
 
23
class FG(object):
 
24
    """Unix terminal foreground color codes (16-color)."""
 
25
    RED = '\033[31m'
 
26
    GREEN = '\033[32m'
 
27
    YELLOW = '\033[33m'
 
28
    BLUE = '\033[34m'
 
29
    MAGENTA = '\033[35m'
 
30
    CYAN = '\033[36m'
 
31
    WHITE = '\033[37m'
 
32
 
 
33
    # Bold Foreground
 
34
    BOLD_RED = '\033[1;31m'
 
35
    BOLD_GREEN = '\033[1;32m'
 
36
    BOLD_YELLOW = '\033[1;33m'
 
37
    BOLD_BLUE = '\033[1;34m'
 
38
    BOLD_MAGENTA = '\033[1;35m'
 
39
    BOLD_CYAN = '\033[1;36m'
 
40
    BOLD_WHITE = '\033[1;37m'
 
41
 
 
42
    NONE = '\033[0m'
 
43
 
 
44
 
 
45
class BG(object):
 
46
    """Unix terminal background color codes (16-color)."""
 
47
    BLACK = '\033[40m'
 
48
    RED = '\033[41m'
 
49
    GREEN = '\033[42m'
 
50
    YELLOW = '\033[43m'
 
51
    BLUE = '\033[44m'
 
52
    MAGENTA = '\033[45m'
 
53
    CYAN = '\033[46m'
 
54
    WHITE = '\033[47m'
 
55
 
 
56
    NONE = '\033[0m'
 
57
 
 
58
 
 
59
def color_string(s, fg, bg=''):
 
60
    return fg + bg + s + FG.NONE
 
61
 
 
62
 
 
63
def re_color_string(compiled_pattern, s, fg):
 
64
    return compiled_pattern.sub(fg + r'\1' + FG.NONE, s)
 
65
 
 
66
 
 
67
def allow_color():
 
68
    if os.name != 'posix':
 
69
        return False
 
70
    if not sys.stdout.isatty():
 
71
        return False
 
72
    try:
 
73
        import curses
 
74
        curses.setupterm()
 
75
        return curses.tigetnum('colors') > 2
 
76
    except curses.error:
 
77
        return False
 
78