-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbattstatt.rb
More file actions
executable file
·162 lines (145 loc) · 3.74 KB
/
Copy pathbattstatt.rb
File metadata and controls
executable file
·162 lines (145 loc) · 3.74 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env ruby -w
require 'optparse'
require 'rbconfig'
# Default options
options = {
verbose: false
}
# Parse command-line arguments
OptionParser.new do |opts|
opts.banner = "Usage: battstatt.rb [options]"
opts.on("-v", "--verbose", "Print detailed battery information") do |v|
options[:verbose] = v
end
opts.on("--version", "Show version information") do
puts "battstatt.rb 1.1.0"
puts "Ruby: #{RUBY_DESCRIPTION}"
puts "Path: #{RbConfig.ruby}"
exit
end
end.parse!
# Execute ioreg to get battery details
begin
ioreg_output = %x(/usr/sbin/ioreg -r -c AppleSmartBattery)
if ioreg_output.empty?
puts "Could not get battery information from ioreg."
exit 1
end
rescue
puts "Unsupported system or could not execute ioreg."
exit(1)
end
stats = {}
ioreg_output.scan(/"([^"]+)" = ([^\n]+)/).each do |match|
key = match[0]
value = match[1].strip
stats[key] = value
end
# Define keys for the default, non-verbose output
DEFAULT_KEYS = [
'CurrentCapacity',
'CycleCount',
'IsCharging',
'TimeRemaining',
'AvgTimeToFull',
'Amperage',
'Temperature'
]
# Keys to always skip, even in verbose mode
SKIP_KEYS = [
'AbsoluteCapacity',
'AppleRawAdapterDetails',
'PackReserve',
'CarrierMode',
'ChargerConfiguration',
'IOGeneralInterest',
'IOReportLegend',
'UpdateTime',
'BootVoltage',
'BatteryData',
'KioskMode',
'DeadBatteryBootData',
'ManufacturerData',
'FedDetails',
'FullPathUpdated',
'BatteryInvalidWakeSeconds',
'ChargerData',
'BootPathUpdated',
'PowerTelemetryData',
'PortControllerInfo',
'UserVisiblePathUpdated',
'IOReportLegendPublic',
'AdapterDetails',
'BatteryInstalled',
'Location',
'built-in'
]
puts
puts "-------------------------------"
puts " Battery Stats"
puts "-------------------------------"
puts
# Determine which keys to display based on the verbose flag
keys_to_display = options[:verbose] ? stats.keys.sort : DEFAULT_KEYS
stats.sort.to_h.each do |k, v|
next if SKIP_KEYS.include?(k)
next unless keys_to_display.include?(k)
display_v = ""
if ['Amperage', 'InstantAmperage'].include?(k)
amperage_val = v.to_i
if amperage_val > 2**63 - 1
amps = amperage_val - 2**64
else
amps = amperage_val
end
display_v = "#{amps} mA"
elsif k.include?('Voltage')
volts = v.to_f / 1000.0
display_v = "#{sprintf("%.3f", volts)} V"
elsif k.include?('Temperature')
celsius = v.to_f / 100.0
display_v = "#{sprintf("%.2f", celsius)} °C"
elsif k.include?('Capacity')
if ['CurrentCapacity', 'MaxCapacity', 'AbsoluteCapacity'].include?(k)
display_v = "#{v} %"
else
display_v = "#{v} mAh"
end
elsif k.end_with?('Seconds')
seconds = v.to_i
if seconds >= 60
display_v = "#{seconds / 60}m #{seconds % 60}s"
else
display_v = "#{seconds}s"
end
elsif ['TimeRemaining', 'AvgTimeToEmpty', 'AvgTimeToFull'].include?(k)
minutes = v.to_i
if minutes == 65535
display_v = "N/A"
elsif minutes > 0
hours = minutes / 60
remaining_minutes = minutes % 60
display_v = "#{hours}h #{remaining_minutes}m"
else
display_v = "0m"
end
else
display_v = v.gsub(/^"|"$/, '').gsub(/^{|}$/, '')
end
puts "#{k} => #{display_v}"
end
puts
# --- Summary Calculations ---
battery_data_str = stats['BatteryData']
design_capacity = nil
if battery_data_str
match = battery_data_str.match(/"DesignCapacity"=(\d+)/)
design_capacity = match[1].to_i if match
end
max_capacity = stats['NominalChargeCapacity']
if design_capacity && max_capacity
percent_of_original = (max_capacity.to_f / design_capacity.to_f) * 100
puts "Battery capacity is currently #{sprintf("%.2f", percent_of_original)}% of original."
else
puts "Could not determine battery capacity."
end