Skip to content

Instantly share code, notes, and snippets.

@amancevice
Last active February 12, 2018 21:45
Show Gist options
  • Save amancevice/48dbd26040e623ef4ccf5e9bae1ed97b to your computer and use it in GitHub Desktop.
Save amancevice/48dbd26040e623ef4ccf5e9bae1ed97b to your computer and use it in GitHub Desktop.
def print_docker_stream(stream):
""" Emulate docker pull/push output on stderr. """
# Initialize list of layers
layers = []
# Interate through stream
for line in stream:
# Print normal layer progress
try:
# Get layer ID
layer = line['id']
# Append to the list of layers
if layer not in layers:
layers.append(layer)
# Get the line number of the layer
lineno = layers.index(layer)
# Get the number of layers
maxlines = len(layers)
# Buffer the feed to get to the correct line
sys.stderr.write('\n' * lineno)
# Write the line
sys.stderr.write(
"{id}: {status} {progress}".format(
id=line.get('id'),
status=line.get('status'),
progress=line.get('progress') or '').ljust(100) + '\r')
# Pad the feed to the number of layers
sys.stderr.write('\n' * (maxlines - lineno))
# Reset the feed back to the top!
sys.stderr.write('\033[F' * maxlines)
# Print status message
except KeyError:
sys.stderr.write("{status}\n".format(status=line['status']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment