Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect macOS disk usage report #14

Open
wants to merge 4 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
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock = false
File renamed without changes.
4 changes: 2 additions & 2 deletions bin/archeyjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function out(distro, item) {
'Fedora': colors.blueB,
'CrunchBang': colors.whiteB,
'LinuxMint': colors.greenB,
'OS X': colors.whiteB,
'macOS': colors.whiteB,
'Windows': colors.whiteB
};
return cols[distro] + item.key + ': ' + colors.clear + item.value;
Expand All @@ -64,7 +64,7 @@ var distroArt = {
'ManjaroLinux': '../art/manjaro.js',
'Arch': '../art/arch.js',
'FreeBSD': '../art/freebsd.js',
'OS X': '../art/osx.js',
'macOS': '../art/macOS.js',
'Windows': '../art/windows.js',
};

Expand Down
40 changes: 26 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
var exec = require('child_process').exec,
os = require('os'),
bytes = require('bytes'),
bytes = require('./lib/bytes'),
colors = require('./art/colors.js'),
elapsed = require('./lib/elapsed.js'),
deDict = require('./lib/de-dict.json'),
wmDict = require('./lib/wm-dict.json');

function color(used, total) {
function color(used, total, base) {
var quadrant = Math.min(Math.ceil( Math.floor(used / total * 100) / 33), 3),
cols = [ colors.greenB, colors.greenB, colors.yellowB, colors.redB ];
return cols[quadrant] + bytes(used) + colors.clear + ' / ' + bytes(total);
return cols[quadrant] + bytes(used, base) + colors.clear + ' / ' + bytes(total, base);
}

var result = {
Expand Down Expand Up @@ -53,7 +53,7 @@ var tasks = [
done();
});
break;
case 'OS X':
case 'macOS':
exec('sw_vers', function(err, stdout, stderr) {
var items = { };
stdout.trim().split('\n').forEach(function(line) {
Expand Down Expand Up @@ -146,7 +146,7 @@ var tasks = [
done();
});
break;
case 'OS X':
case 'macOS':
// port
exec('port installed 2>/dev/null | wc -l', function(err, stdout, stderr) {
var packages = 0;
Expand Down Expand Up @@ -195,15 +195,27 @@ var tasks = [
switch(os.platform()) {
case 'freebsd':
case 'darwin':
// OSX df is weird, it doesn't have a good option for getting bytes...
exec('df -k / 2>/dev/null | tail -1', function(err, stdout, stderr) {
var lines = stdout.trim().split('\n').pop(),
used = parseInt(lines.split(/\s+/)[2], 10) * 1024,
free = parseInt(lines.split(/\s+/)[3], 10) * 1024,
total = used + free;
result.disk = { key: 'Disk', value: color( used, total) };
(function (parse) {
if(distro == 'FreeBSD') {
return exec('df -kc /', parse);
}
exec('sw_vers -productVersion', function(err, stdout, stderr) {
var version = stdout.trim(),
minorVersion = parseInt(version.split('.')[1], 10);
if(minorVersion >= 15) {
exec('df -k /System/Volumes/Data', parse);
} else {
exec('df -k /', parse);
}
});
}(function(err, stdout, stderr) {
var line = stdout.trim().split(/\n/).pop(),
cols = line.split(/\s+/),
total = parseInt(cols[1], 10) * 1024,
used = parseInt(cols[2], 10) * 1024;
result.disk = { key: 'Disk', value: color(used, total, 1000) };
done();
});
}));
break;
case 'win32':
case 'win64':
Expand Down Expand Up @@ -264,7 +276,7 @@ module.exports = function(onDone) {
switch(os.platform()) {
case 'darwin':
processes = [];
distro = 'OS X';
distro = 'macOS';
result.wm = { key: 'Window Manager', value: 'Quartz Compositor' };
fullParallel(tasks, onDone);
break;
Expand Down
12 changes: 12 additions & 0 deletions lib/bytes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = humanSize;

var mags = ' KMGTPEZY';

function humanSize(bytes, base, precision) {
if (base === undefined) base = 1024;
if (precision === undefined) precision = 2;
var magnitude = Math.min(Math.log(bytes) / Math.log(base) | 0, mags.length - 1);
var result = bytes / Math.pow(base, magnitude);
var suffix = mags[magnitude].trim() + 'B';
return result.toFixed(precision) + suffix;
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"url": "https://github.com/mixu/archey.js/issues"
},
"dependencies": {
"bytes": "~3.0.0",
"optimist": "~0.6.0"
}
}