From ccfd3854aca549dea1b3e855ff4c9ed63b923041 Mon Sep 17 00:00:00 2001 From: t2krew Date: Wed, 11 Oct 2017 10:48:23 +0800 Subject: [PATCH 1/2] progress --- lib/client.js | 9 ++++++--- lib/scp.js | 35 +++++++++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/lib/client.js b/lib/client.js index 4af629d..7f1b51a 100644 --- a/lib/client.js +++ b/lib/client.js @@ -160,13 +160,14 @@ Client.prototype.mkdir = function(dir, attrs, callback) { }); }; -Client.prototype.write = function(options, callback) { +Client.prototype.write = function(options, callback, pFn) { var destination = options.destination; destination = unixy(destination); var attrs = options.attrs; var content = options.content; var chunkSize = options.chunkSize || 32768; + var totalSize = attrs.size; var self = this; @@ -214,6 +215,8 @@ Client.prototype.write = function(options, callback) { sftp.write(handle, buf, 0, buf.length, lastIndex, function(err) { lastIndex += buf.length; lastCursor += 1; + var progress = parseInt(lastIndex/totalSize*100); + pFn && pFn(progress); callback(err); }); }); @@ -247,7 +250,7 @@ Client.prototype.write = function(options, callback) { }); }; -Client.prototype.upload = function(src, dest, callback) { +Client.prototype.upload = function(src, dest, callback, pFn) { dest = unixy(dest); var self = this; @@ -277,7 +280,7 @@ Client.prototype.upload = function(src, dest, callback) { destination: dest, content: fd, attrs: stat - }, callback); + }, callback, pFn); } ], function(err) { callback(err); diff --git a/lib/scp.js b/lib/scp.js index 85e2675..c3f6ccf 100644 --- a/lib/scp.js +++ b/lib/scp.js @@ -5,10 +5,12 @@ var async = require('async'); var Client = require('./client').Client; var global_client = new Client(); -function cp2remote(client, src, dest, callback) { +const WIDTH = process.stdout.columns; + +function cp2remote(client, src, dest, callback, pFn) { client.parse(dest); - var _upload = function(files, callback) { + var _upload = function(files, callback, pFn) { var rootdir = files[0]; async.eachSeries(files, function(fpath, done) { @@ -20,7 +22,7 @@ function cp2remote(client, src, dest, callback) { if (stats.isFile()) { var fname = path.relative(rootdir, fpath); client.upload( - fpath, path.join(client.remote.path, fname), done + fpath, path.join(client.remote.path, fname), done, pFn.bind(null, files.length) ); } else { done(); @@ -55,7 +57,7 @@ function cp2remote(client, src, dest, callback) { if (err) { callback(err); } else { - _upload(files, callback); + _upload(files, callback, pFn); } }); } else { @@ -68,7 +70,7 @@ function cp2remote(client, src, dest, callback) { callback(err); return; } - _upload(files, callback); + _upload(files, callback, pFn); }); } } @@ -92,6 +94,9 @@ exports = module.exports = global_client; exports.Client = Client; exports.scp = function(src, dest, client, callback) { + let c = 0; + let t = 0; + if (typeof client === 'function') { callback = client; client = new Client(); @@ -101,6 +106,24 @@ exports.scp = function(src, dest, client, callback) { if (parsed.host && parsed.path) { cp2local(client, parsed, dest, callback); } else { - cp2remote(client, src, dest, callback); + cp2remote(client, src, dest, callback, progressFn); + } + + function progressFn(len, p) { + let str = ''; + if (p >= 100) { + c++; + } + t = c * 100 + p; + let l = parseInt(WIDTH * (t / (len * 100))) - 5; + let progress = parseInt(t / (len * 100) * 100); + l = l > 0 ? l : 0; + for(let i = 0; i < l; i++) { + str += '.'; + process.stdout.cursorTo(0); + process.stdout.write(str); + process.stdout.cursorTo(WIDTH - 5); + process.stdout.write(` ${progress}%`); + } } }; From a47e60379e2658c0894a6bec15cde0d54a5f39cf Mon Sep 17 00:00:00 2001 From: t2krew Date: Wed, 11 Oct 2017 11:16:50 +0800 Subject: [PATCH 2/2] add cp2remote progress bar --- lib/client.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/client.js b/lib/client.js index 7f1b51a..28609e9 100644 --- a/lib/client.js +++ b/lib/client.js @@ -194,6 +194,8 @@ Client.prototype.write = function(options, callback, pFn) { sftp.write(handle, buf, 0, buf.length, lastIndex, function(err) { lastIndex += buf.length; lastCursor += 1; + var progress = parseInt(lastIndex/totalSize*100); + pFn && pFn(progress); callback(err); }); }, function(err) {