Skip to content
This repository has been archived by the owner on Feb 5, 2022. It is now read-only.

add cp2remote progress bar #101

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -193,6 +194,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);
});
}, function(err) {
Expand All @@ -214,6 +217,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);
});
});
Expand Down Expand Up @@ -247,7 +252,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;
Expand Down Expand Up @@ -277,7 +282,7 @@ Client.prototype.upload = function(src, dest, callback) {
destination: dest,
content: fd,
attrs: stat
}, callback);
}, callback, pFn);
}
], function(err) {
callback(err);
Expand Down
35 changes: 29 additions & 6 deletions lib/scp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -55,7 +57,7 @@ function cp2remote(client, src, dest, callback) {
if (err) {
callback(err);
} else {
_upload(files, callback);
_upload(files, callback, pFn);
}
});
} else {
Expand All @@ -68,7 +70,7 @@ function cp2remote(client, src, dest, callback) {
callback(err);
return;
}
_upload(files, callback);
_upload(files, callback, pFn);
});
}
}
Expand All @@ -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();
Expand All @@ -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}%`);
}
}
};