Skip to content
This repository was archived by the owner on Oct 3, 2020. It is now read-only.

Commit cd3dbfd

Browse files
committed
Add Action optional ok_msg and nl
1 parent a8502eb commit cd3dbfd

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

clickclick/console.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,18 @@ def __exit__(self, exc_type, exc_val, exc_tb):
8282

8383
class Action:
8484

85-
def __init__(self, msg, **kwargs):
85+
def __init__(self, msg, ok_msg=' OK', nl=False, **kwargs):
8686
self.msg = msg
87+
self.ok_msg = ok_msg
8788
self.msg_args = kwargs
89+
self.nl = nl
8890
self.errors = []
8991
self._suppress_exception = False
90-
self.ok_msg = ' OK'
9192

9293
def __enter__(self):
9394
action(self.msg, **self.msg_args)
95+
if self.nl:
96+
secho('')
9497
return self
9598

9699
def __exit__(self, exc_type, exc_val, exc_tb):
@@ -134,7 +137,7 @@ def format_time(ts):
134137
diff = now - dt
135138
s = diff.total_seconds()
136139
if s > (3600 * 49):
137-
t = '{:.0f}d'.format(s / (3600*24))
140+
t = '{:.0f}d'.format(s / (3600 * 24))
138141
elif s > 3600:
139142
t = '{:.0f}h'.format(s / 3600)
140143
elif s > 70:
@@ -208,7 +211,7 @@ def print_table(cols, rows, styles=None, titles=None, max_column_widths=None):
208211
for i, col in enumerate(cols):
209212
click.secho(('{:' + str(colwidths[col]) + '}').format(titles.get(col, col.title().replace('_', ' '))),
210213
nl=False, fg='black', bg='white')
211-
if i < len(cols)-1:
214+
if i < len(cols) - 1:
212215
click.secho('│', nl=False, fg='black', bg='white')
213216
click.echo('')
214217

@@ -256,12 +259,12 @@ def choice(prompt: str, options: list, default=None):
256259
value = label = option
257260
if value == default:
258261
promptdefault = i + 1
259-
click.secho('{}) {}'.format(i+1, label), err=stderr)
262+
click.secho('{}) {}'.format(i + 1, label), err=stderr)
260263
while True:
261264
selection = click.prompt('Please select (1-{})'.format(len(options)),
262265
type=int, default=promptdefault, err=stderr)
263266
try:
264-
result = options[int(selection)-1]
267+
result = options[int(selection) - 1]
265268
if isinstance(result, tuple):
266269
value, label = result
267270
else:
@@ -275,6 +278,7 @@ class AliasedGroup(click.Group):
275278
"""
276279
Click group which allows using abbreviated commands
277280
"""
281+
278282
def get_command(self, ctx, cmd_name):
279283
rv = click.Group.get_command(self, ctx, cmd_name)
280284
if rv is not None:

setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ def read_version(package):
5252

5353
class PyTest(TestCommand):
5454

55-
user_options = [('cov=', None, 'Run coverage'), ('cov-xml=', None, 'Generate junit xml report'), ('cov-html=',
56-
None, 'Generate junit html report'), ('junitxml=', None, 'Generate xml of test results')]
55+
user_options = [('cov=', None, 'Run coverage'), ('cov-xml=', None, 'Generate junit xml report'),
56+
('cov-html=', None, 'Generate junit html report'),
57+
('junitxml=', None, 'Generate xml of test results')]
5758

5859
def initialize_options(self):
5960
TestCommand.initialize_options(self)

tests/test_console.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ def test_action():
4545
with Action('Perform and progress..') as act:
4646
act.progress()
4747

48+
with Action('Perform, progress and done', ok_msg='DONE') as act:
49+
act.progress()
50+
51+
with Action('Perform action new line', nl=True):
52+
print('In new line!')
53+
4854
with pytest.raises(SystemExit):
4955
with Action('Try and fail badly..') as act:
5056
act.fatal_error('failing..')

0 commit comments

Comments
 (0)