Posted By

pham on 01/13/08


Tagged

Shell mac osx perl terminal battery laptop


Versions (?)

battery


 / Published in: Perl
 

URL: http://blog.seven29.com/post/22930432

battery is a Perl script that concisely displays your battery status.

  1. #!/usr/bin/perl
  2.  
  3. # battery.pl
  4. #
  5. # Perl script that displays to the terminal the charge on your Mac Intel battery (and takes
  6. # a guess as to how much time is left on the charge).
  7. #
  8. # Version 1.0 (010308) - Hey, it works!
  9. # 1.1 (010408) - Added debugging code; added IsCharging test; changed symbols for TimeLeft.
  10. # 1.2 (011208) - "Fixed" some weirdness with the battery being at less than 100% capacity,
  11. # but still reading "FullyCharged" or reading "FullyCharged" while also
  12. # reading "IsCharging." Wacky.
  13. #
  14. # Author: P. Ham - [email protected]
  15. #
  16. # Cryptic Notes: 65535 may just be a kludge number to mean "infinity." See http://en.wikipedia.org/wiki/65535_(number).
  17.  
  18. use strict;
  19. use Data::Dumper;
  20. my $IOREG = "/usr/sbin/ioreg";
  21.  
  22. my $output = `$IOREG -nAppleSmartBattery`;
  23. my @output = split("\n", $output);
  24. my $batteryHash;
  25.  
  26. foreach (@output) {
  27. if (/\"(MaxCapacity)\"\s+=\s+(\d+)/) {
  28. $batteryHash->{$1} = $2;
  29. } elsif (/\"(DesignCapacity)\"\s+=\s+(\d+)/) {
  30. $batteryHash->{$1} = $2;
  31. } elsif (/\"(CurrentCapacity)\"\s+=\s+(\d+)/) {
  32. $batteryHash->{$1} = $2;
  33. } elsif (/\"(AvgTimeToEmpty)\"\s+=\s+(\d+)/) {
  34. $batteryHash->{$1} = $2;
  35. } elsif (/\"(AvgTimeToFill)\"\s+=\s+(\d+)/) {
  36. $batteryHash->{$1} = $2;
  37. } elsif (/\"(TimeRemaining)\"\s+=\s+(\d+)/) {
  38. $batteryHash->{$1} = $2;
  39. } elsif (/\"(IsCharging)\"\s+=\s+(\w+)/) {
  40. $batteryHash->{$1} = $2;
  41. } elsif (/\"(FullyCharged)\"\s+=\s+(\w+)/) {
  42. $batteryHash->{$1} = $2;
  43. }
  44. }
  45.  
  46. my $DEBUG = 0;
  47. print Dumper $batteryHash if $DEBUG;
  48.  
  49. my $percentCapacity = ( $batteryHash->{"CurrentCapacity"} / $batteryHash->{"MaxCapacity"} ) * 100;
  50. $percentCapacity = int($percentCapacity + .5 * ($percentCapacity <=> 0));
  51.  
  52. my $timeLeft;
  53. if ($batteryHash->{"IsCharging"} eq "Yes" && $batteryHash->{"FullyCharged"} eq "No") {
  54. # battery is charging so can't calculate time left (yet)
  55. $timeLeft = "( + )";
  56. # $timeLeft = "(~/~)";
  57. } elsif ($batteryHash->{"FullyCharged"} eq "Yes") {
  58. $timeLeft = "( f )";
  59. } elsif ($percentCapacity != 100) {
  60. # OK, calculate time left
  61. $timeLeft = $batteryHash->{"TimeRemaining"} / 60;
  62. my ($hours, $minutes) = split(/\./, $timeLeft);
  63. $minutes = 60 * ( "." . $minutes );
  64. $minutes = int($minutes + .005 * ($minutes <=> 0));
  65. $minutes = sprintf ("%02d", $minutes);
  66. $timeLeft = "($hours:$minutes)";
  67. } else {
  68. # not charging and 100% capacity
  69. # $timeLeft = "( - )";
  70. $timeLeft = "( f )";
  71. }
  72.  
  73. print "battery: $percentCapacity% $timeLeft\n";
  74.  

Report this snippet  

You need to login to post a comment.