class R3Status::Blocks::Power

A block that acts as a power indicator.

States:

:charging, :discharging, :full, :unknown, :no_battery

Format Values

Attributes

path[RW]

The path of the battery on the disk. Examples:

* /sys/class/power_supply/BAT1/
* /proc/acpi/battery/BAT0/

Public Class Methods

new(**args, &block) click to toggle source

Creates a new instance of this class. If a block is passed, it will be stored and yielded when the block is clicked.

Calls superclass method R3Status::Blocks::Base::new
# File lib/r3status/blocks/power.rb, line 17
def initialize(**args, &block)
  args = {colors: {charging: '#6DBB45', discharging: '#F4C91D'},
          formats: {charging: 'Bat(charge) %{capacity}%',
                    default: 'Bat %{capacity}%'}}.merge(args)
  super(args, &block)
  
  
  @path = get_battery_path if path.nil?
  path << '/' if path[-1] != '/'
end

Public Instance Methods

capacity() click to toggle source

Returns the current capacity of the battery, or nil if no battery found.

# File lib/r3status/blocks/power.rb, line 43
def capacity
  return nil if state == :no_battery
  `cat #{path}capacity`.chomp.to_i
end
get_battery_path() click to toggle source

Try to find a valid battery path.

# File lib/r3status/blocks/power.rb, line 49
def get_battery_path
  base_paths = ["/proc/acpi/battery/", "/sys/class/power_supply/"]
  base_paths.each do |p|
    if Dir.exist? p
      return p + (Dir.entries(p) - %w{ . .. }).last
    end
  end
  
end
state() click to toggle source

Returns the current state of the block

# File lib/r3status/blocks/power.rb, line 37
def state
  return :no_battery unless File.exist? path
  `cat #{path}status`.chomp.downcase.to_sym 
end
update() click to toggle source

Updates the text and color of this block.

# File lib/r3status/blocks/power.rb, line 29
def update
  cap, st = capacity, state
  
  @full_text = formats[st] % {val: cap, capacity: cap, state: st}
  @text_color = colors[st]
end