Skip to content

Commit ca46ac4

Browse files
committed
add --color argument
1 parent 5b03c0b commit ca46ac4

File tree

2 files changed

+44
-44
lines changed

2 files changed

+44
-44
lines changed

pythonforandroid/logger.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,35 @@ def format(self, record):
6161

6262
class colorama_shim(object):
6363

64-
def __init__(self):
64+
def __init__(self, real):
6565
self._dict = defaultdict(str)
66+
self._real = real
67+
self._enabled = False
6668

6769
def __getattr__(self, key):
68-
return self._dict[key]
70+
return getattr(self._real, key) if self._enabled else self._dict[key]
6971

70-
Null_Style = Null_Fore = colorama_shim()
72+
def enable(self, enable):
73+
self._enabled = enable
7174

72-
if stdout.isatty():
73-
Out_Style = Colo_Style
74-
Out_Fore = Colo_Fore
75-
else:
76-
Out_Style = Null_Style
77-
Out_Fore = Null_Fore
75+
Out_Style = colorama_shim(Colo_Style)
76+
Out_Fore = colorama_shim(Colo_Fore)
77+
Err_Style = colorama_shim(Colo_Style)
78+
Err_Fore = colorama_shim(Colo_Fore)
7879

79-
if stderr.isatty():
80-
Err_Style = Colo_Style
81-
Err_Fore = Colo_Fore
82-
else:
83-
Err_Style = Null_Style
84-
Err_Fore = Null_Fore
80+
81+
def setup_color(color):
82+
enable_out = (False if color == 'never' else
83+
True if color == 'always' else
84+
stdout.isatty())
85+
Out_Style.enable(enable_out)
86+
Out_Fore.enable(enable_out)
87+
88+
enable_err = (False if color == 'never' else
89+
True if color == 'always' else
90+
stderr.isatty())
91+
Err_Style.enable(enable_err)
92+
Err_Fore.enable(enable_err)
8593

8694

8795
def info_main(*args):

pythonforandroid/toolchain.py

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@
2828
CompiledComponentsPythonRecipe,
2929
BootstrapNDKRecipe, NDKRecipe)
3030
from pythonforandroid.archs import (ArchARM, ArchARMv7_a, Archx86)
31-
from pythonforandroid.logger import (logger, info, warning, debug,
31+
from pythonforandroid.logger import (logger, info, warning, setup_color,
3232
Out_Style, Out_Fore, Err_Style, Err_Fore,
33-
info_notify, info_main, shprint,
34-
Null_Fore, Null_Style)
33+
info_notify, info_main, shprint)
3534
from pythonforandroid.util import current_directory, ensure_dir
3635
from pythonforandroid.bootstrap import Bootstrap
3736
from pythonforandroid.distribution import Distribution, pretty_log_dists
@@ -43,17 +42,6 @@
4342
sys.path.insert(0, join(toolchain_dir, "tools", "external"))
4443

4544

46-
info(''.join(
47-
[Err_Style.BRIGHT, Err_Fore.RED,
48-
'This python-for-android revamp is an experimental alpha release!',
49-
Err_Style.RESET_ALL]))
50-
info(''.join(
51-
[Err_Fore.RED,
52-
('It should work (mostly), but you may experience '
53-
'missing features or bugs.'),
54-
Err_Style.RESET_ALL]))
55-
56-
5745
def add_boolean_option(parser, names, no_names=None,
5846
default=True, dest=None, description=None):
5947
group = parser.add_argument_group(description=description)
@@ -204,6 +192,9 @@ def __init__(self):
204192
parser.add_argument(
205193
'--debug', dest='debug', action='store_true',
206194
help='Display debug output and all build info')
195+
parser.add_argument(
196+
'--color', dest='color', choices=['always', 'never', 'auto'],
197+
help='Enable or disable color output (default enabled on tty)')
207198
parser.add_argument(
208199
'--sdk-dir', '--sdk_dir', dest='sdk_dir', default='',
209200
help='The filepath where the Android SDK is installed')
@@ -282,6 +273,18 @@ def __init__(self):
282273
args, unknown = parser.parse_known_args(sys.argv[1:])
283274
self.dist_args = args
284275

276+
setup_color(args.color)
277+
278+
info(''.join(
279+
[Err_Style.BRIGHT, Err_Fore.RED,
280+
'This python-for-android revamp is an experimental alpha release!',
281+
Err_Style.RESET_ALL]))
282+
info(''.join(
283+
[Err_Fore.RED,
284+
('It should work (mostly), but you may experience '
285+
'missing features or bugs.'),
286+
Err_Style.RESET_ALL]))
287+
285288
# strip version from requirements, and put them in environ
286289
requirements = []
287290
for requirement in split_argument_list(args.requirements):
@@ -357,19 +360,8 @@ def recipes(self, args):
357360
"--compact", action="store_true", default=False,
358361
help="Produce a compact list suitable for scripting")
359362

360-
add_boolean_option(
361-
parser, ["color"],
362-
default=True,
363-
description='Whether the output should be colored:')
364-
365363
args = parser.parse_args(args)
366364

367-
Fore = Out_Fore
368-
Style = Out_Style
369-
if not args.color:
370-
Fore = Null_Fore
371-
Style = Null_Style
372-
373365
ctx = self.ctx
374366
if args.compact:
375367
print(" ".join(set(Recipe.list_recipes(ctx))))
@@ -380,18 +372,18 @@ def recipes(self, args):
380372
print('{Fore.BLUE}{Style.BRIGHT}{recipe.name:<12} '
381373
'{Style.RESET_ALL}{Fore.LIGHTBLUE_EX}'
382374
'{version:<8}{Style.RESET_ALL}'.format(
383-
recipe=recipe, Fore=Fore, Style=Style,
375+
recipe=recipe, Fore=Out_Fore, Style=Out_Style,
384376
version=version))
385377
print(' {Fore.GREEN}depends: {recipe.depends}'
386-
'{Fore.RESET}'.format(recipe=recipe, Fore=Fore))
378+
'{Fore.RESET}'.format(recipe=recipe, Fore=Out_Fore))
387379
if recipe.conflicts:
388380
print(' {Fore.RED}conflicts: {recipe.conflicts}'
389381
'{Fore.RESET}'
390-
.format(recipe=recipe, Fore=Fore))
382+
.format(recipe=recipe, Fore=Out_Fore))
391383
if recipe.opt_depends:
392384
print(' {Fore.YELLOW}optional depends: '
393385
'{recipe.opt_depends}{Fore.RESET}'
394-
.format(recipe=recipe, Fore=Fore))
386+
.format(recipe=recipe, Fore=Out_Fore))
395387

396388
def bootstraps(self, args):
397389
'''List all the bootstraps available to build with.'''

0 commit comments

Comments
 (0)