バッテリーの残量表示
30分プログラム、その426。HP2133に入れているUbunutuのウインドウマネージャをRatpoisonにしたら、バッテリー表示が消えてしまった。
でも、バッテリー表示のためにGNOMEにはもどれない体になってしまったので、簡単なバッテリー残量表示プログラムを書いてみた。
/proc/acpi/battery/を読めばバッテリーの情報は得られるので、わりと簡単。
使い方
実行すると残量をパーセントで表示する。
$ ./battery 89.0%
screenのステータスラインに組み込んでやる。
# 60秒(1分)に一回、残量を更新 backtick 0 0 60 battery % %0`でバッテリー残量を表示 hardstatus alwayslastline "[%02c](%0`) %-w%{=b bw}%n %t%{-}%+w"
ソースコード
#!/usr/bin/env ruby class Battery attr_reader :remain,:full def initialize(path) @remain = File.read("#{path}/state"). scan(/^remaining capacity:\s*(\d+)/).first.first.to_i @full = File.read("#{path}/info"). scan(/^last full capacity:\s*(\d+)/).first.first.to_i end def percent (@remain.to_f / @full) * 100 end end bat = Battery.new('/proc/acpi/battery/BAT1') printf "%.1f%%\n",bat.percent