/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
import os
0.43.8 by Parth Malwankar
added color for regex pattern.
17
import re
0.43.4 by Parth Malwankar
initial support for color for fixed string grep.
18
import sys
0.43.2 by Parth Malwankar
added unix color codes.
19
0.43.8 by Parth Malwankar
added color for regex pattern.
20
0.43.2 by Parth Malwankar
added unix color codes.
21
class FG(object):
22
    """Unix terminal foreground color codes (16-color)."""
23
    RED = '\033[31m'
24
    GREEN = '\033[32m'
25
    YELLOW = '\033[33m'
26
    BLUE = '\033[34m'
27
    MAGENTA = '\033[35m'
28
    CYAN = '\033[36m'
29
    WHITE = '\033[37m'
30
31
    # Bold Foreground
32
    BOLD_RED = '\033[1;31m'
33
    BOLD_GREEN = '\033[1;32m'
34
    BOLD_YELLOW = '\033[1;33m'
35
    BOLD_BLUE = '\033[1;34m'
36
    BOLD_MAGENTA = '\033[1;35m'
37
    BOLD_CYAN = '\033[1;36m'
38
    BOLD_WHITE = '\033[1;37m'
39
0.43.4 by Parth Malwankar
initial support for color for fixed string grep.
40
    NONE = '\033[0m'
41
0.43.8 by Parth Malwankar
added color for regex pattern.
42
0.43.2 by Parth Malwankar
added unix color codes.
43
class BG(object):
44
    """Unix terminal background color codes (16-color)."""
45
    BLACK = '\033[40m'
46
    RED = '\033[41m'
47
    GREEN = '\033[42m'
48
    YELLOW = '\033[43m'
49
    BLUE = '\033[44m'
50
    MAGENTA = '\033[45m'
51
    CYAN = '\033[46m'
52
    WHITE = '\033[47m'
53
54
    NONE = '\033[0m'
55
0.43.8 by Parth Malwankar
added color for regex pattern.
56
57
def color_string(s, fg, bg=''):
0.43.4 by Parth Malwankar
initial support for color for fixed string grep.
58
    return fg + bg + s + FG.NONE
59
0.43.8 by Parth Malwankar
added color for regex pattern.
60
61
def re_color_string(compiled_pattern, s, fg):
62
    return compiled_pattern.sub(fg + r'\1' + FG.NONE, s)
63
64
0.43.4 by Parth Malwankar
initial support for color for fixed string grep.
65
def allow_color():
66
    if os.name != 'posix':
67
        return False
68
    if not sys.stdout.isatty():
69
        return False
70
    try:
71
        import curses
72
        curses.setupterm()
73
        return curses.tigetnum('colors') > 2
74
    except curses.error:
75
        return False
0.43.2 by Parth Malwankar
added unix color codes.
76