From fc5640c79dd2b990be59af85a0cb9d18a56ef4c6 Mon Sep 17 00:00:00 2001 From: Akis Kesoglou Date: Wed, 10 May 2017 22:00:51 +0300 Subject: [PATCH] Draw a progress bar for upload progress Change-Type: patch --- build/actions/deploy.js | 19 ++++++++++++++++--- lib/actions/deploy.coffee | 14 ++++++++++++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/build/actions/deploy.js b/build/actions/deploy.js index 360a99a1..77117ba4 100644 --- a/build/actions/deploy.js +++ b/build/actions/deploy.js @@ -1,5 +1,5 @@ // Generated by CoffeeScript 1.12.5 -var Promise, dockerUtils, formatImageName, getBuilderLogPushEndpoint, getBuilderPushEndpoint, getBundleInfo, parseInput, performUpload, pushProgress, uploadLogs, uploadToPromise; +var Promise, dockerUtils, formatImageName, getBuilderLogPushEndpoint, getBuilderPushEndpoint, getBundleInfo, parseInput, performUpload, pushProgress, renderProgress, uploadLogs, uploadToPromise; Promise = require('bluebird'); @@ -51,6 +51,19 @@ parseInput = Promise.method(function(params, options) { return [appName, options.build, source, image]; }); +renderProgress = function(percentage, stepCount) { + var _, bar, barCount, spaceCount; + if (stepCount == null) { + stepCount = 50; + } + _ = require('lodash'); + percentage = Math.max(0, Math.min(percentage, 100)); + barCount = Math.floor(stepCount * percentage / 100); + spaceCount = stepCount - barCount; + bar = "[" + (_.repeat('=', barCount)) + ">" + (_.repeat(' ', spaceCount)) + "]"; + return bar + " " + (percentage.toFixed(1)) + "%"; +}; + pushProgress = function(imageSize, request, logStreams, timeout) { var ansiEscapes, logging, progressReporter; if (timeout == null) { @@ -58,7 +71,7 @@ pushProgress = function(imageSize, request, logStreams, timeout) { } logging = require('../utils/logging'); ansiEscapes = require('ansi-escapes'); - logging.logInfo(logStreams, 'Initialising...'); + logging.logInfo(logStreams, 'Initializing...'); return progressReporter = setInterval(function() { var percent, sent; sent = request.req.connection._bytesDispatched; @@ -70,7 +83,7 @@ pushProgress = function(imageSize, request, logStreams, timeout) { process.stdout.write(ansiEscapes.cursorUp(1)); process.stdout.clearLine(); process.stdout.cursorTo(0); - return logging.logInfo(logStreams, "Uploaded " + (percent.toFixed(1)) + "%"); + return logging.logInfo(logStreams, renderProgress(percent)); }, timeout); }; diff --git a/lib/actions/deploy.coffee b/lib/actions/deploy.coffee index 14fd7ede..9530d735 100644 --- a/lib/actions/deploy.coffee +++ b/lib/actions/deploy.coffee @@ -31,11 +31,21 @@ parseInput = Promise.method (params, options) -> return [appName, options.build, source, image] +# Builds and returns a Docker-like progress bar like this: +# [==================================> ] 64% +renderProgress = (percentage, stepCount = 50) -> + _ = require('lodash') + percentage = Math.max(0, Math.min(percentage, 100)) + barCount = stepCount * percentage // 100 + spaceCount = stepCount - barCount + bar = "[#{_.repeat('=', barCount)}>#{_.repeat(' ', spaceCount)}]" + return "#{bar} #{percentage.toFixed(1)}%" + pushProgress = (imageSize, request, logStreams, timeout = 250) -> logging = require('../utils/logging') ansiEscapes = require('ansi-escapes') - logging.logInfo(logStreams, 'Initialising...') + logging.logInfo(logStreams, 'Initializing...') progressReporter = setInterval -> sent = request.req.connection._bytesDispatched percent = (sent / imageSize) * 100 @@ -45,7 +55,7 @@ pushProgress = (imageSize, request, logStreams, timeout = 250) -> process.stdout.write(ansiEscapes.cursorUp(1)) process.stdout.clearLine() process.stdout.cursorTo(0) - logging.logInfo(logStreams, "Uploaded #{percent.toFixed(1)}%") + logging.logInfo(logStreams, renderProgress(percent)) , timeout getBundleInfo = (options) ->