<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.thinkwiki.org/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Foggy</id>
	<title>ThinkWiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://www.thinkwiki.org/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Foggy"/>
	<link rel="alternate" type="text/html" href="https://www.thinkwiki.org/wiki/Special:Contributions/Foggy"/>
	<updated>2026-05-09T00:44:23Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.31.12</generator>
	<entry>
		<id>https://www.thinkwiki.org/w/index.php?title=Battery.rb&amp;diff=37310</id>
		<title>Battery.rb</title>
		<link rel="alternate" type="text/html" href="https://www.thinkwiki.org/w/index.php?title=Battery.rb&amp;diff=37310"/>
		<updated>2008-04-14T02:32:15Z</updated>

		<summary type="html">&lt;p&gt;Foggy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==How to use it==&lt;br /&gt;
The following ruby script can generates the following output&lt;br /&gt;
&lt;br /&gt;
./battery.rb&lt;br /&gt;
 [+] 01:02 (90%)&lt;br /&gt;
&lt;br /&gt;
./battery.rb -v&lt;br /&gt;
 The battery is charging&lt;br /&gt;
 Current Status		90%&lt;br /&gt;
 Remaining Charging Time	01h02m&lt;br /&gt;
 Charging Till		22:52&lt;br /&gt;
 &lt;br /&gt;
 After 91 cycles the battery has 90% of its design capacity&lt;br /&gt;
