-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatteryStatus.rb
executable file
·93 lines (75 loc) · 2.19 KB
/
batteryStatus.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env ruby
$hasColor = true
begin
require 'smart_colored'
rescue LoadError
$hasColor = false
end
#require 'pry'
def safeToI(val)
if (val == "")
return -1
else
return val.to_i
end
end
def mouseBattery
battery = `ioreg -c BNBMouseDevice | grep Percent | tail -1| sed 's/.* = //'`
return safeToI(battery)
end
def keyboardBattery
battery = `ioreg -c AppleBluetoothHIDKeyboard | grep BatteryPercent | tail -1| sed 's/.* = //'`
return safeToI(battery)
end
def trackpadBattery
battery = `ioreg -c BNBTrackpadDevice | grep BatteryPercent | tail -1| sed 's/.* = //'`
return safeToI(battery)
end
def systemBattery
systemAmounts = `ioreg -n AppleSmartBattery | grep Capacity | head -2`;
## Pull out each of the two lines, 1: maxCapacity, 2: currentCapacity
values = systemAmounts.split("\n").map do |line|
line.gsub(/^.*\s+=\s+/, '')
end
systemBatteryCapacity = -1
if (values.length >= 2)
systemBatteryCapacity = (values[1].to_f/values[0].to_f)*100
end
#binding.pry
return systemBatteryCapacity
end
def formatOutput(level)
# if (level < 0)
# return "Disconnected".colored.black.bold
# elsif (level < 33)
# return " #{level}%".colored.red
# elsif (level < 66)
# return " #{level}%".colored.yellow
# else
# return " #{level}%".colored.green
# end
if (level > 99) then level = 99; end
lev = "%02d%% " % level
if !$hasColor
if level < 0
return "Disconnected"
else
return lev
end
end
if (level < 0)
return "Disconnected".colored.black.bold
elsif (level < 33)
return lev.to_s.colored.red
elsif (level < 66)
return lev.to_s.colored.yellow
else
return lev.to_s.colored.green
end
end
# puts "System Battery: #{formatOutput(systemBattery)}"
# puts "Trackpad Battery: #{formatOutput(trackpadBattery)}"
# puts "Mouse Battery: #{formatOutput(mouseBattery)}"
# puts "Keyboard Battery: #{formatOutput(keyboardBattery)}"
puts "System Battery: #{formatOutput(systemBattery)} Trackpad Battery: #{formatOutput(trackpadBattery)}"
puts "Mouse Battery: #{formatOutput(mouseBattery)} Keyboard Battery: #{formatOutput(keyboardBattery)}"