Programming and Scripting :: lua fltk scripting help for mpd client



I'm hoping someone has better scripting skills than I do, because I could really use some help. I'm working on a simple gui front-end for the mpc extension in the myDSL repository.

I've got the basic buttons working, but I'm having a heck of a time with the volume slider. I can get it to update the mpd server's volume without a hitch, but I cannot for the life of me get it to read in the server's current volume on startup of the script.

Can someone please take a look? I'd love to get this working so I can submit it to both the repository, and the musicpd.org website.

I promise I'll tidy my (currently awful) code up before I release anything.

Code Sample

#!/bin/flua

volnum = execute("/opt/mpc | grep volume | sed 's/[[:alpha:]]//g' | sed 's/%//g' | sed 's/[[:space:]]//g' | sed 's/://g'")
volcmd = {}
window = Window{210,65; label="jukebox control"}


b1 = Button{10,10,30,30, "<<"}
function b1.callback()
  execute("/opt/mpc prev")
end

b2 = Button{50,10,30,30, "->"}
function b2.callback()
  execute("/opt/mpc play")
end

b3 = Button{90,10,30,30, "||"}
function b3.callback()
  execute("/opt/mpc toggle")
end

b4 = Button{130,10,30,30, "[]"}
function b4.callback()
  execute("/opt/mpc stop")
end

b5 = Button{170,10,30,30, ">>"}
function b5.callback()
  execute("/opt/mpc next")
end

-- volume slider begin

volume = Value_Slider{20,50,185,15,"V:";
  align=Align.left, type=Slidertype.horiz,
  minimum=0, maximum = 100, step = 1,
  value=volnum}

function volume:callback()
volnum = self.value
volcmd = "/opt/mpc volume" .." "..  volnum
execute (volcmd)

end


-- volume slider end

window:end_layout() -- ends the layout of the window
window:show()

dare2dreamer, the execute function returns the exit code for the command you run, not its output.  There may be better ways, but here's one way of getting the output of a function into a string:

Code Sample
> myfilename=tmpname()
> print(myfilename)      
/tmp/fileZh6gak
> execute("date > " .. myfilename)
> myfile = openfile(myfilename,"r")
> mystring = read(myfile,"*a")
> print(mystring)
Thu Jun  8 07:09:19 EDT 2006


> closefile(myfile)
> execute("rm " .. myfilename)
>
> mynewstring=gsub(mystring,"\n","")
> print(mynewstring)
Thu Jun  8 07:23:22 EDT 2006


The tmpfile() function gives you a unique string for your temp file's name.  The .. in the execute is a concatination of the two strings.  You may need to use gsub function to remove the trailing return character.

Personally I'd rather have a more direct way to access an app's stdout/stderr in lua fltk, but so far that redirect to file seem to be the best alternative.  It would be sweet to be able set a variable according to stdout like the Bash $(command) or `command`.

One note, clacker: You can use "remove(myfilename)" instead of calling an external rm.


original here.