&lt;br /&gt;
./battery.rb -b&lt;br /&gt;
 [######### ]&lt;br /&gt;
&lt;br /&gt;
./battery.rb -h&lt;br /&gt;
 This is a very small script for getting some usefull information about your battery&lt;br /&gt;
 Please notice that this script requires tp_smapi which is only available on ThinkPads&lt;br /&gt;
 &lt;br /&gt;
 Version 0.1 written by futejia during the 24C3 in Berlin&lt;br /&gt;
 The script is published under the 'Beer License' which means you can do whatever you want, and when you like the script you buy me a beer&lt;br /&gt;
 &lt;br /&gt;
 Command line options:&lt;br /&gt;
     All information you usually need in one single line&lt;br /&gt;
 -h  Prints this help&lt;br /&gt;
 -b  Prints a nice ascii battery (Single line)&lt;br /&gt;
 -v  Gives some more information about the battery&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==The script itself==&lt;br /&gt;
Simple safe this as battery.rb (Of course you need ruby and tp_smapi installed)&lt;br /&gt;
 &lt;br /&gt;
 #!/usr/bin/ruby &lt;br /&gt;
 def getFileValue(filename)&lt;br /&gt;
   file = File.new(filename,&amp;quot;r&amp;quot;)&lt;br /&gt;
   content = file.gets&lt;br /&gt;
   file.close&lt;br /&gt;
   content = content.delete &amp;quot;\n&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def addLeadingZero(value)&lt;br /&gt;
   if value &amp;lt; 10&lt;br /&gt;
     output = &amp;quot;0&amp;quot; + value.to_s&lt;br /&gt;
   else&lt;br /&gt;
     output = value.to_s&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 begin&lt;br /&gt;
   state = getFileValue(&amp;quot;/sys/devices/platform/smapi/BAT0/state&amp;quot;)&lt;br /&gt;
   if state == &amp;quot;charging&amp;quot; &lt;br /&gt;
     remaning_time = getFileValue(&amp;quot;/sys/devices/platform/smapi/BAT0/remaining_charging_time&amp;quot;).to_i&lt;br /&gt;
     symbol = &amp;quot;[+]&amp;quot;&lt;br /&gt;
     descriptingword = &amp;quot;Charging&amp;quot;&lt;br /&gt;
   elsif state == &amp;quot;discharging&amp;quot;&lt;br /&gt;
     remaning_time = getFileValue(&amp;quot;/sys/devices/platform/smapi/BAT0/remaining_running_time&amp;quot;).to_i&lt;br /&gt;
     symbol = &amp;quot;[-]&amp;quot;&lt;br /&gt;
     descriptingword = &amp;quot;Running&amp;quot;&lt;br /&gt;
   elsif state == &amp;quot;idle&amp;quot;&lt;br /&gt;
     remaning_time = 0&lt;br /&gt;
     symbol = &amp;quot;[ ]&amp;quot;&lt;br /&gt;
     descriptingword = &amp;quot;Idle&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
     puts &amp;quot;Unknown value: '&amp;quot;+state+&amp;quot;'&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   percent = getFileValue(&amp;quot;/sys/devices/platform/smapi/BAT0/remaining_percent&amp;quot;).to_i&lt;br /&gt;
   batterybar = (percent.to_f / 10).round&lt;br /&gt;
   hours = remaning_time / 60&lt;br /&gt;
   minutes = remaning_time % 60&lt;br /&gt;
 &lt;br /&gt;
   if ARGV[0] == &amp;quot;-v&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
     puts &amp;quot;The battery is &amp;quot; + state&lt;br /&gt;
     #getting some information about the current capacity&lt;br /&gt;
     design_capacity = getFileValue(&amp;quot;/sys/devices/platform/smapi/BAT0/design_capacity&amp;quot;).to_i&lt;br /&gt;
     last_full_capacity = getFileValue(&amp;quot;/sys/devices/platform/smapi/BAT0/last_full_capacity&amp;quot;).to_i&lt;br /&gt;
     capacity = last_full_capacity * 100 / design_capacity&lt;br /&gt;
 &lt;br /&gt;
     cycles = getFileValue(&amp;quot;/sys/devices/platform/smapi/BAT0/cycle_count&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
     d = Time::now()&lt;br /&gt;
     d += remaning_time * 60&lt;br /&gt;
     puts &amp;quot;Current Status\t\t&amp;quot; + percent.to_s + &amp;quot;%&amp;quot;&lt;br /&gt;
     puts &amp;quot;Remaining &amp;quot; + descriptingword + &amp;quot; Time\t&amp;quot; +   addLeadingZero(hours) + &amp;quot;h&amp;quot; + addLeadingZero(minutes) + &amp;quot;m&amp;quot;&lt;br /&gt;
     if state != &amp;quot;idle&amp;quot;&lt;br /&gt;
       puts descriptingword + &amp;quot; Till\t\t&amp;quot; + addLeadingZero(d.hour()) + &amp;quot;:&amp;quot; + addLeadingZero(d.min())&lt;br /&gt;
     end&lt;br /&gt;
     puts &amp;quot;&amp;quot;&lt;br /&gt;
     puts &amp;quot;After &amp;quot; + cycles.to_s + &amp;quot; cycles the battery has &amp;quot; + capacity.to_s + &amp;quot;% of its design capacity&amp;quot;&lt;br /&gt;
   elsif ARGV[0] == &amp;quot;-b&amp;quot;&lt;br /&gt;
     battery = &amp;quot;&amp;quot;&lt;br /&gt;
     batterybar.downto(1) { battery += &amp;quot;#&amp;quot; }&lt;br /&gt;
     batterybar.upto(9) { battery += &amp;quot; &amp;quot; }&lt;br /&gt;
     puts &amp;quot;[&amp;quot; + battery + &amp;quot;]&amp;quot;&lt;br /&gt;
   elsif ARGV[0] == &amp;quot;-h&amp;quot;&lt;br /&gt;
     puts &amp;quot;This is a very small script for getting some usefull information about your battery&amp;quot;&lt;br /&gt;
     puts &amp;quot;Please notice that this script requires tp_smapi which is only available on ThinkPads&amp;quot;&lt;br /&gt;
     puts&lt;br /&gt;
     puts &amp;quot;Version 0.1 written by futejia during the 24C3 in Berlin&amp;quot;&lt;br /&gt;
     puts &amp;quot;The script is published under the 'Beer License' which means you can do whatever you want, and when you like the script you buy me a beer&amp;quot;&lt;br /&gt;
     puts &lt;br /&gt;
     puts &amp;quot;Command line options:&amp;quot;&lt;br /&gt;
     puts &amp;quot;    All information you usually need in one single line&amp;quot;&lt;br /&gt;
     puts &amp;quot;-h  Prints this help&amp;quot;&lt;br /&gt;
     puts &amp;quot;-b  Prints a nice ascii battery (Single line)&amp;quot;&lt;br /&gt;
     puts &amp;quot;-v  Gives some more information about the battery&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
     puts symbol + &amp;quot; &amp;quot; + addLeadingZero(hours) + &amp;quot;:&amp;quot; + addLeadingZero(minutes) + &amp;quot; (&amp;quot; + percent.to_s + &amp;quot;%)&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;/div&gt;</summary>
		<author><name>Foggy</name></author>
		
	</entry>
</feed>