Class: Barabara::Modules::Battery
- Inherits:
-
Object
- Object
- Barabara::Modules::Battery
- Includes:
- Wisper::Publisher
- Defined in:
- lib/barabara/modules/battery.rb
Constant Summary collapse
- ICONS =
Predefined status icons:
{ 'low' => "\ue034", 'med' => "\ue036", 'high' => "\ue037", 'full' => "\ue09e", 'charge' => "\ue041" }.freeze
Instance Method Summary collapse
-
#format_string ⇒ String
Prepare output format string.
-
#icon ⇒ String
Select battery status icon.
-
#initialize(config = GlobalConfig.config.module_config('battery')) ⇒ Battery
constructor
Initialize new Battery object.
-
#parse! ⇒ Object
Read battery status from sysfs.
-
#render ⇒ String
Render battery status as a string.
-
#to_h ⇒ Hash
Convert battery attributes to hash.
-
#watch ⇒ Object
Enter event loop and feed the message bus with events.
Constructor Details
#initialize(config = GlobalConfig.config.module_config('battery')) ⇒ Battery
Initialize new Battery object.
27 28 29 30 31 32 33 34 |
# 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 |
Instance Method Details
#format_string ⇒ String
Prepare output format string.
68 69 70 71 72 73 74 75 76 77 78 79 |
# 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 ⇒ String
Select battery status icon.
54 55 56 57 58 59 60 61 62 63 |
# 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! ⇒ Object
Read battery status from sysfs. Only updates the object attributes, does not return anything.
38 39 40 41 42 43 44 45 46 47 48 49 |
# 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 ⇒ String
Render battery status as a string.
91 92 93 94 |
# File 'lib/barabara/modules/battery.rb', line 91 def render parse! format(format_string, to_h) end |
#to_h ⇒ Hash
Convert battery attributes to hash.
84 85 86 |
# File 'lib/barabara/modules/battery.rb', line 84 def to_h { icon: icon, capacity: @capacity, power: @power } end |
#watch ⇒ Object
Enter event loop and feed the message bus with events.
97 98 99 100 101 102 |
# File 'lib/barabara/modules/battery.rb', line 97 def watch loop do publish(:event, 'battery', render) sleep @status == 'C' ? 30 : 10 end end |