--- /dev/null
+#!/usr/bin/env python
+
+import itertools
+import subprocess
+import re
+import sys
+
+# Run ioreg to get the battery status line
+classes = ["AppleSmartBattery", "IOBatteryInfo"]
+cmd = ["ioreg", "-w0", "-r", "-c"]
+iterables = []
+for klass in classes:
+ p = subprocess.Popen(args=cmd + [klass], stdout=subprocess.PIPE)
+ iterables.append(p.stdout)
+
+lines = []
+for line in itertools.chain(*iterables):
+ if line.startswith("+"):
+ continue
+ line = line.strip()
+ if line.find("=") >= 0:
+ line = line.replace("Yes", "True").replace("No", "False")
+ line = line.replace("=", ":")
+ if line.find("<") >= 0:
+ line = line.replace("<", "'<").replace(">", ">'")
+ line += ","
+ lines.append(line)
+exec "data = " + "\n".join(lines)
+
+
+fields = [
+ "Current",
+ "Capacity",
+ "Capacity%",
+ "Cycle Count",
+
+ "CurrentCapacity",
+ "MaxCapacity",
+ "DesignCapacity",
+ "CycleCount",
+ "DesignCycleCount",
+]
+for field in fields:
+ if data.has_key(field):
+ print (field+":").ljust(3+max(map(len, fields))),
+ print data[field]
+
+
+++ /dev/null
-#!/usr/bin/env python
-
-import subprocess, re, sys
-
-# Run ioreg to get the battery status line
-p = subprocess.Popen(args=["ioreg", "-w0", "-l"], stdout=subprocess.PIPE)
-for line in p.stdout.xreadlines():
- if line.find("IOBatteryInfo") >= 0:
- p.stdout.close()
- break
-else:
- print "IOBatteryInfo not found!"
- sys.exit(1)
-
-
-# Parse the line to a dict & calculate the percentage
-line = line.strip()
-m = re.compile('^.*"IOBatteryInfo" = ').match(line)
-line = line[m.end()+1:-1].replace("=", ":")
-exec "fields = " + line
-fields["Capacity%"] = int(100.0 * float(fields["Capacity"]) / float(fields["AbsoluteMaxCapacity"]))
-
-
-# Print to screen
-for field in ("Current", "Capacity", "Capacity%", "Cycle Count"):
- print "%s:\t%d" % (field, fields[field])
-
-