using existing format-file-size; fixing nits with progress output

This commit is contained in:
Mike Gray
2016-04-22 16:04:52 -07:00
parent 432ab69598
commit cbe97a054a
4 changed files with 67 additions and 18 deletions

View File

@@ -22,7 +22,9 @@
"babel-plugin-transform-runtime": "^6.6.0",
"babel-preset-es2015": "6.6.0",
"babel-preset-react": "6.5.0",
"flow-bin": "^0.23.0"
"flow-bin": "^0.23.0",
"chai": "3.5.0",
"mocha": "2.4.5"
},
"scripts": {
"start": "babel -d dist -w src",

View File

@@ -0,0 +1,46 @@
// @flow
import {assert} from 'chai';
import {suite, test} from 'mocha';
import formatFileSize from './format-file-size.js';
suite('spread-sheet-number', () => {
function doTest(n, s) {
test(s, () => {
assert.equal(formatFileSize(n), s);
});
}
doTest(0, '0B');
doTest(1, '1B');
doTest(2, '2B');
doTest(123, '123B');
doTest(1023, '1023B');
doTest(1024, '1KB');
doTest(1025, '1KB');
doTest(1.5 * 1024, '1.5KB');
doTest(1.2345 * 1024, '1.2KB');
const K = 1024;
const M = 1024 * K;
const G = 1024 * M;
const T = 1024 * G;
const P = 1024 * T;
doTest(.95 * M, '972.8KB');
doTest(M, '1MB');
doTest(1.23 * M, '1.2MB');
doTest(.95 * G, '972.8MB');
doTest(G, '1GB');
doTest(1.23 * G, '1.2GB');
doTest(.95 * T, '972.8GB');
doTest(T, '1TB');
doTest(1.23 * T, '1.2TB');
doTest(.95 * P, '972.8TB');
doTest(P, '1PB');
doTest(1.23 * P, '1.2PB');
});

View File

@@ -0,0 +1,14 @@
// @flow
const units = ['', 'K', 'M', 'G', 'T', 'P'];
export default function formatFileSize(n: number): string {
if (n < 1) {
return n + 'B';
}
const exp = Math.min(Math.floor(Math.log(n) / Math.log(1024)), units.length - 1);
n = Number((n / Math.pow(1024, exp)).toFixed(1));
return n + units[exp] + 'B';
}

View File

@@ -3,6 +3,7 @@
import fs from 'mz/fs';
import path from 'path';
import argv from 'yargs';
import formatFileSize from './format-file-size.js';
import {
newMapOfStringToRefOfDirectoryEntry,
Directory,
@@ -125,24 +126,10 @@ function processBlob(p: string, store: DataStore): Promise<RefValue<NomsBlob>> {
});
}
function humanFileSize(bytes, si) {
const thresh = si ? 1000 : 1024;
if (Math.abs(bytes) < thresh) {
return bytes + ' B';
}
const units = si
? ['kB','MB','GB','TB','PB','EB','ZB','YB']
: ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'];
let u = -1;
do {
bytes /= thresh;
++u;
} while (Math.abs(bytes) >= thresh && u < units.length - 1);
return bytes.toFixed(1) + ' ' + units[u];
}
function updateProgress() {
process.stdout.write(`\r${numFilesComplete} of ${numFilesFound} entries processed... ${humanFileSize(sizeFilesComplete,true)} of ${humanFileSize(sizeFilesFound,true)} contents processed`); // eslint-disable-line max-len
process.stdout.write(`\r${numFilesComplete} of ${numFilesFound} entries \
(${formatFileSize(sizeFilesComplete)} of ${formatFileSize(sizeFilesFound)}) \
processed...`);
}
function parseArgs() {