Skip to content

Commit fd4bfe8

Browse files
committed
Refactor and add multiline output control
Refactor the frequency based output to a helper method. Add in a parameter to control if progress output to a single line.
1 parent 2366b20 commit fd4bfe8

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

awscli/customizations/s3/results.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ class ResultPrinter(BaseResultHandler):
316316
SRC_TRANSFER_LOCATION_FORMAT = '{src}'
317317

318318
def __init__(
319-
self, result_recorder, out_file=None, error_file=None, frequency=0
319+
self, result_recorder, out_file=None, error_file=None, frequency=0, oneline=True
320320
):
321321
"""Prints status of ongoing transfer
322322
@@ -352,19 +352,30 @@ def __init__(
352352
FinalTotalSubmissionsResult: self._clear_progress_if_no_more_expected_transfers,
353353
}
354354
self._now = time.time()
355+
self._oneline = oneline
355356

356357
def __call__(self, result):
357358
"""Print the progress of the ongoing transfer based on a result"""
359+
result_handler = self._result_handler_map.get(type(result), self._print_noop)
360+
if type(result) is ProgressResult:
361+
result_handler = self._override_progress_result_handler(
362+
result, result_handler
363+
)
364+
result_handler(result=result)
365+
366+
def _override_progress_result_handler(self, result, result_handler):
358367
if (
359-
self._first
360-
or (self._frequency == 0)
361-
or (time.time() - self._now >= self._frequency)
362-
):
363-
self._result_handler_map.get(type(result), self._print_noop)(
364-
result=result
368+
type(result) in [ProgressResult]
369+
and (
370+
self._first
371+
or (self._frequency == 0)
372+
or (time.time() - self._now >= self._frequency)
365373
)
374+
):
366375
self._now = time.time()
367376
self._first = False
377+
return result_handler
378+
return self._print_noop
368379

369380
def _print_noop(self, **kwargs):
370381
# If the result does not have a handler, then do nothing with it.
@@ -475,15 +486,19 @@ def _print_progress(self, **kwargs):
475486
if not self._result_recorder.expected_totals_are_final():
476487
progress_statement += self._STILL_CALCULATING_TOTALS
477488

478-
# Make sure that it overrides any previous progress bar.
479-
progress_statement = self._adjust_statement_padding(
480-
progress_statement, ending_char='\r'
481-
)
482-
# We do not want to include the carriage return in this calculation
483-
# as progress length is used for determining whitespace padding.
484-
# So we subtract one off of the length.
485-
self._progress_length = len(progress_statement) - 1
486-
489+
if self._oneline:
490+
# Make sure that it overrides any previous progress bar.
491+
progress_statement = self._adjust_statement_padding(
492+
progress_statement, ending_char='\r'
493+
)
494+
# We do not want to include the carriage return in this calculation
495+
# as progress length is used for determining whitespace padding.
496+
# So we subtract one off of the length.
497+
self._progress_length = len(progress_statement) - 1
498+
else:
499+
progress_statement = self._adjust_statement_padding(
500+
progress_statement, ending_char='\n'
501+
)
487502
# Print the progress out.
488503
self._print_to_out_file(progress_statement)
489504

awscli/customizations/s3/s3handler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ def _add_result_printer(self, result_recorder, result_processor_handlers):
107107
result_printer = ResultPrinter(
108108
result_recorder,
109109
frequency=self._cli_params.get('progress_frequency'),
110+
oneline=not self._cli_params.get('progress_multiline'),
110111
)
111112
result_processor_handlers.append(result_printer)
112113

awscli/customizations/s3/subcommands.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,15 @@
539539
),
540540
}
541541

542+
PROGRESS_MULTILINE = {
543+
'name': 'progress-multiline',
544+
'dest': 'progress_multiline',
545+
'action': 'store_true',
546+
'help_text': (
547+
'Show progress on multiple lines.'
548+
),
549+
}
550+
542551

543552
EXPECTED_SIZE = {
544553
'name': 'expected-size',
@@ -683,6 +692,7 @@
683692
ONLY_SHOW_ERRORS,
684693
NO_PROGRESS,
685694
PROGRESS_FREQUENCY,
695+
PROGRESS_MULTILINE,
686696
PAGE_SIZE,
687697
IGNORE_GLACIER_WARNINGS,
688698
FORCE_GLACIER_TRANSFER,

0 commit comments

Comments
 (0)