#!/usr/bin/ruby sleep_interval = 60 def read_data(filename) data = Hash.new File.open(filename) do |file| file.each_line do |line| fields = line.chomp.split(/:\s+/) data[fields[0]] = fields[1] end end return data end def format_hours(hours) hour_count = hours.floor minute_count = ((hours - hour_count) * 60).floor second_count = ((((hours - hour_count) * 60) - minute_count) * 60).floor return sprintf("%02d:%02d:%02d", hour_count, minute_count, second_count) end info = read_data("/proc/acpi/battery/C174/info") state = read_data("/proc/acpi/battery/C174/state") cap_pct = 100 * state["remaining capacity"].to_f / info["design capacity"].to_f if state["charging state"] == "discharging" then remaining_time = format_hours(state["remaining capacity"].to_f / state["present rate"].to_f) elsif state["charging state"] == "charging" then remaining_time = format_hours((info["design capacity"].to_i - state["remaining capacity"].to_i) / state["present rate"].to_f) else remaining_time = "unknown" end text = sprintf("%s, %d%%, %s", state["charging state"], cap_pct, remaining_time) system("osd_cat", "--pos=middle", "--align=center", "--font=-*-terminus-bold-r-*-*-*-320-*-*-*-*-iso10646-1", "--color=yellow", "--shadow=1", "--barmode=percentage", "--percentage=#{cap_pct}", "--text=#{text}")