Programming and Scripting :: xbattery - works nicely in JWM taskbar <Swallow>



I found that I was only using Torsmo to keep an eye on my battery and CPU.  The xload app gives me enough information about the CPU, and I've adapted and installed xbattery.

xbattery is written in Tcl, and I'd like to convert it to MurgaLua so that it's native to DSL.

Can anyone offer up tips about the equivalent commands and flow control structures for Lua?  In particular...

1) how to read the apm file?
2) how to get substrings?
3) how to construct the graphical interface?
4) what format images work best with Lua / DSL?
5) how to sleep() in Lua?

Much thanks for any pointers,
J

PS - here's the Tcl code:
Code Sample
#!/bin/wish8.3 -f
#
# xbattery V1.0
#
# Battery and AC power status for X using the kernel's Advanced Power
# Management (APM) BIOS interface.
#
# Author         : Roland Barmettler <roli@freestone.ch>
# Last modified  : 15.02.97
#------------------------------------------------------------------------------
#  
#     Copyright (C) 1997 by Roland Barmettler <roli@freestone.ch>
#
#     This program is free software; you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation; either version 2 of the License, or
#     (at your option) any later version.
#
#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with this program; if not, write to the Free Software
#     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#------------------------------------------------------------------------------

# Check for the existance of /proc/apm

if { [file exists "/proc/apm"] } \
{
 # init variables
 set capacity "999%"
 set battexists ""
 set cap 999
 set ac 0
 set spacer "   "

 set batt_xbm_100 "/etc/X11/include/X11/bitmaps/battery_100.xbm"
 set batt_xbm_90 "/etc/X11/include/X11/bitmaps/battery_90.xbm"
 set batt_xbm_80 "/etc/X11/include/X11/bitmaps/battery_80.xbm"
 set batt_xbm_70 "/etc/X11/include/X11/bitmaps/battery_70.xbm"
 set batt_xbm_60 "/etc/X11/include/X11/bitmaps/battery_60.xbm"
 set batt_xbm_50 "/etc/X11/include/X11/bitmaps/battery_50.xbm"
 set batt_xbm_40 "/etc/X11/include/X11/bitmaps/battery_40.xbm"
 set batt_xbm_30 "/etc/X11/include/X11/bitmaps/battery_30.xbm"
 set batt_xbm_20 "/etc/X11/include/X11/bitmaps/battery_20.xbm"
 set batt_xbm_10 "/etc/X11/include/X11/bitmaps/battery_10.xbm"
 set batt_xbm_0 "/etc/X11/include/X11/bitmaps/battery_0.xbm"
 set ac_power_xbm "/etc/X11/include/X11/bitmaps/ac_power.xbm"

 set backg #CFCFCF

 set backg_high    #00A000
 set backg_med     #A0A000
 set backg_medlow  #FF5000
 set backg_low     #FF0000

 # Init X interface
 label .bitmap -bitmap @$batt_xbm_100 -bg $backg -relief ridge
 label .capacity -textvariable capacity -bg $backg -relief ridge
 label .spacer -textvariable spacer -bg $backg
 #label .battexists -textvariable battexists -bg $backg
 pack .bitmap -anchor n -side top -fill both -expand 1
 pack .spacer -anchor s -side right -fill both -expand 1
 pack .capacity -anchor s -side right -fill both -expand 1
 #pack .battexists -anchor s -side right -fill both -expand 1

 while { 1 } \
 {
   # get info from /proc/apm
   set apm_file [open "/proc/apm" r ]
   gets $apm_file apm_info
   set capacity [string trim [string range "$apm_info" 28 32] " -"]
   set battexists [string range "$apm_info" 29 31]
   set cap [string trim $capacity "%"]
   set cap [string trim $cap " "]
   set ac [string index "$apm_info" 17]
   
   # is there a battery installed ?
   if { $battexists == "-1%" } \
   {
     set capacity "No Batt"
   }

   # are we connected to AC power ?
   if { $ac == 1 } \
   {
     # yes, we are
     destroy .bitmap
     label .bitmap -bitmap @$ac_power_xbm -bg $backg -relief ridge
     pack .bitmap -anchor n -side top -fill both -expand 1
   } else \
   {
     # no, we are running on battery power
     destroy .bitmap
     if { $cap > 90 } {
       label .bitmap -bitmap @$batt_xbm_100 -bg $backg_high -relief ridge
     } elseif { $cap > 80 } {
       label .bitmap -bitmap @$batt_xbm_90 -bg $backg_high -relief ridge
     } elseif { $cap > 70 } {
       label .bitmap -bitmap @$batt_xbm_80 -bg $backg_high -relief ridge
     } elseif { $cap > 60 } {
       label .bitmap -bitmap @$batt_xbm_70 -bg $backg_high -relief ridge
     } elseif { $cap > 50 } {
       label .bitmap -bitmap @$batt_xbm_60 -bg $backg_med -relief ridge
     } elseif { $cap > 40 } {
       label .bitmap -bitmap @$batt_xbm_50 -bg $backg_med -relief ridge
     } elseif { $cap > 30 } {
       label .bitmap -bitmap @$batt_xbm_40 -bg $backg_med -relief ridge
     } elseif { $cap > 20 } {
       label .bitmap -bitmap @$batt_xbm_30 -bg $backg_medlow -relief ridge
     } elseif { $cap > 10 } {
       label .bitmap -bitmap @$batt_xbm_20 -bg $backg_medlow -relief ridge
     } elseif { $cap > 0  } {
       label .bitmap -bitmap @$batt_xbm_10 -bg $backg_low -relief ridge
     } else {
       label .bitmap -bitmap @$batt_xbm_0 -bg $backg_low -relief ridge
     }
     # if the battery is really low, use red background colour
     if { $cap < 5 } {
       destroy .bitmap
       label .bitmap -bitmap @$batt_xbm_0 -bg $backg_low -relief ridge
     }
     pack .bitmap -anchor n -side top -fill both -expand 1
   }
   update idletasks
   close $apm_file

   # Sleep for 10 seconds
   after 10000
 }
} else \
{
 label .info -text "No APM BIOS support found !" -relief ridge
 button .ok -text "That's too bad" -command exit
 pack .info .ok -padx 2m -pady 2m
}

Quote
I found that I was only using Torsmo to keep an eye on my battery and CPU.  The xload app gives me enough information about the CPU...


Man who uses too many tools to monitor battery and CPU runs charge out of battery more quickly...


original here.