equal
deleted
inserted
replaced
|
1 """ |
|
2 Sets up the terminal color scheme. |
|
3 """ |
|
4 |
|
5 import sys |
|
6 |
|
7 from django.utils import termcolors |
|
8 |
|
9 def supports_color(): |
|
10 """ |
|
11 Returns True if the running system's terminal supports color, and False |
|
12 otherwise. |
|
13 """ |
|
14 unsupported_platform = (sys.platform in ('win32', 'Pocket PC') |
|
15 or sys.platform.startswith('java')) |
|
16 # isatty is not always implemented, #6223. |
|
17 is_a_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty() |
|
18 if unsupported_platform or not is_a_tty: |
|
19 return False |
|
20 return True |
|
21 |
|
22 def color_style(): |
|
23 """Returns a Style object with the Django color scheme.""" |
|
24 if not supports_color(): |
|
25 return no_style() |
|
26 class dummy: pass |
|
27 style = dummy() |
|
28 style.ERROR = termcolors.make_style(fg='red', opts=('bold',)) |
|
29 style.ERROR_OUTPUT = termcolors.make_style(fg='red', opts=('bold',)) |
|
30 style.NOTICE = termcolors.make_style(fg='red') |
|
31 style.SQL_FIELD = termcolors.make_style(fg='green', opts=('bold',)) |
|
32 style.SQL_COLTYPE = termcolors.make_style(fg='green') |
|
33 style.SQL_KEYWORD = termcolors.make_style(fg='yellow') |
|
34 style.SQL_TABLE = termcolors.make_style(opts=('bold',)) |
|
35 return style |
|
36 |
|
37 def no_style(): |
|
38 """Returns a Style object that has no colors.""" |
|
39 class dummy: |
|
40 def __getattr__(self, attr): |
|
41 return lambda x: x |
|
42 return dummy() |