From 369b3c0f3f69b5cb32acecd72ca7baab4316aa3d Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Sun, 21 Jan 2018 14:57:18 +0100 Subject: [PATCH] optimized OSX battery --- CHANGELOG.md | 1 + lib/battery.js | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ab3ff3..2774a83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -99,6 +99,7 @@ Other changes | Version | Date | Comment | | -------------- | -------------- | -------- | +| 3.33.15 | 2018-01-21 | optimized OSX battery | | 3.33.14 | 2018-01-17 | bugfix `diskLayout()` (Windows) | | 3.33.13 | 2018-01-12 | bugfix `memLayout()` (Windows) | | 3.33.12 | 2017-12-25 | fixed typos | diff --git a/lib/battery.js b/lib/battery.js index 6ccc7d8..7f70d19 100644 --- a/lib/battery.js +++ b/lib/battery.js @@ -80,7 +80,7 @@ module.exports = function (callback) { } } if (_darwin) { - exec("ioreg -n AppleSmartBattery -r | grep '\"CycleCount\"';ioreg -n AppleSmartBattery -r | grep '\"IsCharging\"';ioreg -n AppleSmartBattery -r | grep '\"MaxCapacity\"';ioreg -n AppleSmartBattery -r | grep '\"CurrentCapacity\"'", function (error, stdout) { + exec("ioreg -n AppleSmartBattery -r | grep '\"CycleCount\"';ioreg -n AppleSmartBattery -r | grep '\"IsCharging\"';ioreg -n AppleSmartBattery -r | grep '\"MaxCapacity\"';ioreg -n AppleSmartBattery -r | grep '\"CurrentCapacity\"';pmset -g batt | grep %", function (error, stdout) { if (!error) { let lines = stdout.toString().replace(/ +/g, '').replace(/"+/g, '').split('\n'); lines.forEach(function (line) { @@ -88,7 +88,15 @@ module.exports = function (callback) { if (line.toLowerCase().indexOf('cyclecount') !== -1) result.cyclecount = parseFloat(line.split('=')[1].trim()); if (line.toLowerCase().indexOf('ischarging') !== -1) result.ischarging = (line.split('=')[1].trim().toLowerCase() === 'yes'); if (line.toLowerCase().indexOf('maxcapacity') !== -1) result.maxcapacity = parseFloat(line.split('=')[1].trim()); - if (line.toLowerCase().indexOf('currentcapacity') !== -1) result.currentcapacity = parseFloat(line.split('=')[1].trim()); + if (line.toLowerCase().indexOf('internalbattery') !== -1) { + let parts = line.split(';'); + if (parts && parts[0]) { + let parts2 = parts[0].split('\t'); + if (parts2 && parts2[1]) { + result.percent = parseFloat(parts2[1].trim().replace('%', '')); + } + } + } } }); }