class Barabara::Modules::Battery
Constants
- ICONS
Predefined status icons:
Public Class Methods
new(config = GlobalConfig.config.module_config('battery'))
click to toggle source
Initialize new Battery
object.
@param name [String] System battery name.
# File lib/barabara/modules/battery.rb, line 27 def initialize(config = GlobalConfig.config.module_config('battery')) @name = config['name'] || ENV['BAT'] @path = "/sys/class/power_supply/#{@name}/uevent" @capacity = 100 @power = 0.0 @status = 'U' @icons = config['icons'] || ICONS end
Public Instance Methods
format_string()
click to toggle source
Prepare output format string.
@return [String] Format string suitable for Kernel#printf.
# File lib/barabara/modules/battery.rb, line 68 def format_string case @status when 'F' then @icons['full'] when 'C' then @icons['charge'] + ' %<capacity>d%%' else if @power > 3.5 '%<icon>s %<capacity>d%%:%<power>.0fW' else '%<icon>s %<capacity>d%%:%<power>.1fW' end end end
icon()
click to toggle source
Select battery status icon.
@return [String] Battery
status icon.
# File lib/barabara/modules/battery.rb, line 54 def icon return 'U' unless @status == 'D' case @capacity when 0..35 then @icons['low'] when 36..65 then @icons['med'] when 66..100 then @icons['high'] else 'U' end end
parse!()
click to toggle source
Read battery status from sysfs. Only updates the object attributes, does not return anything.
# File lib/barabara/modules/battery.rb, line 38 def parse! IO.readlines(@path).each do |line| case line when /POWER_SUPPLY_STATUS=/ @status = line.split('=')[1][0] when /POWER_SUPPLY_CAPACITY=/ @capacity = line.split('=')[1].to_i when /POWER_SUPPLY_POWER_NOW=/ @power = line.split('=')[1].to_f / 10**6 end end end
render()
click to toggle source
Render battery status as a string.
@return [String] Battery
status (ready for sending to the panel).
# File lib/barabara/modules/battery.rb, line 91 def render parse! format(format_string, to_h) end
to_h()
click to toggle source
Convert battery attributes to hash.
@return [Hash] Attribute hash suitable for String#format.
# File lib/barabara/modules/battery.rb, line 84 def to_h { icon: icon, capacity: @capacity, power: @power } end
watch()
click to toggle source
Enter event loop and feed the message bus with events.
# File lib/barabara/modules/battery.rb, line 97 def watch loop do publish(:event, 'battery', render) sleep @status == 'C' ? 30 : 10 end end