public
Created

Battery indicator for Xmobar

  • Download Gist
bat.rb
Ruby
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
# encoding: utf-8
 
# B: 96% ↑ (0:14)
# B: 20% ↓ (0:30)
# B: 100% ↔
 
acpi_output = %x[/usr/bin/acpi].split(' ').map{|i| i.gsub(',', '')}
 
status_symbols = {
'Unknown' => '↔',
'Full' => '↔',
'Charging' => '↑',
'Discharging' => '↓'
}
 
level_colors = {
'red' => (0..9),
'yellow' => (10..19),
'green' => (20..100)
}
 
output_percentage = acpi_output[3]
 
battery_level = case output_percentage.delete('%').to_i
when level_colors['red']
"<fc=#FF0000>#{output_percentage}</fc>"
when level_colors['yellow']
"<fc=#FFFF00>#{output_percentage}</fc>"
when level_colors['green']
"<fc=#00FF00>#{output_percentage}</fc>"
else
"<fc=#00FF00>#{output_percentage}</fc>"
end
 
symbol = status_symbols[acpi_output[2]]
 
time = if acpi_output[4]
time_orig = acpi_output[4].split(':')
"(#{time_orig[0]}:#{time_orig[1]})"
else
"(F)"
end
 
puts "Bat: #{battery_level} #{symbol} #{time}"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.