# Originally by Wael Nasreddine . # # Made a standalone script for usage with xmonad by Patrick Hof # # Last Change: 2007-12-22 class Battery STATEFILE = '/proc/acpi/battery/BAT0/state' INFOFILE = '/proc/acpi/battery/BAT0/info' LOW = 5 LOW_ACTION = 'echo "Low battery" | xmessage -center -buttons quit:0 -default quit -file -' CRITICAL = 1 CRITICAL_ACTION = 'echo "Critical battery" | xmessage -center -buttons quit:0 -default quit -file -' def initialize @warned_low = false @warned_critical = false end def battery batt = IO.readlines(STATEFILE) battinfo = IO.readlines(INFOFILE) battpresent = battinfo[0].gsub(/.*:\s*/,'').chomp if battpresent == "yes" bat_a = batt[4].gsub(/.*:\s*/,'').chomp.chomp("mAh").to_f bat_b = battinfo[2].gsub(/.*:\s*/,'').chomp.chomp(" mAh").to_f batt_percent = ((bat_a / bat_b) * 100).to_i batt_state = batt[2].gsub(/.*:\s*/,'').chomp # Take action in case battery is low/critical if batt_state == "discharging" && batt_percent <= CRITICAL unless @warned_critical system("ssid #{CRITICAL_ACTION} &") @warned_critical = true end elsif batt_state == "discharging" && batt_percent <= LOW unless @warned_low system("ssid #{LOW_ACTION} &") @warned_low = true end else @warned_low = false @warned_critical = false end # If percent is 100 and state is discharging then # the battery is full and not discharging. batt_state = "=" if batt_state == "charged" || ( batt_state == "discharging" && batt_percent >= 97 ) batt_state = "^" if batt_state == "charging" batt_state = "v" if batt_state == "discharging" text = "#{batt_state} #{batt_percent} #{batt_state}" return text else return "N/A" end end end