Skip to main content

Small remake of Acpitool based power widget for Awesome3

Frist you need to have acpitool installed which is basically a Linux ACPI client, allowing you to query or set ACPI values and display them. Basically said it parses /proc/acpi or /sys/class entries and presents the output in a well formatted text. Acpitool is available in most of the Linux distros and the requirement is that your kernel has the support for ACPI, which it probably does.

Here is the code, also don't forget to register  mybattmon to the widget list in rc.lua



mybattmon = widget({ type = "textbox", 
                     name = "mybattmon",
                     align = "right" })

function battery_status ()
    --output buffer
    local output={} 
    -- read data from battery
    local fd=io.popen("acpitool -b", "r") 
    local line=fd:read()
    -- we match the output from acpitool
    local battery_load = string.match(line,
                           " (%d*\.%d+)%%")
    local time_rem = string.match(line,
                           "(%d+\:%d+)\:%d+")
    local discharging -- temp var for opening tag
    if string.match(line, 
                   "discharging")=="discharging" then 
                -- discharging: set the color to red
                discharging="< span color=\"#CC7777\">"
    elseif tonumber(battery_load)>85 then --almost charged
                discharging="< span color=\"#77CC77\">"
    else --charging
                discharging="< span color=\"#CCCC77\">"
    end
    if battery_num and battery_load and time_rem then
       table.insert(output,
       discharging.."B "..battery_load.."%% "..time_rem..
       "</span >")
    elseif battery_num and battery_load then 
    --remaining time unavailable so the battery is loading
       table.insert(output,discharging.."B "..battery_load..
       "%%</span >")
     end 
     -- even more data unavailable: 
     -- we might be getting an unexpected output format 
     -- so let's just skip this line.
    return table.concat(output," ") 
end
mybattmon.text = " " .. battery_status() .. " "
-- every 30 sec call the fucntion battery_status()
my_battmon_timer=timer({timeout=30}) 
my_battmon_timer:add_signal("timeout", function()
    mybattmon.text = " " .. battery_status() .. " "
end)
my_battmon_timer:start()

and I must say Lua is really good scripting language.
The original code works with multiple batteries and can be found here

Popular posts from this blog

HTML 5 data-* attributes, how to use them and why

It is always tempting to add custom attributes in HTML so that you can use the data stored there to do X. But if you do that there is no way of knowing if your HTML attribute will not be overridden in the future and used for something else and additionally you will not be writing valid HTML markup that can pass HTML 5 validator and with that you can create some very bad side effects. That is why there is a spec in HTML 5 called custom data attributes that enable number of useful features.

You may go around and read the specs, but the basic idea is very simple, you can add any attribute that starts with "data-" and that attribute will be treated as non-visible data for that attribute. By non-visible I mean that it is not something that gets rendered to the client so it does not affect the layout or style of the page, but it is there in the HTML so in no way this is private.
So let's get right into it, the following snippet is a valid HTML5 markup

<div id="aweso…

Basic Authentication with RestTemplate

Spring Rest Templates are very good way of writing REST clients. By default they work with basic HTTP so if we need to use Basic Authorization we would need to init the rest template with custom HttpClient. This way the Rest Template will automatically use Basic Auth and append to the HTTP headers "Authorization: Basic BASE64ENCODED_USER_PASS".

HttpClient client = new HttpClient(); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("USERNAME","PASS"); client.getState().setCredentials( new AuthScope("www.example.com", 9090, AuthScope.ANY_REALM), credentials); CommonsClientHttpRequestFactory commons = new CommonsClientHttpRequestFactory(client); RestTemplate template = new RestTemplate(commons); SomeObject result = template.getForObject( "http://www.example.com:9090/",SomeObject.class );

In EE application this would probably be managed by DI framework like Spring Core and only initialized once sin…

How to Not Hate JavaScript: Tips from the Frontline

This article was originally published on voxxed under, https://www.voxxed.com/blog/2014/11/how-to-not-hate-java-script-tips-from-the-frontline/


In my work assisting teams with JavaScript related problems, I’ve noticed some common issues. If you're experiencing frustrations with the language too, this article might be of some assistance. Disclaimer: a few of my tips might be obvious to some of you, but hopefully you’ll find at least some useful nuggets of information here! These pointers are especially useful when dealing with enterprise applications and CMS solutions. This is where we have our code, the CMS code, the code from that team nobody wants to mention...and, of course, all of them are loaded asynchronously.
The Debugger Statement
This is one of the most underused features when dealing with JavaScript, especially since it’s supported by the majority of browsers. The debugger statement was part of the first edition of ECMA Script so it's been here for quite some time.

F…