Programming and Scripting :: tiny lua-fltk script



Nice! That was what I was trying, but couldn't. Took me a little to understand what you were doing. I understand the sharp operator returns table lenght. Already cleaning the code :)
Code Sample

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

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

-- L10N (Somewhat)
window_text="flRun v"..version  -- Try FireSomething for the fun of it
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={}
file = io.open(history_file, "r")
if file then
 for line in file:lines() do
   table.insert(history_table, line)
 end
 file:close()
end

assoc = {}  -- Code by Robert Shingledecker
file = io.open(gassoc_file, "r")
if file then
for line in file:lines() do
  i,_ = string.find(line,":")
  if i then
    local j = #assoc+1
    assoc[j] = {}
    assoc[j][1] = string.sub(line,1,i-1)
    assoc[j][2] = string.sub(line,i+1)
  end
end
file:close()
end

input = fltk:Fl_Input_Choice(10,30,ww-20,bh,input_text)
input:align(fltk.FL_ALIGN_TOP)
if #history_table then
 input:value(history_table[#history_table]) -- Show last feed command
 for i=1,#history_table,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
   command=input:value()
   io.output(io.open(history_file,"w+"))
   if #history_table then
     start=1
     if #history_table >= max_history then
       start=#history_table-(max_history-2) -- Discard older entries
     end
     for i=start,#history_table,1 do
       if history_table[i]~=command then
         io.write(history_table[i]..'\n')
       end
     end
   end
   io.write(command..'\n')
   io.close()
   if command:find("%.%a*$") then
     ext=command:sub(command:find("%.%a*$"))
     for i=1,#assoc,1 do
       if ext == '.'..assoc[i][1] then
         command=assoc[i][2]..' '..command -- Redefine as app + doc
       end
     end
   end -- Note: if an .ext is found but not recognized, it would be
   os.execute(command..' 2>/dev/null &') -- launched anyway, as .sh
   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)

if not arg[1] then
 w:show()
 Fl:run()
else
 print ("flRun v"..version.." launches applications under X\n")
 print "Usage flrun\n" -- [options] [file options]\n"
--  print "   file options     Launch file directly, no dialog boxes"
--  print "   --persist        Dialog persistance"
--  print "   --preload file   Load file as default command"
--  print "   --notips         Disable tooltips"
--  print "   --version        Show version"
--  print "   --help           Show this guide\n"
end


Sent to extensions, as well

Some more minor updates, more cleanup and rethought of the roberts code.
Code Sample

assoc = {}
file = io.open(assoc_file, "r")
if file then
 for line in file:lines() do
   i,i = line:find(":")
   if i then
     table.insert(assoc, {line:sub(1,i-1),line:sub(i+1)})
   end
 end
 file:close()
end

I am not sending it, though. Instead of annoying at Extensions I'll save those for some 'major' update (like full command line support).


original here.