f*#k 250k from pretty-bytes

This commit is contained in:
Trent Mick 2015-08-25 21:27:59 -07:00
parent 788d5ff31b
commit d255a1c2a9
2 changed files with 23 additions and 5 deletions

View File

@ -173,6 +173,21 @@ function humanDurationFromMs(ms) {
return bits.slice(0, 2).join('');
}
/**
* Adapted from <http://stackoverflow.com/a/18650828>
*/
function humanSizeFromBytes(bytes) {
assert.number(bytes, 'bytes');
var sizes = ['B', 'KiB', 'MiB', 'GiB', 'TiB'];
if (bytes === 0) {
return '0 B';
}
var i = Number(Math.floor(Math.log(bytes) / Math.log(1024)));
var s = String(bytes / Math.pow(1024, i));
var precision1 = (s.indexOf('.') === -1
? s + '.0' : s.slice(0, s.indexOf('.') + 2));
return format('%s %s', precision1, sizes[i]);
}
//---- exports
@ -187,6 +202,7 @@ module.exports = {
kvToObj: kvToObj,
longAgo: longAgo,
isUUID: isUUID,
humanDurationFromMs: humanDurationFromMs
humanDurationFromMs: humanDurationFromMs,
humanSizeFromBytes: humanSizeFromBytes
};
// vim: set softtabstop=4 shiftwidth=4:

View File

@ -4,10 +4,10 @@
* `triton info ...`
*/
var assert = require('assert-plus');
var common = require('./common');
var f = require('util').format;
var format = require('util').format;
var prettybytes = require('pretty-bytes');
function do_info(subcmd, opts, args, callback) {
var self = this;
@ -62,8 +62,10 @@ function do_info(subcmd, opts, args, callback) {
Object.keys(states).forEach(function (state) {
console.log('- %d %s', states[state], state);
});
console.log('- %s RAM Total', prettybytes(memory * 1000 * 1000));
console.log('- %s Disk Total', prettybytes(disk * 1000 * 1000));
console.log('- %s RAM Total',
common.humanSizeFromBytes(memory * 1000 * 1000));
console.log('- %s Disk Total',
common.humanSizeFromBytes(disk * 1000 * 1000));
}
callback();
}