Code Snippets
From Murga-Projects Wiki
Get screen dimensions
my_screen_width=Fl:w()
my_screen_height=Fl:h()
print("your screen: "..my_screen_width.."x"..my_screen_height)
Change the default box type
Fl:set_boxtype(fltk.FL_UP_BOX,fltk.FL_THIN_UP_BOX)
Fl:set_boxtype(fltk.FL_DOWN_BOX,fltk.FL_THIN_DOWN_BOX)
Print the class name (Fl_*) of a given widget
print(my_widget.bind_lua_typeinfo.name)
perform tasks depending on whether or not X is running (Linux and OSX)
if os.getenv("DISPLAY") then
graphical commands
else
console commands
end
Grab command output
my_cmd=io.popen("my_command") -- runs command
my_result=my_cmd:read("*a") -- read output of command to a variable
print(my_result)
my_cmd:close()
Display command output in Fl_Browser()
for my_line in io.popen("my_command"):lines() do my_browser:add(my_line) end
or...
my_cmd=io.popen("my_command")
for my_line in my_cmd:lines() do my_browser:add(my_line) end
my_cmd:close()
Write to a file
my_filename=io.open("my_file.txt","w")
if my_filename then -- always check for write permission
my_filename:write("some text")
my_filename:close()
end
Perform an action on an arbitrary series of objects
Similar to "for i in one two three; do echo $i; done" in bash.
for i,v in ipairs({"string", os, os.time}) do print(type(v)) end
Set up an exit confirmation
Assumes the main window (named "my_window" here) does not have another function
function my_exit()
my_confirm=fltk.fl_choice("sure you want to quit?","no","yes",NULL)
if my_confirm == 1 then os.exit(0) end
end
my_window:callback(my_exit)
Go through a directory and it's subdirectories (like in a recursive manner) and spit out a table of files (with files in a subdirectory in a subtable) and a list of directories.
function recurse(dir,maxdepth,depth) -- {{{
-- Function recurses through a dir, just to maxdepth
-- depth should only be used for recursing.
-- it returns the found files as a table, with a table for each subdir.
local attr = lfs.attributes
local output = {}
local outputdirs = {}
local cd = lfs.chdir
if depth == nil then depth = 0 end
-- TODO foolproofing
cd(dir)
depth = depth+1
for entry in lfs.dir('./') do
if not( entry == "." or entry == "..") then
local attri=attr(entry)
if attri == nil then
print ('Skipping file '..pwd()..entry..', not a normal file')
elseif attri["mode"]=="file" then
print("Added file "..pwd()..entry)
table.insert(output,pwd()..entry)
elseif attri["mode"]=="directory" then
if not (depth > maxdepth) then
print("Added and gone into "..pwd()..entry)
table.insert(outputdirs,pwd()..entry)
table.insert(output,recurse(entry,maxdepth,depth+1))
end
end
end
end
cd('..')
return output,outputdirs
end --}}}
Mapping function: applies a function to each element in the table, does not work (yet) recursive tables (tables that contain themselves). The support for having the function mapped to tables is experimental ;).
function map(func,tab,work_on_tab) --{{{
-- takes a function , a table and the boolean work_on_tab as input.
-- executes func on each element, recursing into nested tables
-- and returns the output in the same structure as the input data.
-- FIXME stackoverflows when executed on recursive tables (like after table.insert(tab,tab))
local output = {}
for k,v in pairs(tab) do
if (type(v) == 'table' and not (work_on_tab)) then
output[k] = map(func,v)
else
output[k] = func(v)
end
end
return output
end --}}}
Using Ncurses in Linux with alien.
c,i,v,p,s=alien.load("/usr/lib/libncurses.so"),"int","void","pointer","string"
--variables
TRUE = 1
FALSE = 0
ERR = -1
COLOR_BLACK=0
COLOR_RED=1
COLOR_GREEN=2
COLOR_YELLOW=3
COLOR_BLUE=4
COLOR_MAGENTA=5
COLOR_CYAN=6
COLOR_WHITE=7
NCURSES_ATTR_SHIFT=8
function NCURSES_BITS(mask,shift) return bit.lshift(mask,(shift + NCURSES_ATTR_SHIFT)) end
c.initscr:types(i); -- initialize the curses library
--c.keypad:types(i,p,b); -- enable keyboard mapping
--c.nonl:types(i); -- tell curses not to do NL->CR/NL on output
--c.cbreak:types(i); -- take input chars one at a time, no wait for \n
--c.echo:types(i); -- echo input - in color
c.start_color:types(i);
c.has_colors:types(i);
c.init_pair:types(i,i,i,i) -- number, color1 , color2 --> zoek kleuren op!o
c.endwin:types(i)
c.getch:types(i)
c.attrset:types(i,i)
--c.COLOR_PAIRS:types(i,i)
c.initscr()
--c.keypad("stdscr",1)
--c.nonl()
--c.cbreak()
--c.echo()
c.start_color()
c.init_pair(1, COLOR_RED, COLOR_BLACK);
c.init_pair(2, COLOR_GREEN, COLOR_BLACK);
c.init_pair(3, COLOR_YELLOW, COLOR_BLACK);
c.init_pair(4, COLOR_BLUE, COLOR_BLACK);
c.init_pair(5, COLOR_CYAN, COLOR_BLACK);
c.init_pair(6, COLOR_MAGENTA, COLOR_BLACK);
c.init_pair(7, COLOR_WHITE, COLOR_BLACK);
num = 0
while char ~= string.byte("q") do
char = c.getch()
-- #define
-- #define COLOR_PAIR(n) NCURSES_BITS(n, 0)
c.attrset(NCURSES_BITS(num % 8,0))
num = num+1
end
c.endwin()
os.exit()

