-- One-liners and small pieces of code that may be useful in murgaLua
-- NOTE: user-created strings and variable names used in these examples
-- are preceded by "my_". For example, the variable "my_browser" is
-- assumed to be an Fl_Browser named my_browser. The exception is in
-- functions written by others, where it is shown here without change.

-- Print the FLTK version (floating point format)
print(Fl:version())

-- Print the Lua version
print(_VERSION)

-- Print the name of the host operating system
print(murgaLua.getHostOsName())

-- Print the full filename of the current script
print(arg[0])

-- Disable the dotted-line focus indicator (only occasionally useful)
Fl:visible_focus(0)

-- 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 font
Fl:set_font(fltk.FL_HELVETICA,"my_font_name")

-- Change the font of all popup messages
fltk.fl_message_font(font,font_size)

-- 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)

-- Allow scheme to be set from FLTK_SCHEME environment variable
-- or optionally, in X11, the fltk.scheme X resource
Fl:scheme(NULL)

-- Allow colors to be inherited from system
-- (X11 resources: fltk.background, fltk.foreground, Text.background)
Fl:get_system_colors()

-- Print a filename extension (does not need to be an existing file)
print(fltk.fl_filename_ext("my_filename.txt"))

-- Print the file type of a given file
print(lfs.attributes("my_filename").mode)

-- Generate a pseudo-random number between 1 and 10
-- Thanks to Bill for helping me with this
math.randomseed(os.time()) -- os.time() is an arbitrary function used to supply a seed number
math.random(1,10)

-- Print the class name (Fl_*) of a given widget
print(my_widget.bind_lua_typeinfo.name)

-- Print the key/value pairs of a table
for k,v in pairs(my_table) do print(k,v) end
 
-- Print the index/value pairs of a numerical indexed table
for k,v in pairs(my_table) do print(k,v) end
for i,v in ipairs(my_table) do print(i,v) end
for i=1,table.getn(testing) do print(i,testing[i]) end -- not as reliable
 
-- 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

-- 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)

-- Get boot parameter on Unix-like systems with /proc/cmdline
-- For parameter "A=B", target is "A=", result is "B".
-- written by Robert Shingledecker for Damn Small Linux
function getbootparam(target)
  filename = "/proc/cmdline"
  io.input(filename) -- set stdin
  cmdline = io.read() -- read stdin
  local x,y=string.find(cmdline,target) -- find start and end of target in /proc/cmdline contents
  if x ~= nil then
     result = string.sub(cmdline,y+1) 
     local i,j=string.find(result,' ')
     if i ~= nil then
        result = string.sub(result,1,i-1)
     end
     if string.sub(target,-1,-1) ~= "=" then
        result = target
     end
     return result
  end
  io.input()
end

