Programming and Scripting :: tiny lua-fltk script



my dumb mistake.
I'd downloaded 0.3 to my slackware system the day it was released, but never actually installed it =o)

I think the Fl_Input_Choice behavior is visually odd, although its actual behavior is as expected.

In fact it behaves like a Fl_Menu_Button and certainly that is odd, considering the beginning of the widget is far to the left and all that space is wasted fot the menu.

I expect the next revision of the toolkit adresses that.

BTW, is it correct to asume a simple regex of *: matches all text in a string before double dots and :* matches all the string after that? Not at the right machine right now, and I need exactly that. Yeah, /opt/grun/gassoc

Mmmmm, thinking about it, it will match that *AND* the double dots, and I don't want that. Will try anyway.

[Edit]: .*:??  :??.*
[Edit2]: http://www.regular-expressions.info/anchors.html <--- This is interesting, but my head hurts already. I'll try to grasp the concept later. I need a rest.
[Edit3]: ^[a-z]*:\b  :\b[a-z]*$  <---- Argh! :lol:  cu l8r

This might help:
http://www.lua.org/manual/5.1/manual.html#5.4.1

for all text in a string before a colon I'd try .*: and following a colon I'd use :.*

You may need to add a % before the colon if it is not interpreted as part of the string (i don't think this is necessary, though)

Thanks mikshaw. Didn't work, but pointed me into the (almost) right direction. I did some dirty workaround here and there, but flRun v1.0.0 made it into usefulness :)

Potentially spaghetti code follows:
Code Sample

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

version="1.0.0"
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={}
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

gassoc_table={}
file = io.open(gassoc_file, "r")
if file then
 for line in file:lines() do
   table.insert(gassoc_table,{
                line:sub(line:find(".*%f[:]"));
                line:sub(line:find("%a*$"))})
 end
 file:close()
end

input = fltk:Fl_Input_Choice(10,30,ww-20,bh,input_text)
input:align(fltk.FL_ALIGN_TOP)
if table_length~=0 then
 input:value(history_table[table_length]) -- Show last feed command
 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()
   command=input:value()
   if command:find("%.%a*$") then
     ext=command:sub(command:find("%.%a*$"))
     for i=1,table.maxn(gassoc_table),1 do
       if ext == '.'..gassoc_table[i][1] then
         command=gassoc_table[i][2]..' '..command
       end
     end
   end
   os.execute(command..' 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)

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

Nice Job!

When looking through this, I thought of a way to implement console based applications. Instead of using pattern matching on the associations table build, use the colon as a delimiter, by doing this we could eliminate the need of the grun consfile and its implementation. Simply by specifying, as we do for fluxbox menu, via an aterm -e prefix. For example, in my assoc file I added the following lines:

lua:aterm -e vi
lst:aterm -e vi
sh:aterm -e vi

To implement this meant only to change the table build from pattern matching to delimiter. Here is the modded flrun.lua


Code Sample

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

version="1.0.0"
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={}
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

assoc = {}
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
end

input = fltk:Fl_Input_Choice(10,30,ww-20,bh,input_text)
input:align(fltk.FL_ALIGN_TOP)
if table_length~=0 then
input:value(history_table[table_length]) -- Show last feed command
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()
  command=input:value()
  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
      end
    end
  end
  os.execute(command..' 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)

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

Next Page...
original here.