From d255a1c2a9dad1607b2602d96070950fb33c4ada Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Tue, 25 Aug 2015 21:27:59 -0700 Subject: [PATCH] f*#k 250k from pretty-bytes --- lib/common.js | 18 +++++++++++++++++- lib/do_info.js | 10 ++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/common.js b/lib/common.js index 362f4af..7c6c5e9 100755 --- a/lib/common.js +++ b/lib/common.js @@ -173,6 +173,21 @@ function humanDurationFromMs(ms) { return bits.slice(0, 2).join(''); } +/** + * Adapted from + */ +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: diff --git a/lib/do_info.js b/lib/do_info.js index 94dadd6..7089806 100644 --- a/lib/do_info.js +++ b/lib/do_info.js @@ -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(); }