Class: IStats::Battery
- Inherits:
-
Object
- Object
- IStats::Battery
- Extended by:
- BATTERY_STATS
- Defined in:
- lib/iStats/battery.rb
Class Method Summary collapse
-
.all ⇒ Object
Call all functions (stats).
- .battery_charge ⇒ Object
-
.battery_health ⇒ Object
Get the battery health Calls a C method from BATTERY_STATS module.
-
.battery_temperature ⇒ Object
Get the battery temperature.
- .battery_time_remaining ⇒ Object
-
.cur_capacity ⇒ Object
Current capacity.
-
.cur_max_capacity ⇒ Object
Current max capacity.
-
.cycle_count ⇒ Object
Prints the battery cycle count info.
-
.delegate(stat) ⇒ Object
Delegate CLI command to function.
-
.design_cycle_count ⇒ Object
Get the battery design cycle count Calls a C method from BATTERY_STATS module.
-
.grep_ioreg(keyword) ⇒ Object
Get information from ioreg.
-
.ori_max_capacity ⇒ Object
Original max capacity.
-
.print_capacity_info ⇒ Object
Print battery capacity info.
-
.validate_battery ⇒ Object
Check if there’s a battery on the system.
Methods included from BATTERY_STATS
get_battery_charge, get_battery_design_cycle_count, get_battery_health, get_battery_temp, get_battery_time_remaining, has_battery?
Class Method Details
.all ⇒ Object
Call all functions (stats)
37 38 39 40 41 42 43 44 |
# File 'lib/iStats/battery.rb', line 37 def all return unless validate_battery battery_health cycle_count print_capacity_info battery_temperature end |
.battery_charge ⇒ Object
125 126 127 128 129 |
# File 'lib/iStats/battery.rb', line 125 def battery_charge charge = get_battery_charge result = charge ? "#{charge}%" : "Unknown" puts "Battery charge: #{result}" end |
.battery_health ⇒ Object
Get the battery health Calls a C method from BATTERY_STATS module
108 109 110 |
# File 'lib/iStats/battery.rb', line 108 def battery_health puts "Battery health: #{get_battery_health}" end |
.battery_temperature ⇒ Object
Get the battery temperature
101 102 103 |
# File 'lib/iStats/battery.rb', line 101 def battery_temperature puts "Battery temp: #{get_battery_temp.round(2)}#{Symbols.degree}C " end |
.battery_time_remaining ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/iStats/battery.rb', line 112 def battery_time_remaining time = get_battery_time_remaining if time.is_a? Integer hours = time / 3600 minutes = time / 60 - hours * 60 time = "%i:%02i" % [hours, minutes] end puts "Battery time remaining: #{time}" end |
.cur_capacity ⇒ Object
Current capacity
83 84 85 |
# File 'lib/iStats/battery.rb', line 83 def cur_capacity grep_ioreg("CurrentCapacity") end |
.cur_max_capacity ⇒ Object
Current max capacity
77 78 79 |
# File 'lib/iStats/battery.rb', line 77 def cur_max_capacity grep_ioreg("MaxCapacity") end |
.cycle_count ⇒ Object
Prints the battery cycle count info
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/iStats/battery.rb', line 48 def cycle_count @ioreg_out ||= %x( ioreg -rn AppleSmartBattery ) cycle_count = @ioreg_out[/"CycleCount" = ([0-9]*)/, 1] if cycle_count == nil puts "Cycle count: unknown" else max_cycle_count = design_cycle_count percentage = (cycle_count.to_f/max_cycle_count.to_f)*100 thresholds = [45, 65, 85, 95] puts "Cycle count: #{cycle_count} " + Printer.gen_sparkline(percentage, thresholds) + " #{percentage.round(1)}%" puts "Max cycle count: #{max_cycle_count}" end end |
.delegate(stat) ⇒ Object
Delegate CLI command to function
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/iStats/battery.rb', line 11 def delegate(stat) # Before we deletage check if there's a battery on system return unless validate_battery case stat when 'all' all when 'temp', 'temperature' battery_temperature when 'health' battery_health when 'cycle_count', 'cc' cycle_count when 'time', 'remain' battery_time_remaining when 'charge' battery_charge when 'capacity' print_capacity_info else Command.help "Unknown stat for Battery: #{stat}" end end |
.design_cycle_count ⇒ Object
Get the battery design cycle count Calls a C method from BATTERY_STATS module
134 135 136 |
# File 'lib/iStats/battery.rb', line 134 def design_cycle_count get_battery_design_cycle_count end |
.grep_ioreg(keyword) ⇒ Object
Get information from ioreg
64 65 66 67 |
# File 'lib/iStats/battery.rb', line 64 def grep_ioreg(keyword) @ioreg_out ||= %x( ioreg -rn AppleSmartBattery ) capacity = @ioreg_out[/"#{keyword}" = ([0-9]*)/, 1] end |
.ori_max_capacity ⇒ Object
Original max capacity
71 72 73 |
# File 'lib/iStats/battery.rb', line 71 def ori_max_capacity grep_ioreg("DesignCapacity") end |
.print_capacity_info ⇒ Object
Print battery capacity info
89 90 91 92 93 94 95 96 97 |
# File 'lib/iStats/battery.rb', line 89 def print_capacity_info percentage = (cur_max_capacity.to_f/ori_max_capacity.to_f)*100 thresholds = [45, 65, 85, 95] charge = get_battery_charge charge = charge ? " #{charge}%" : "" puts "Current charge: #{cur_capacity} mAh#{charge}" puts "Maximum charge: #{cur_max_capacity} mAh " + Printer.gen_sparkline(100-percentage, thresholds) + " #{percentage.round(1)}%" puts "Design capacity: #{ori_max_capacity} mAh" end |
.validate_battery ⇒ Object
Check if there’s a battery on the system
140 141 142 143 144 |
# File 'lib/iStats/battery.rb', line 140 def validate_battery valid = has_battery? puts 'No battery on system' unless valid valid end |