Skip to content

Instantly share code, notes, and snippets.

@alexandregz
Last active January 11, 2016 15:49
Show Gist options
  • Save alexandregz/ea292941c641b09dfbd3 to your computer and use it in GitHub Desktop.
Save alexandregz/ea292941c641b09dfbd3 to your computer and use it in GitHub Desktop.
bitbar plugin using system_profiler command
#!/usr/bin/env perl
# <bitbar.title>Battery Apple Bluetooth keyboard</bitbar.title>
# <bitbar.version>1.0</bitbar.version>
# <bitbar.author>Alexandre Espinosa Menor</bitbar.author>
# <bitbar.author.github>alexandregz</bitbar.author.github>
# <bitbar.desc>Show vagrant images running, from vagrant global-status command</bitbar.desc>
#
# command from https://github.com/matryer/bitbar-plugins/issues/84 by @keithamus
#
use strict;
my $output = `system_profiler SPBluetoothDataType`;
if($output =~ /Minor Type: Keyboard.*Battery Level: (\d+)/sm) {
print "Keyboard: $1%";
}
@airblade
Copy link

The script needs a ? after the .* to make it a non-greedy match. I.e. the regex should be:

/Minor Type: Keyboard.*?Battery Level: (\d+)/sm

Without the ? the regex matches the battery level of the mouse, which is listed after the keyboard.

@alexandregz
Copy link
Author

Fine, thx. I had the mouse before the keyboard O:-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment