~bzr-pqm/bzr/bzr.dev

0.43.2 by Parth Malwankar
added unix color codes.
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
6531.3.9 by Jelmer Vernooij
Remove broken tests..
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0.40.147 by Jelmer Vernooij
Fix compatibility with newer versions of bzr: don't use relative imports in lazy imports, and import features from bzrlib.tests.features.
16
17
from __future__ import absolute_import
18
0.43.2 by Parth Malwankar
added unix color codes.
19
import os
0.43.4 by Parth Malwankar
initial support for color for fixed string grep.
20
import sys
0.43.2 by Parth Malwankar
added unix color codes.
21
0.43.8 by Parth Malwankar
added color for regex pattern.
22
0.43.2 by Parth Malwankar
added unix color codes.
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
0.43.4 by Parth Malwankar
initial support for color for fixed string grep.
42
    NONE = '\033[0m'
43
0.43.8 by Parth Malwankar
added color for regex pattern.
44
0.43.2 by Parth Malwankar
added unix color codes.
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
0.43.8 by Parth Malwankar
added color for regex pattern.
58
59
def color_string(s, fg, bg=''):
0.43.4 by Parth Malwankar
initial support for color for fixed string grep.
60
    return fg + bg + s + FG.NONE
61
0.43.8 by Parth Malwankar
added color for regex pattern.
62
63
def re_color_string(compiled_pattern, s, fg):
64
    return compiled_pattern.sub(fg + r'\1' + FG.NONE, s)
65
66
0.43.4 by Parth Malwankar
initial support for color for fixed string grep.
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
0.43.2 by Parth Malwankar
added unix color codes.
78