#! /usr/bin/env lua -- Copyright (C) 2010 Mikael BERTHE "McKael" local sleep = require "socket".sleep local function sysload () local PROCFILE = "/proc/loadavg" local ld local fh = io.open(PROCFILE) if fh then local l = fh:read("*l") fh:close() if l then ld = l:match("^(%d+%.%d%d %d+%.%d%d %d+%.%d%d) ") end end return (ld or "") end local function systemp () local TEMPFILE = "/proc/acpi/thermal_zone/THM/temperature" local temp local fh = io.open(TEMPFILE) if fh then local l = fh:read("*l") fh:close() if l then temp = l:match("^temperature:%s+(%d+) C$") end end if temp then return temp.."°" end return "" end local function sysdate () return os.date("%a %d %b %H:%M") end local sysbatt_maxbatt local sysbatt_cmpt local function sysbatt () local BATDIR = "/proc/acpi/battery/BAT0/" local batt function setmaxcap () local fh = io.open(BATDIR.."info") if not fh then return end if sysbatt_maxbatt then if sysbatt_cmpt then sysbatt_cmpt = sysbatt_cmpt + 1 else sysbatt_cmpt = 1 end if sysbatt_cmpt % 60 ~= 0 then return end end for l in fh:lines() do local lastfcap = l:match("^last full capacity:%s*(%d+) m[AW]h") if lastfcap then sysbatt_maxbatt = lastfcap; break end end fh:close() end function getremcap () local fh = io.open(BATDIR.."state") if not fh then return end local msg = "" local cur for l in fh:lines() do local m m = l:match("^charging state:%s*(%w+)") if m then if m == "discharging" then msg = "-"; elseif m == "charging" then msg = "+"; end end m = l:match("^remaining capacity:%s*(%d+) m[AW]h") if m then cur = m break -- Charging state is given before capacity, usually end end fh:close() if not cur then return msg end local v = math.floor(tonumber(cur) * 100 / sysbatt_maxbatt) return msg..(v > 100 and "100%" or v.."%") end setmaxcap() return getremcap() end local function mcabber_unread () local UNRDFILE = "/tmp/event.unread" local unread local fh = io.open(UNRDFILE) if not fh then return "" end unread = fh:read("*l") fh:close() if unread and unread ~= "0" then return "("..unread..")" end return "" end local function awesome_update_textbox (text) local AWESOMECLI = "/usr/bin/awesome-client" local fh = io.popen(AWESOMECLI, "w") if fh then fh:write("mytextbox.text = \" "..text.."\"\n") fh:close() end end function mainloop () while true do sleep(2) local status = mcabber_unread() if status ~= "" then status = status .. " " end status = status .. sysbatt() status = status .. " " .. systemp() status = status .. " [" .. sysload() .. "]" status = status .. " - " .. sysdate() --print(status) awesome_update_textbox(status) end end --os.setlocale(os.setlocale(nil, "ctype"), "time") os.setlocale("fr_FR.utf8", "time") mainloop()