2007-02-08

fix statusd_volume2.lua

Original script is here
Running this script with ion3 returned stdin:1: attempt to concatenate global 'master_level' (a nil value)
I don't really know anything about lua, but figured I would check things out. Looking at the script, here is the relevant chunk:

local function get_volume()
local f=io.popen('amixer','r')
local s=f:read('*all')
f:close()
local _, _, master_level, master_state = string.find(s, "%[(%d*%%)%] %[(%a*)%]")
local sound_state = ""
if master_state == "off" then
sound_state = "MUTE "
end
return master_level.."", sound_state..""
end
Lua has a cli, just like perl or python, so I tried running part of the script manually.

$ lua
Lua 5.1.1 Copyright (C) 1994-2006 Lua.org, PUC-Rio
> f=io.popen('amixer','r')
> s=f:read('*all')
> f:close()
> master_level=string.find(s, "%[(%d*%%)%]")
> print(master_level)
171
That isn't good.
After some searching with our trusty google, it seems that lua doesn't work this way anymore. From the lua tutorial page here, I figured out it should actually look more like this.

> master_level = string.sub(s, string.find(s, "%[(%d*%%)%]"))
> print(master_level)
[87%]
Which is what we need to see for this script to work. Wohoo!
Here is the relevant chunk of the fixed script.

local function get_volume()
local f=io.popen('amixer','r')
local s=f:read('*all')
f:close()
local master_level = string.sub(s, string.find(s, "%[(%d*%%)%]"))
local master_state = string.sub(s, string.find(s, "%[(%a*)%]"))
local sound_state = ""
if master_state == "[off]" then
sound_state = "MUTE "
end
return master_level.."", sound_state..""
end
Also, here is the diff from my version of the script and the original statuds_volume2.lua script.

$ diff statusd_volume2.lua statusd_volume.lua
31c31,32
< local _, _, master_level, master_state = string.find(s, "%[(%d*%%)%] %[(%a*)%]")
---
> local master_level = string.sub(s, string.find(s, "%[(%d*%%)%]"))
> local master_state = string.sub(s, string.find(s, "%[(%a*)%]"))
33c34
< if master_state == "off" then
---
> if master_state == "[off]" then

No comments: