Created
January 28, 2011 00:43
-
-
Save Igneous/799608 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/luajit2 | |
require "socket" | |
require "LuaXml" | |
io.stdout:setvbuf "no" | |
--zipcode = 38501 | |
function round(number, decimal) | |
local multiplier = 10^(decimal or 0) | |
return math.floor(number * multiplier + 0.5) / multiplier | |
end | |
function sleep(sec) | |
socket.select(nil,nil,sec) | |
end | |
function getfield (f) | |
local v = _G -- start with the table of globals | |
for w in string.gfind(f, "[%w_]+") do | |
v = v[w] | |
end | |
return v | |
end | |
function setfield (f, v) | |
local t = _G -- start with the table of globals | |
for w, d in string.gfind(f, "([%w_]+)(.?)") do | |
if d == "." then -- not last field? | |
t[w] = t[w] or {} -- create table if absent | |
t = t[w] -- get the table | |
else -- last field | |
t[w] = v -- do the assignment | |
end | |
end | |
end | |
--[[ Not used atm | |
function getweather() | |
local http = require("socket.http") | |
local ltn12 = require("ltn12") | |
local io = require("io") | |
http.request { | |
url = "http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=" .. zipcode , | |
sink = ltn12.sink.file(io.open("weather.xml", "w")) | |
} | |
end | |
function parseweather() | |
local wxml = assert(xml.load("weather.xml")) | |
location = xml.find(wxml:find("display_location"),"full") | |
temp = wxml:find("temp_f") | |
weather = wxml:find("weather") | |
wicon = xml.find(wxml:find("icon_set","name","Mobile"),"icon_url") | |
end | |
]]-- | |
function getcpupct(cpuname) | |
if getfield("prev_idle" .. cpuname) == nil then | |
setfield("prev_idle" .. cpuname, 0) | |
end | |
if getfield("prev_total" .. cpuname) == nil then | |
setfield("prev_total" .. cpuname, 0) | |
end | |
local statfile = assert(io.open("/proc/stat", "r")) | |
--- Get the cpu line and insert each space-seperated value into a table | |
for line in statfile:lines() do | |
local sfound = string.find(line, cpuname) | |
if sfound ~= nil then | |
cpu = {} | |
local sword | |
for sword in line:gmatch("%S+") do | |
table.insert(cpu,sword) | |
end | |
table.remove(cpu,1) | |
end | |
end | |
statfile:close() | |
-- Get the idle cpu time | |
local idle = cpu[4] | |
-- Calculate the total cpu time | |
local total = 0 | |
for row,cpuval in ipairs(cpu) do | |
total = total + cpuval | |
-- print("Loop run, total = ", total, " cpuval = ", cpuval) | |
end | |
-- Calculate the CPU usage since we last checked | |
local diff_idle = idle - getfield("prev_idle" .. cpuname) | |
local diff_total = total - getfield("prev_total" .. cpuname) | |
local diff_usage = (1000 * (diff_total - diff_idle) / diff_total + 5) / 10 | |
-- Remember the total and idle CPU times for the next check | |
setfield("prev_total" .. cpuname, total) | |
setfield("prev_idle" .. cpuname, idle) | |
return round(diff_usage, 0) | |
end | |
function getmempct() | |
local memfile = assert(io.open("/proc/meminfo", "r")) | |
for line in memfile:lines() do | |
itr = 0 | |
if string.find(line, "MemTotal: ", 1) then | |
for mtword in line:gmatch("%S+") do | |
itr = itr + 1 | |
if itr == 2 then | |
mtotal = mtword | |
end | |
end | |
end | |
if string.find(line, "MemFree: ", 1) then | |
for mfword in line:gmatch("%S+") do | |
itr = itr + 1 | |
if itr == 2 then | |
mfree = mfword | |
end | |
end | |
end | |
if string.find(line, "Buffers: ", 1) then | |
for mbword in line:gmatch("%S+") do | |
itr = itr + 1 | |
if itr == 2 then | |
mbuf = mbword | |
end | |
end | |
end | |
if string.find(line, "Cached: ", 1) and not string.find(line, "SwapCached: ", 1) then | |
for mcword in line:gmatch("%S+") do | |
itr = itr + 1 | |
if itr == 2 then | |
mcache = mcword | |
end | |
end | |
end | |
end | |
memfile:close() | |
m_divisor = mtotal / 100 | |
m_used = ( mtotal - mfree - mbuf - mcache ) / m_divisor | |
return round(m_used, 0) | |
end | |
function gdbar(pct,fgcolor,bgcolor,x_dimension,y_dimension,maxpct) | |
-- NOTE: Need to have some way to test if pct > maxpct, if so, return maxpct | |
-- Also need to test if pct is of type int before we compare it, so our "if" statement doesn't barf | |
if tonumber(pct) > maxpct then | |
pct = maxpct | |
end | |
local gdbar_diff = maxpct / x_dimension | |
local gdbar_div = pct / gdbar_diff | |
local gdbar_rdiv = round(gdbar_div) | |
return("^fg(" .. fgcolor .. ")^r(" .. gdbar_rdiv .. "x" .. y_dimension .. ")^fg(" .. bgcolor .. ")^r(" .. x_dimension - gdbar_rdiv .. "x" .. y_dimension .. ")^fg()") | |
end | |
--[[ | |
getweather() | |
parseweather() | |
]]-- | |
main_loop_itr = 0 | |
while true do | |
main_loop_itr = main_loop_itr + 1 | |
date = os.date("%A, %b %d. %I:%M%p") | |
date_right_px = string.len(date) * 6 + 10 | |
cpu_pct0 = getcpupct("cpu0 ") | |
cpu_pct1 = getcpupct("cpu1 ") | |
mem_pct = getmempct() | |
--[[ | |
if main_loop_itr == 200 then | |
main_loop_itr = 0 | |
getweather() | |
parseweather() | |
end | |
]]-- | |
print( | |
"^p(_LEFT)^p(-120)cpu0:^p(3)" .. gdbar(cpu_pct0,"white","darkgrey",80,10,100) .. | |
"^p(10)cpu1:^p(3)" .. gdbar(cpu_pct1,"white","darkgrey",80,10,100) .. | |
"^p(10)mused:^p(3)" .. gdbar(mem_pct,"white","darkgrey",80,10,100) .. | |
-- "^p(10)^i(/home/igneous/.dzen/bitmaps/weather/clear.xbm) " .. location[1] .. ": (" .. weather[1] .. ")@" .. temp[1] .. "f " .. | |
"^p(_RIGHT)^p(-" .. date_right_px .. ")" .. date | |
) | |
sleep(1.5) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment