Programming and Scripting :: tiny lua-fltk script



Sure!
Code Sample

#!/bin/lua
-- Launches applications under X
-- by Daniel Plata Lorenzo "MakodFilu"

version="0.9.1"
max_history=10
history_file=os.getenv("HOME").."/.flrun_history"
ww=250  --window width
wh=100  --window height
bw=60   --button width
bh=25   --button height

-- L10N (Somewhat)
window_text="flRun v"..version
input_text="Launch Application"
cancel_text="&Cancel"
cancel_tooltip="Cancel launch"
ok_text="&Ok"
ok_tooltip="Launch application"
browse_text="&Browse"
browse_tooltip="Browse for command"
choose_text="Choose Application"

-- Code
w=fltk:Fl_Window(ww,wh,window_text)

history_table={}
table_length=0
file = io.open(history_file, "r")
if file then
 for line in file:lines() do
   table.insert(history_table, line)
 end
 file:close()
 table_length=table.maxn(history_table)
 if not table_length then table_length=0 end
end

input = fltk:Fl_Input_Choice(10,30,230,bh,input_text)
input:align(fltk.FL_ALIGN_TOP)
if table_length~=0 then
 input:value(history_table[table_length]) -- Show last feed command
--  input:mark(1)
 for i=1,table_length,1 do
   input:add(history_table[i])
 end
end

ok = fltk:Fl_Return_Button(ww-3*bw*1.2,wh-35,bw,bh,ok_text)
ok:tooltip(ok_tooltip)
ok:callback(
function(ok)
 if input:value() == "" then
   io.write(string.char(7)) io.flush()
 else
   if table_length < max_history then start=1
     else start=table_length-(max_history-2) -- Discard older entries
   end
   io.output(io.open(history_file,"w+"))
   for i=start,table_length,1 do
     if history_table[i]~=input:value() then
       io.write(history_table[i]..'\n')
     end
   end
   io.write(input:value()..'\n')
   io.close()
   os.execute(input:value()..' 2>/dev/null &')
   os.exit(0)
 end
end)

cancel = fltk:Fl_Button(ww-2*bw*1.2,wh-35,bw,bh,cancel_text)
cancel:tooltip(cancel_tooltip)
cancel:callback(
function(cancel)
 os.exit(0)
end)

browse = fltk:Fl_Button(ww-1*bw*1.2,wh-35,bw,bh,browse_text)
browse:tooltip(browse_tooltip)
browse:callback(
function(browse)
 input:value(fltk.fl_file_chooser(choose_text,"*",single,nil))
end)

w:show()
Fl:run()

Thank you.

A couple of problems I'm having...
Fl_Input_Choice was not accepted by my murgaLua (version 0.3). Using Fl_Input or Fl_Choice does work, though.
After trying to run it a second time I get an error with line 45 (input:add(history_table[i])) if using Fl_Input, and a messed up result in history if using Fl_Choice.
I haven't looked into it any further yet, but I do know the script posted does not work for me as is.

I am using DSL 3.3RC2. I have not noticed any version disparities, but I remember reading in www.FLTK.org that Fl_Input_Choice was recently introduced into FLTK. I amazed it was already available in murgaLua wich seemengly predated the announcement.

What you are experiencing is consistent: if using Fl_Input you can't create a menu there, and if using Fl_Choice you can't get the new command to write it into the history_file. I'll try to separate that into two widgets if I can reproduce it in a clean DSL 3.3RC2. The way I did it would be not as hard as I presumed from the very start I would need two widgets for the trick.

Robert, may I ask... is the bundled DSL 3.3RC2 murgaLua newer than the DSL 3.2 one?

It's probably my version causing the trouble.  I was testing on a different system, because i'm not running DSL at the moment (and was assuming all binaries involved are the same).  I think this may be the xft-enabled version i'm using, which does have some minor differences.  Another possible difference is if Robert compiled murgaLua for DSL rather than using one of the official binaries.
I'll mess around with the script later when I get back into DSL (i'm using 3.2).

muragLua was updated to v0.3 in DSL v3.2.
The script works fine for me.

Next Page...
original here.