Aros/Developer/Docs/LUA
Lua
- Functional
- Object-orientated
There are other aspects of Lua programming than functions, but functions are easy to understand, an example...
-- comments here function my_function(par1,par2) ... end
As you see, it's easy to declare a function, you might use this function elsewhere by calling:
my_function("hello","world")
Within a function, you can declare local values. Local values have a scope which is limited to the function where they are declared. Any function may return a value, here's an example:
function calculate(a,b) local temp temp=a+b return temp end
As you may have guessed, this function performs an addition, you can call:
c=calculate(1,2)
and the value of "c" will be 3.
Lua has some common controls like "if...then" or "while...do". Here is a short example:
if val==0 then result="zero" elseif val < 0 then result="negative" else result="positive" end
And another one:
while i>0 do result=result*2 i=i-1 end
To compare numeric values, you can use the operators:
== (equals) ~= (different) >(greater) <(lower) >= (greater or equal) <= (lower or equal)
AmiLua
AmiLua extends Lua with some functions for handling intuition windows, gadgets, menus and graphics functions.
There are 3 Lua tables:
- Siamiga: Windows, creating of gadgets and menues, message handling, drawing functions
- Sigadget: setting and getting of gadget parameters
- Sipicture: loading, grabbing and blitting of bitmaps.
.alua is the specific "dialect" of lua that includes zulu and siamiga. If you are going to write some zulu or siamiga stuff you need to use the .alua extension and to point to amilua.
Siamiga
Siamiga.createwindow(title, left, top, [width], [height], [sizeable], [smartrefresh]) Siamiga.openwindow(window) Siamiga.querywindow(window) Siamiga.closewindow(window) Siamiga.pset(window, x,y) Siamiga.line(window, x1, y1 , [x2] , [y2]) Siamiga.box(window, [left], [top], [width], [height], filled) Siamiga.ellipse(window, x, y, rx, ry) Siamiga.text(window, x, y, txt) Siamiga.querytext(window, text) Siamiga.createpen(red, green, blue) Siamiga.releasepen(number) Siamiga.setpen(window, number) Siamiga.waitmessage(win1, [...]) Siamiga.getmessage(window) Siamiga.filebox([title], [default dir]) Siamiga.messagebox(window, title, text, buttons) Siamiga.queryscreen() Siamiga.addmenu(window, code, type, label, [key]) Siamiga.addgadget(window, code, type, left, top, width, height, label, [default], [min], [max]) Siamiga.__gc
Sigadget
Sigadget.set(gadget, value) Sigadget.get(gadget)
Sipicture
Sipicture.load(filename) Sipicture.put(picture, window, left, top, [width], [height]) Sipicture.get(window, left, top, width, height) Sipicture.query(picture) Sipicture.free(picture) Sipicture.__gc
A complex drawing utility, should be written it in C, as you will soon reach the limits of what LUA offers.
Zulu
Zulu allows you to create MUI applications from Lua scripts.
You'll find it in the current nightly build in the directory "Extras/Development/Lua". You need an assign named "lua" to this directory. The binary Amilua is build with the MUI binding. Some examples are in the subdir "test-mui.Documentation is in "doc/zulu.txt". If you haven't programmed MUI before you should download mui38dev from Aminet and study the documentation.
Groups are MUI/Zune classes, too. Of course, you have to look in the documentation what attributes are possible. The Group class is a subclass of the Area class, this makes it possible to use its attributes like MUIA_Disabled.
object = mui.new(class, tag1, value1, tag2, value2 , ...) object = mui.make(class, param1, param2 , ... ) mui.set(object, tag1, value1, ...) intval = mui.getint(object, tag) strval = mui.getstr(object, tag) ptrval = mui.getptr(object, tag) boolval = mui.getbool(object, tag) intval = mui.doint(object, method, ...) strval = mui.dostr(object, method, ...) ptrval = mui.doptr(object, method, ...) boolval = mui.dobool(object, method, ...) mui.dispose(object) id, signals = mui.input(app) mui.wait(signals) retval = mui.request(app, win, flags, title, gadgets, text) id = mui.makeid(4 digit string) path = mui.filerequest(tag1, value1, ...) bool = mui.check(object) object = strarray.new( string1, string2 , ...) strarray.dispose(object) str = strarray.get(object, index) require "muidefs" - * MUIC are class descriptors to use as first parameter for mui.new * MUIO are class descriptors to use as first parameter for mui.make * MUIM are methods for mui.doint, mui.dostr, mui.dobool and mui.doptr * MUIA are attributes * MUIV are values require "muifuncs" require "muiasl"
ASL requesters
To get multiple files from an ASL requester through Zulu function mui.filerequest(), you have to call the function within curly brackets in order to get multiple results
files = { mui.filerequest(.....) }
mui.ASLFR_DoMultiSelect, true
Below there's a snipped of my Lua code (based on Mazze's example)
file = { mui.filerequest( mui.ASLFR_TitleText, "Select one or more files to compress", mui.ASLFR_InitialHeight, 400, mui.ASLFR_InitialWidth, 320, mui.ASLFR_InitialLeftEdge, 40, mui.ASLFR_InitialTopEdge, 20, mui.ASLFR_DoMultiSelect, true, mui.ASLFR_PositiveText, "OK", mui.ASLFR_RejectIcons, true, mui.ASLFR_NegativeText, "CANCEL", mui.ASLFR_InitialFile, "asl.library", mui.ASLFR_InitialDrawer, "libs:" ) } print("Selected File(s)") print(file)
Lua supports currently only static libraries. zulu and siamiga a static libraries. So you have to create a static library and then re-link AmiLua with that library. IIRC there is needed a header file for the connection between Lua and the library.
Libraries
LUA.Filesystem (not implemented yet)
lfs.attributes (filepath [, aname]) e.g. dev, ino, nlink, uid, gid, rdev, access, modification, change, size, blocks, blksize lfs.chdir (path) lfs.lock_dir(path, [seconds_stale]) lfs.currentdir () iter, dir_obj = lfs.dir (path) lfs.lock (filehandle, mode[, start[, length]]) lfs.mkdir (dirname) lfs.rmdir (dirname) lfs.setmode (file, mode) lfs.symlinkattributes (filepath [, aname]) lfs.touch (filepath [, atime [, mtime]]) lfs.unlock (filehandle[, start[, length]])
LUA.Socket
lua.sqlite3
db = sqlite3.open("filename")
db = sqlite3.open_memory()
db:close()
db:exec
db:irows
db:rows
db:cols
db:first_irows
db:first_rows
db:first_cols
db:prepare
db:set_function
LuaSQL frontend docs.
Lua use of first-class functions allow the employment of many powerful techniques from functional programming;
and full lexical scoping allows fine-grained information hiding to enforce the principle of least privilege.
Lua allows programmers to implement namespaces, classes, and other related features using its single table implementation;
Lua does not contain explicit support for inheritance, but allows it to be implemented relatively easily with metatables.
Examples
Shell variables are files which are stored in ENV:.
So you can either use output redirection:
lua:lua checkspace.lua >env:myvariable
Or you can use the file handling functions of Lua to write to ENV:myvariable.
lua:lua checkspace.lua dh0 IF $myvariable GT 18000000 VAL ... ENDIF
require "muidefs" require "muifuncs" function creategui() local button = mui.SimpleButton("_Ok") local text = mui.TextObject(mui.MUIA_Text_Contents, "27cHello world! How are you?") local window = mui.WindowObject( mui.MUIA_Window_Title, "Hello world!", mui.MUIA_Window_RootObject, mui.ScrollgroupObject( mui.MUIA_Scrollgroup_Contents, mui.VirtgroupObject( mui.Child, text, mui.Child, button ) ) ) app = mui.ApplicationObject( mui.MUIA_Application_Window, window ) assert(app:check(), "Cant create application") window:doint(mui.MUIM_Notify, mui.MUIA_Window_CloseRequest, true, app, 2, mui.MUIM_Application_ReturnID, mui.MUIV_Application_ReturnID_Quit) button:doint(mui.MUIM_Notify, mui.MUIA_Pressed, false, app, 2, mui.MUIM_Application_ReturnID, mui.MUIV_Application_ReturnID_Quit) window:set(mui.MUIA_Window_Open, true) end function main() creategui() app:doint(mui.MUIM_Application_Execute) end _, err = pcall(main) if err then print("Error: " .. err) end if app then app:dispose() end
This example lets you enable/disable the group with the two text fields.
require "muidefs" require "muifuncs" ok_id = 1 cancel_id = 2 function creategui() local okbutton = mui.SimpleButton("_Enable") local cancelbutton = mui.SimpleButton("_Disable") local text1 = mui.TextObject(mui.MUIA_Text_Contents, "27cHello world!") local text2 = mui.TextObject(mui.MUIA_Text_Contents, "27cHow are you?") group = mui.VGroup( mui.Child, text1, mui.Child, text2 ) window = mui.WindowObject( mui.MUIA_Window_Title, "Hello world!", mui.MUIA_Window_RootObject, mui.VGroup( mui.Child, group, mui.Child, mui.HGroup( mui.Child, okbutton, mui.Child, cancelbutton ) ) ) app = mui.ApplicationObject( mui.MUIA_Application_Window, window ) assert(app:check(), "Cant create application") window:doint(mui.MUIM_Notify, mui.MUIA_Window_CloseRequest, true, app, 2, mui.MUIM_Application_ReturnID, mui.MUIV_Application_ReturnID_Quit) okbutton:doint(mui.MUIM_Notify, mui.MUIA_Pressed, false, app, 2, mui.MUIM_Application_ReturnID, ok_id) cancelbutton:doint(mui.MUIM_Notify, mui.MUIA_Pressed, false, app, 2, mui.MUIM_Application_ReturnID, cancel_id) window:set(mui.MUIA_Window_Open, true) end function main() creategui() running = true while running do id, signals = app:input() if id == mui.MUIV_Application_ReturnID_Quit then running = false elseif id == ok_id then group:set(mui.MUIA_Disabled, false) elseif id == cancel_id then group:set(mui.MUIA_Disabled, true) end if running then mui.wait(signals) end end end _, err = pcall(main) if err then print("Error: " .. err) end if app then app:dispose() end
-- AntiwordGUI.alua -- AmiLua/Zulu GUI for antiword -- $VER: antiwordgui.alua 1.1 (28.01.2007) -- This is Public Domain software; use at your own risk -- Send bug reports to mrustler@gmx.de require "muidefs" require "muifuncs" require "muiasl" verbose = false outputformats = strarray.new("PDF", "Postscript", "XML", "Text", "Formatted Text") papersizes = strarray.new("10x14", "a3", "a4", "a5", "b4", "b5", "executive", "folio", "legal", "letter", "note", "quarto", "statement", "tabloid") imagelevels = strarray.new("Ghostscript extension", "no images", "PS level 2", "PS level3") ok_id = 1 infile_id = 3 outfile_id = 4 function main() local outputformat_rdio = mui.RadioObject(mui.MUIA_Radio_Entries, outputformats, mui.MUIA_Radio_Active, 3) local papersize_cyc = mui.CycleObject(mui.MUIA_Cycle_Entries, papersizes, mui.MUIA_Cycle_Active, 2) local image_cyc = mui.CycleObject(mui.MUIA_Cycle_Entries, imagelevels, mui.MUIA_Cycle_Active, 2) local ok_btn = mui.SimpleButton("Call antiword") local infile_btn = mui.SimpleButton("Infile") local outfile_btn = mui.SimpleButton("Outfile") local infile_str = mui.StringObject(mui.MUIA_Frame, mui.MUIV_Frame_String) local outfile_str = mui.StringObject(mui.MUIA_Frame, mui.MUIV_Frame_String) local width_str = mui.StringObject(mui.MUIA_Frame, mui.MUIV_Frame_String, mui.MUIA_String_Accept , "0123456879") local hidden_cm = mui.make(mui.MUIO_Checkmark, 0) local removed_cm = mui.make(mui.MUIO_Checkmark, 0) local landscape_cm = mui.make(mui.MUIO_Checkmark, 0) window = mui.WindowObject( mui.MUIA_Window_Title, "Antiword GUI", mui.MUIA_Window_RootObject, mui.VGroup( mui.Child, mui.VGroup( mui.Child, mui.HGroup( mui.MUIA_Frame, mui.MUIV_Frame_Group, mui.MUIA_FrameTitle, "Output format", mui.Child, outputformat_rdio ), mui.Child, mui.ColGroup(2, mui.MUIA_Frame, mui.MUIV_Frame_Group, mui.MUIA_FrameTitle, "Options", mui.Child, mui.Label("Paper size"), mui.Child, papersize_cyc, mui.Child, mui.Label("Image level"), mui.Child, image_cyc, mui.Child, mui.Label("Include removed text"), mui.Child, removed_cm, mui.Child, mui.Label("Include hidden text"), mui.Child, hidden_cm, mui.Child, mui.Label("Landscape"), mui.Child, landscape_cm, mui.Child, mui.Label("Width"), mui.Child, width_str, mui.Child, infile_btn, mui.Child, infile_str, mui.Child, outfile_btn, mui.Child, outfile_str ) ), mui.Child, mui.RectangleObject( mui.MUIA_Rectangle_HBar, true, mui.MUIA_FixHeight, 2 ), mui.Child, ok_btn ) ) app = mui.ApplicationObject( mui.MUIA_Application_Description, "GUI for antiword", mui.MUIA_Application_Window, window ) assert(app:check(), "Cant create application") window:doint(mui.MUIM_Notify, mui.MUIA_Window_CloseRequest, true, app, 2, mui.MUIM_Application_ReturnID, mui.MUIV_Application_ReturnID_Quit) ok_btn:doint(mui.MUIM_Notify, mui.MUIA_Pressed, false, app, 2, mui.MUIM_Application_ReturnID, ok_id) infile_btn:doint(mui.MUIM_Notify, mui.MUIA_Pressed, false, app, 2, mui.MUIM_Application_ReturnID, infile_id) outfile_btn:doint(mui.MUIM_Notify, mui.MUIA_Pressed, false, app, 2, mui.MUIM_Application_ReturnID, outfile_id) window:set(mui.MUIA_Window_Open, true) local running = true local id,signals,file while running do id, signals = app:input() if id == mui.MUIV_Application_ReturnID_Quit then running = false elseif id == ok_id then local command = "antiword:antiword" local format_opt = outputformat_rdio:getint(mui.MUIA_Radio_Active) local papersize_opt = papersizes:get(papersize_cyc:getint(mui.MUIA_Cycle_Active) + 1) local infile_opt = infile_str:getstr(mui.MUIA_String_Contents) local outfile_opt = outfile_str:getstr(mui.MUIA_String_Contents) local width_opt = width_str:getint(mui.MUIA_String_Integer) local landscape_opt = landscape_cm:getint(mui.MUIA_Selected) local imagelevel_opt = image_cyc:getint(mui.MUIA_Cycle_Active) if (infile_opt == "") or (outfile_opt == "") then app:request(window, 0, "Error", "OK", "Input or output file name is missing") else if format_opt == 0 then -- pdf command = command .. " -a" .. papersize_opt .. " -i" .. imagelevel_opt elseif format_opt == 1 then -- ps command = command .. " -p" .. papersize_opt .. " -i" .. imagelevel_opt if landscape_opt == 1 then command = command .. " -L" end elseif format_opt == 2 then -- xml command = command .. " -x db" elseif format_opt == 3 then -- text command = command .. " -t" .. " -w" .. width_opt elseif format_opt == 4 then -- formatted text command = command .. " -f" .. " -w" .. width_opt end if removed_cm:getbool(mui.MUIA_Selected) then command = command .. " -r" end if hidden_cm:getbool(mui.MUIA_Selected) then command = command .. " -s" end command = command .. ' "' .. infile_opt .. '" >"' .. outfile_opt .. '"' if verbose then print (command) end local retval = os.execute(command) if retval > 0 then app:request(window, 0, "Error", "OK", "antiword returned error code " .. retval) end end elseif id == infile_id then file = mui.filerequest( mui.ASLFR_TitleText, "Choose input file", mui.ASLFR_RejectIcons, true, mui.ASLFR_DoPatterns, true ) if file then infile_str:set(mui.MUIA_String_Contents, file) end elseif id == outfile_id then file = mui.filerequest( mui.ASLFR_TitleText, "Choose output file", mui.ASLFR_DoSaveMode, true, mui.ASLFR_RejectIcons, true ) if file then outfile_str:set(mui.MUIA_String_Contents, file) end end if running then mui.wait(signals) end end end _, err = pcall(main) if err then print("Error: " .. err) end if app then app:dispose() end if outputformats then outputformats:dispose() end if papersizes then papersizes:dispose() end if imagelevels then imagelevels:dispose() end
-- zuluwget.alua -- AmiLua/Zune GUI for wget -- $VER: zuluwget.alua 0.3 (7.01.2011) -- This is Public Domain software; use at your own risk -- Send bug reports to tpaul@wp.pl require "muidefs" require "muifuncs" require "muiasl" verbose = false ok_id = 1 http_id = 2 inp_id = 3 dir_id = 4 help_id = 5 canc_id = 6 function main() local language = os.getenv ("language") if language ~= "Italiano" and language ~= "Deutsch" and language ~= "Polski" then language = "english" end strings = {} strings["Italiano"] = {"per", "URL:", "File di input (lista di indirizzi):", "Sfogliare", "Locali cassetto download:", "continua", "console nascondere", "Ulteriori parametri:", "Aiuto (wget --help)", "_Correre", "_Smettere", "Errore", "Indirizzo o file con i mising URL","wget restituito il codice di errore"} strings["Deutsch"] = {"für", "URL:", "Eingabe-Datei (Liste von Adressen):", "_Blättern", "Lokale laden Schublade:","weiter","Konsole verstecken", "Zusätzliche Parameter:", "_Hilfe (wget --help)", "_Laufen", "Be_enden", "Fehler", "keine Adresse oder keine Datei mit URLs", "wget Fehlercode zurückgegeben"} strings["english"] = {"for", "URL:", "Input file (list of URL's):", "_Browse", "Local download drawer:", "continue", "quiet", "Additional parameters:", "_Help (wget --help)", "_Run", "_Quit", "Error", "Adress or file with URL's is mising", "wget returned error code"} strings["Polski"] = {"dla", "URL:", "Plik wej¶ciowy (lista adresów):", "_Przegl±daj", "Katalog docelowy:", "kontynuuj", "ukryj konsolê", "Dodatkowe parametry:", "Po_moc (wget --help)", "_Uruchom", "_Wyjd¼", "B³±d", "Brak adresu lub pliku z adresami URL", "wget zwróci³ kod b³êdu"} local ok_btn = mui.SimpleButton(strings[language][10]) local dir_btn = mui.SimpleButton(strings[language][4]) local inp_btn = mui.SimpleButton(strings[language][4]) local http_str = mui.StringObject(mui.MUIA_Frame, mui.MUIV_Frame_String) local addi_str = mui.StringObject(mui.MUIA_Frame, mui.MUIV_Frame_String) local cont_cm = mui.make(mui.MUIO_Checkmark, 0) local inp_cm = mui.make(mui.MUIO_Checkmark, 0) local quiet_cm = mui.make(mui.MUIO_Checkmark, 0) local info = mui.TextObject(mui.MUIA_Text_Contents, "\27cZune/Lua GUI " .. strings[language][1] .. " wget\nv.0.3 (7.1.2011)") local dir_str = mui.StringObject(mui.MUIA_Frame, mui.MUIV_Frame_String) local inp_str = mui.StringObject(mui.MUIA_Frame, mui.MUIV_Frame_String) local help_btn = mui.SimpleButton(strings[language][9]) local canc_btn = mui.SimpleButton(strings[language][11]) group = mui.VGroup( mui.Child, mui.VGroup( mui.Child, mui.HGroup( mui.MUIA_FixWidth, 300, mui.MUIA_Frame, mui.MUIV_Frame_Group, mui.Child, info ), mui.Child, mui.VGroup( mui.MUIA_Frame, mui.MUIV_Frame_Group, mui.Child, mui.VGroup( mui.Child, mui.Label("\27l" .. strings[language][2]), mui.Child, http_str, mui.Child, mui.Label("\n\27c[-i] " .. strings[language][3]) ), mui.Child, mui.ColGroup(2, mui.MUIA_FixWidth, 300, mui.Child, inp_str, mui.Child, inp_btn ), mui.Child, mui.VGroup( mui.Child, mui.Label("\n\27l[-P] " .. strings[language][5]) ), mui.Child, mui.ColGroup(2, mui.MUIA_FixWidth, 300, mui.Child, dir_str, mui.Child, dir_btn ) ) ), mui.Child, mui.VGroup( mui.MUIA_Frame, mui.MUIV_Frame_Group, mui.Child, mui.ColGroup(4, mui.Child, cont_cm, mui.Child, mui.Label("\27l[-c] " .. strings[language][6]), mui.Child, quiet_cm, mui.Child, mui.Label("\27l[-q] " .. strings[language][7]) ), mui.Child, mui.VGroup( mui.MUIA_FixWidth, 300, mui.Child, mui.Label("\n\27l" .. strings[language][8]), mui.Child, addi_str, mui.Child, help_btn ) ), mui.Child, mui.ColGroup(2, mui.Child, ok_btn, mui.Child, canc_btn ) ) window = mui.WindowObject( mui.MUIA_Window_Title, "ZuluWget", mui.MUIA_Window_RootObject, group ) app = mui.ApplicationObject( mui.MUIA_Application_Description, "GUI for wget", mui.MUIA_Application_Window, window ) assert(app:check(), "Cant create application") window:doint(mui.MUIM_Notify, mui.MUIA_Window_CloseRequest, true, app, 2, mui.MUIM_Application_ReturnID, mui.MUIV_Application_ReturnID_Quit) ok_btn:doint(mui.MUIM_Notify, mui.MUIA_Pressed, false, app, 2, mui.MUIM_Application_ReturnID, ok_id) inp_btn:doint(mui.MUIM_Notify, mui.MUIA_Pressed, false, app, 2, mui.MUIM_Application_ReturnID, inp_id) dir_btn:doint(mui.MUIM_Notify, mui.MUIA_Pressed, false, app, 2, mui.MUIM_Application_ReturnID, dir_id) help_btn:doint(mui.MUIM_Notify, mui.MUIA_Pressed, false, app, 2, mui.MUIM_Application_ReturnID, help_id) canc_btn:doint(mui.MUIM_Notify, mui.MUIA_Pressed, false, app, 2, mui.MUIM_Application_ReturnID, canc_id) window:set(mui.MUIA_Window_Open, true) local running = true local id,signals,http while running do id, signals = app:input() if id == mui.MUIV_Application_ReturnID_Quit then running = false elseif id == canc_id then running = false elseif id == help_id then command = "wget --help /p" os.execute(command) elseif id == ok_id then local command = "wget" local http_opt = http_str:getstr(mui.MUIA_String_Contents) local addi_opt = addi_str:getstr(mui.MUIA_String_Contents) local dir_opt = dir_str:getstr(mui.MUIA_String_Contents) local inp_opt = inp_str:getstr(mui.MUIA_String_Contents) if cont_cm:getbool(mui.MUIA_Selected) then command = command .. " -c" end if quiet_cm:getbool(mui.MUIA_Selected) then command = command .. " -q" end if inp_opt ~= "" or inp_opt ~= "\"\"" then command = command .. " -i " .. "\"" .. inp_opt .. "\"" end if http_opt == "" and (inp_opt == "\"\"" or inp_opt == "") then app:request(window, 0, strings[language][12], "OK", strings[language][13]) else command = command .. " " .. addi_opt .. " -P " .."\"" .. dir_opt .. "wget\" " .. http_opt if verbose then print (command) end local retval = os.execute(command) if retval > 0 then app:request(window, 0, "strings[language][12]", "OK", strings[language][14].. " " .. retval) end end elseif id == dir_id then dir = mui.filerequest( mui.ASLFR_TitleText, strings[language][5], mui.ASLFR_DoSaveMode, true, mui.ASLFR_RejectIcons, true ) if dir then dir_str:set(mui.MUIA_String_Contents, dir) end elseif id == inp_id then inp = mui.filerequest( mui.ASLFR_TitleText, strings[language][3], mui.ASLFR_DoSaveMode, true, mui.ASLFR_RejectIcons, true ) if inp then inp_str:set(mui.MUIA_String_Contents, inp) end end if running then mui.wait(signals) end end end _, err = pcall(main) if err then print(strings[language][12] .. err) end if app then app:dispose() end
require "muidefs" require "muifuncs" require "muiasl" UnitArray = strarray.new("SI","US") LanguageArray = strarray.new("en","fr","de","es","sv","no","ru","pl") Location = "London" Units = 0 Lang = 0 UpdateFreq = 60 Conf_menu_ID = 10 Conf_close_ID = 20 Conf_save_ID = 30 -- Conditions as reported by google weather API (Could be incomplete!) -- "PARTLY SUNNY" -- "SCATTERED THUNDERSTORMS" -- "SHOWERS" -- "SCATTERED SHOWERS" -- "RAIN AND SNOW" -- "OVERCAST" -- "LIGHT SNOW" -- "DRIZZLE" -- "FREEZING DRIZZLE" -- "CHANCE OF RAIN" -- "SUNNY" -- "CLEAR" -- "MOSTLY SUNNY" -- "PARTLY CLOUDY" -- "MOSTLY CLOUDY" -- "CHANCE OF STORM" -- "RAIN" -- "CHANCE OF SNOW" -- "CLOUDY" -- "MIST" -- "STORM" -- "THUNDERSTORM" -- "CHANCE OF TSTORM" -- "SLEET" -- "SNOW" -- "ICY" -- "DUST" -- "FOG" -- "SMOKE" -- "HAZE" -- "FLURRIES" -- "LIGHT RAIN" -- "SNOW SHOWERS" for_City = "" for_CurT = 0 for_CurH = "" for_CurC = "PARTLY SUNNY.PNG" for_CurW = "" for_F1Day = "" for_F1Min = 0 for_F1Max = 0 for_F1Con = "PARTLY SUNNY.PNG" for_F2Day = "" for_F2Min = 0 for_F2Max = 0 for_F2Con = "PARTLY SUNNY.PNG" for_F3Day = "" for_F3Min = 0 for_F3Max = 0 for_F3Con = "PARTLY SUNNY.PNG" for_F4Day = "" for_F4Min = 0 for_F4Max = 0 for_F4Con = "PARTLY SUNNY.PNG" -- only LUA simple XML parser (test) XL = {} function XL:new() local x = {} setmetatable(x, self) self.__index = self return x end function XL._get_attrs(s) local arg = {} string.gsub(s, "(%w+)=([\"'])(.-)%2", function (w, _, a) arg[w] = a end) return arg end function XL:root() for _, v in ipairs(self.xml) do if v.tag then return v end end return nil end function XL:find(x, t) r = {} if x~=nil then for _, v in ipairs(x) do if v.tag and v.tag == t then r[#r+1] = v end end end return r end function XL:from_file(s) local f=io.open(s) local s = f:read('*all') s = string.gsub(s, '_', 'A') self:from_string(s) f:close() end function XL:from_string(s) self.xml = nil -- reset local stack = {} local top = {} table.insert(stack, top) local ni,c,tag,attr, empty local i, j = 1, 1 while true do ni,j,c,tag,attr, empty = string.find(s, "<(%/?)([%w:]+)(.-)(%/?)>", i) if not ni then break end local text = string.sub(s, i, ni-1) if not string.find(text, "^%s*$") then table.insert(top, text) end if empty == "/" then -- empty element tag table.insert(top, {tag=tag, attr=self._get_attrs(attr)}) elseif c == "" then -- start tag top = {tag=tag, attr=self._get_attrs(attr)} table.insert(stack, top) -- new level else -- end tag local toclose = table.remove(stack) -- remove top top = stack[#stack] if #stack < 1 then error("nothing to close with "..tag) end if toclose.tag ~= tag then error("trying to close "..toclose.tag.." with "..tag) end table.insert(top, toclose) end i = j+1 end local text = string.sub(s, i) if not string.find(text, "^%s*$") then table.insert(stack[#stack], text) end if #stack > 1 then error("unclosed "..stack[stack.n].tag) end self.xml = stack[1] end -- Load and Save configuration file function LoadConfig(configfile) -- Loads the configuration file local cf cf = io.open(configfile,"r") if cf == nil then app:request(window, 0 , "Weather Forecast", "OK", "File does not exist") else -- Load Configuration file Location = cf:read("*l") Units = tonumber(cf:read("*l")) Lang = tonumber(cf:read("*l")) UpdateFreq = tonumber(cf:read("*l")) cf:close() end end function SaveConfig(configfile) -- Saves the configuration file local cf if (configfile ~= nil) then cf = io.open(configfile,"w+") if cf ~= nil then cf:write(Location .. "\n") cf:write(tostring(Units) .. "\n") cf:write(tostring(Lang) .. "\n") cf:write(tostring(UpdateFreq) .. "\n") cf:close() end end end -- GUI function menuentry(title, short) -- Creates a single menu entry return mui.MenuitemObject(mui.MUIA_Menuitem_Title, title, mui.MUIA_Menuitem_Shortcut, short) end function set_menu_id(object, id) -- Add a return ID to a menu selecvt event if id == 0 then error("Menu ID must not be 0") else object:doint(mui.MUIM_Notify, mui.MUIA_Menuitem_Trigger, mui.MUIV_EveryTime, app, 2, mui.MUIM_Application_ReturnID, id) end end function Slider(min, max, value) return mui.SliderObject( mui.MUIA_Numeric_Min, min, mui.MUIA_Numeric_Max, max, mui.MUIA_Numeric_Value, value) end function CreateGui() Conf_menu = menuentry("Configure","C") Quit_menu = menuentry("Quit","Q") Menu = mui.MenustripObject( mui.MUIA_Family_Child, mui.MenuObject( mui.MUIA_Menu_Title, "Project", mui.MUIA_Family_Child, Conf_menu, mui.MUIA_Family_Child, Quit_menu ) ) ImCurrent = mui.new(mui.MUIC_Dtpic, mui.MUIA_Dtpic_Name,"Icons/PARTLY SUNNY.png") ImForecast1 = mui.new(mui.MUIC_Dtpic, mui.MUIA_Dtpic_Name,"Icons/PARTLY SUNNY.png") ImForecast2 = mui.new(mui.MUIC_Dtpic, mui.MUIA_Dtpic_Name,"Icons/PARTLY SUNNY.png") ImForecast3 = mui.new(mui.MUIC_Dtpic, mui.MUIA_Dtpic_Name,"Icons/PARTLY SUNNY.png") ImForecast4 = mui.new(mui.MUIC_Dtpic, mui.MUIA_Dtpic_Name,"Icons/PARTLY SUNNY.png") CurT = mui.TextObject(mui.MUIA_Text_Contents, "5 °C", mui.MUIA_Text_PreParse, "\27c") CurH = mui.TextObject(mui.MUIA_Text_Contents, "75%", mui.MUIA_Text_PreParse, "\27c") CurW = mui.TextObject(mui.MUIA_Text_Contents, "NE 20mph", mui.MUIA_Text_PreParse, "\27c") ForT1 = mui.TextObject(mui.MUIA_Text_Contents, "0 to 10", mui.MUIA_Text_PreParse, "\27c") ForT2 = mui.TextObject(mui.MUIA_Text_Contents, "0 to 10", mui.MUIA_Text_PreParse, "\27c") ForT3 = mui.TextObject(mui.MUIA_Text_Contents, "0 to 10", mui.MUIA_Text_PreParse, "\27c") ForT4 = mui.TextObject(mui.MUIA_Text_Contents, "0 to 10", mui.MUIA_Text_PreParse, "\27c") Current = mui.VGroup( mui.MUIA_Frame, mui.MUIV_Frame_Group, mui.MUIA_FrameTitle, "Current", mui.Child, mui.HGroup( mui.Child, mui.RectangleObject(), mui.Child, ImCurrent, mui.Child, mui.RectangleObject() ), mui.Child, CurT ) DayT1 = mui.VGroup( mui.MUIA_Frame, mui.MUIV_Frame_Group, mui.MUIA_FrameTitle, "Today", mui.Child, mui.HGroup( mui.Child, mui.RectangleObject(), mui.Child, ImForecast1, mui.Child, mui.RectangleObject() ), mui.Child, ForT1 ) DayT2 = mui.VGroup( mui.MUIA_Frame, mui.MUIV_Frame_Group, mui.MUIA_FrameTitle, "Day +1", mui.Child, mui.HGroup( mui.Child, mui.RectangleObject(), mui.Child, ImForecast2, mui.Child, mui.RectangleObject() ), mui.Child, ForT2 ) DayT3 = mui.VGroup( mui.MUIA_Frame, mui.MUIV_Frame_Group, mui.MUIA_FrameTitle, "Day +2", mui.Child, mui.HGroup( mui.Child, mui.RectangleObject(), mui.Child, ImForecast3, mui.Child, mui.RectangleObject() ), mui.Child, ForT3 ) DayT4 = mui.VGroup( mui.MUIA_Frame, mui.MUIV_Frame_Group, mui.MUIA_FrameTitle, "Day +3", mui.Child, mui.HGroup( mui.Child, mui.RectangleObject(), mui.Child, ImForecast4, mui.Child, mui.RectangleObject() ), mui.Child, ForT4 ) window = mui.WindowObject( mui.MUIA_Window_Title, "Weather Forecast for " .. Location, mui.MUIA_Window_ID , mui.makeid("WEFO"), mui.WindowContents, mui.VGroup( mui.MUIA_Group_SameWidth, true, mui.Child, mui.HGroup( mui.Child, Current, mui.Child, DayT1, mui.Child, DayT2, mui.Child, DayT3, mui.Child, DayT4 ), mui.Child, mui.HGroup( mui.Child, mui.HGroup( mui.MUIA_Frame, mui.MUIV_Frame_Group, mui.Child, CurH ), mui.Child, mui.HGroup( mui.MUIA_Frame, mui.MUIV_Frame_Group, mui.Child, CurW ) ) ) ) Conf_Loc = mui.StringObject(mui.MUIA_String_Contents, Location) Conf_Lan = mui.CycleObject(mui.MUIA_Cycle_Entries, LanguageArray, mui.MUIA_Cycle_Active, 0) Conf_Uni = mui.CycleObject(mui.MUIA_Cycle_Entries, UnitArray, mui.MUIA_Cycle_Active, 0) Conf_Upd = Slider(1,15,2) SAVE = mui.SimpleButton("_Save") CANCEL = mui.SimpleButton("_Cancel") conf_window = mui.WindowObject( mui.MUIA_Window_Title, "Weather Forecast configuration", mui.MUIA_Window_ID , mui.makeid("WFCO"), mui.WindowContents, mui.VGroup( mui.MUIA_Group_SameWidth, true, mui.Child, mui.ColGroup( 2, mui.Child, mui.Label1("Location") , mui.Child, Conf_Loc, mui.Child, mui.Label1("Language") , mui.Child, Conf_Lan, mui.Child, mui.Label1("Units") , mui.Child, Conf_Uni, mui.Child, mui.Label1("Update Rate") , mui.Child, Conf_Upd ), mui.Child, mui.HGroup( mui.Child, mui.RectangleObject(), mui.Child, SAVE, mui.Child, mui.RectangleObject(), mui.Child, CANCEL, mui.Child, mui.RectangleObject() ) ) ) app = mui.ApplicationObject( mui.MUIA_Application_Title , "Weather Forecast", mui.MUIA_Application_Version , "$VER: Weather Forecast for AROS v1.0", mui.MUIA_Application_Author , "Yannick Erb", mui.MUIA_Application_Description, "Weather Forecast for AROS", mui.MUIA_Application_Base , "AROSWEFO", mui.MUIA_Application_Menustrip , Menu, mui.SubWindow , window, mui.SubWindow , conf_window ) assert(app:check(), "Failed to create Application.") collectgarbage("collect") -- set up actions window:doint(mui.MUIM_Notify, mui.MUIA_Window_CloseRequest, true, app, 2, mui.MUIM_Application_ReturnID, mui.MUIV_Application_ReturnID_Quit) conf_window:doint(mui.MUIM_Notify, mui.MUIA_Window_CloseRequest, true, app, 2, mui.MUIM_Application_ReturnID, Conf_close_ID) CANCEL:doint(mui.MUIM_Notify, mui.MUIA_Pressed, false, app, 2, mui.MUIM_Application_ReturnID, Conf_close_ID) SAVE:doint(mui.MUIM_Notify, mui.MUIA_Pressed, false, app, 2, mui.MUIM_Application_ReturnID, Conf_save_ID) -- set up Menu actions set_menu_id(Conf_menu, Conf_menu_ID) set_menu_id(Quit_menu, mui.MUIV_Application_ReturnID_Quit) end function ConvertTemp(T, inUnit, outUnit) outUnitStr = strarray.get(UnitArray,outUnit+1) if inUnit == outUnitStr then return T end if inUnit == "SI" then return (math.ceil(T*1.8+32)) else return (math.ceil((T-32)/1.8)) end end function ConvertIconName(s) s = string.gsub(s, 'A', ' ') s = string.gsub(s, '/ig/images/weather/', 'Icons/') s = string.gsub(s, '.gif', '.png') return s end function GetForecast() local cf -- Get forecast from google weather API Command = "wget -q http://www.google.com/ig/api?weather=" .. Location .. "&hl=" .. strarray.get(LanguageArray,Lang+1) .. " -O WeatherForecast.xml" os.execute(Command) parser = XL:new() parser:from_file("WeatherForecast.xml") weather = parser:find(parser:root(), "weather") if weather[1] ~= nil then -- Global informations Info = parser:find(weather[1],"forecastAinformation") if Info[1] ~= nil then city = parser:find(Info[1],"city") for_City = city[1].attr.data UnitSys = parser:find(Info[1],"unitAsystem") CurUnits = UnitSys[1].attr.data -- Current Conditions CurrentCond = parser:find(weather[1],"currentAconditions") condition = parser:find(CurrentCond[1],"icon") for_CurC = ConvertIconName(condition[1].attr.data) tempc = parser:find(CurrentCond[1],"tempAc") for_CurT = ConvertTemp(tonumber(tempc[1].attr.data),"SI",Units) humidity = parser:find(CurrentCond[1],"humidity") for_CurH = humidity[1].attr.data windcondition = parser:find(CurrentCond[1],"windAcondition") for_CurW = windcondition[1].attr.data -- Forecasted Conditions forecastconditions = parser:find(weather[1],"forecastAconditions") Day = parser:find(forecastconditions[1],"dayAofAweek") for_F1Day = Day[1].attr.data Min = parser:find(forecastconditions[1],"low") for_F1Min = ConvertTemp(tonumber(Min[1].attr.data),CurUnits,Units) Max = parser:find(forecastconditions[1],"high") for_F1Max = ConvertTemp(tonumber(Max[1].attr.data),CurUnits,Units) condition = parser:find(forecastconditions[1],"icon") for_F1Con = ConvertIconName(condition[1].attr.data) Day = parser:find(forecastconditions[2],"dayAofAweek") for_F2Day = Day[1].attr.data Min = parser:find(forecastconditions[2],"low") for_F2Min = ConvertTemp(tonumber(Min[1].attr.data),CurUnits,Units) Max = parser:find(forecastconditions[2],"high") for_F2Max = ConvertTemp(tonumber(Max[1].attr.data),CurUnits,Units) condition = parser:find(forecastconditions[2],"icon") for_F2Con = ConvertIconName(condition[1].attr.data) Day = parser:find(forecastconditions[3],"dayAofAweek") for_F3Day = Day[1].attr.data Min = parser:find(forecastconditions[3],"low") for_F3Min = ConvertTemp(tonumber(Min[1].attr.data),CurUnits,Units) Max = parser:find(forecastconditions[3],"high") for_F3Max = ConvertTemp(tonumber(Max[1].attr.data),CurUnits,Units) condition = parser:find(forecastconditions[3],"icon") for_F3Con = ConvertIconName(condition[1].attr.data) Day = parser:find(forecastconditions[4],"dayAofAweek") for_F4Day = Day[1].attr.data Min = parser:find(forecastconditions[4],"low") for_F4Min = ConvertTemp(tonumber(Min[1].attr.data),CurUnits,Units) Max = parser:find(forecastconditions[4],"high") for_F4Max = ConvertTemp(tonumber(Max[1].attr.data),CurUnits,Units) condition = parser:find(forecastconditions[4],"icon") for_F4Con = ConvertIconName(condition[1].attr.data) end end end function UpdateDisplay() -- Set pictures mui.set(ImCurrent , mui.MUIA_Dtpic_Name, for_CurC ) mui.set(ImForecast1, mui.MUIA_Dtpic_Name, for_F1Con ) mui.set(ImForecast2, mui.MUIA_Dtpic_Name, for_F2Con ) mui.set(ImForecast3, mui.MUIA_Dtpic_Name, for_F3Con ) mui.set(ImForecast4, mui.MUIA_Dtpic_Name, for_F4Con ) -- Set Current Values mui.set(window, mui.MUIA_Window_Title, "Weather Forecast for " .. for_City) mui.set(CurH, mui.MUIA_Text_Contents, for_CurH) mui.set(CurW, mui.MUIA_Text_Contents, for_CurW) if Units == 0 then mui.set(CurT, mui.MUIA_Text_Contents, tostring(for_CurT) .. " °C") else mui.set(CurT, mui.MUIA_Text_Contents, tostring(for_CurT) .. " °F") end --Set Forecasted Values mui.set(DayT1, mui.MUIA_FrameTitle, for_F1Day) mui.set(DayT2, mui.MUIA_FrameTitle, for_F2Day) mui.set(DayT3, mui.MUIA_FrameTitle, for_F3Day) mui.set(DayT4, mui.MUIA_FrameTitle, for_F4Day) mui.set(ForT1, mui.MUIA_Text_Contents, tostring(for_F1Min) .. " to " .. tostring(for_F1Max)) mui.set(ForT2, mui.MUIA_Text_Contents, tostring(for_F2Min) .. " to " .. tostring(for_F2Max)) mui.set(ForT3, mui.MUIA_Text_Contents, tostring(for_F3Min) .. " to " .. tostring(for_F3Max)) mui.set(ForT4, mui.MUIA_Text_Contents, tostring(for_F4Min) .. " to " .. tostring(for_F4Max)) end function main() -- if WeatherForecat.cfg exists load it cf = io.open("WeatherForecast.cfg","r") if cf ~= nil then LoadConfig("WeatherForecast.cfg") io.close(cf) else SaveConfig("WeatherForecast.cfg") end -- Get initial forecast GetForecast() -- Open Window CreateGui() window:set(mui.MUIA_Window_Open, true) -- Initial update UpdateDisplay() running = true UpdateTimer = os.time() -- Main loop while running do -- Get user inputs id, signals = app:input() if id == mui.MUIV_Application_ReturnID_Quit then -- Exit GUI running = false elseif id == Conf_menu_ID then mui.set(Conf_Loc, mui.MUIA_String_Contents, Location) mui.set(Conf_Lan, mui.MUIA_Cycle_Active, Lang) mui.set(Conf_Uni, mui.MUIA_Cycle_Active, Units) mui.set(Conf_Upd, mui.MUIA_Numeric_Value, UpdateFreq) conf_window:set(mui.MUIA_Window_Open, true) elseif id == Conf_close_ID then conf_window:set(mui.MUIA_Window_Open, false) elseif id == Conf_save_ID then Location = mui.getstr(Conf_Loc, mui.MUIA_String_Contents) Units = mui.getint(Conf_Uni, mui.MUIA_Cycle_Active) Lang = mui.getint(Conf_Lan, mui.MUIA_Cycle_Active) UpdateFreq = mui.getint(Conf_Upd, mui.MUIA_Numeric_Value) SaveConfig("WeatherForecast.cfg") conf_window:set(mui.MUIA_Window_Open, false) -- Update the display GetForecast() UpdateDisplay() UpdateTimer = os.time() end -- update forecast if os.difftime(os.time(),UpdateTimer) > UpdateFreq*60 then GetForecast() UpdateDisplay() UpdateTimer = os.time() end if running then mui.wait(signals) end end end --main() _, err = pcall(main) if err then print("Error: " .. err) end if app then app:dispose() end
How to declare variable located in external file? I need config text file with some variables. How to declare them in main file?
example: config.cfg: v = 5 main.lua: a = 5 v = ????????? a = a + v
loadfile() is what you need.
Issues
Floattext.mui class & Lua Is it possible use this class in Zulu? How to use it to read and show text in window? Could not get it working nicely for WHD_Menu. Ended up using a list object. You can download WHD_Menu from Aros Archive to have a look on how it was achieved. This object is used to display the readme file (variable is CurrentReadMe).
Is it possible to use in this class a function to search requested word in readme file to get line number as result? Each line is an entry in the list. Just parse all entries and search for the string in the field. You'll get directly the line number this way.
This thread multiple file selection isn't working out of the box, you have to create your own class and this is not possible with Lua/Zulu, you have to do it in C.
If you download to other location having space in name still will be problem. How to fix it? Ensure that the path is enclosed in double quotes. From the AROS Exec Download Tool:
local retval = os.execute('c:run sys:utilities/multiview "' .. readme_target .. '"')
Here I have the double quotes only for readme_target because the path to MultiView doesn't have spaces.
changed line to:
local dir_opt = "\"" .. dir_str:getstr(mui.MUIA_String_Contents) .. "\""
How to internationalize Lua script? There is no interface to locale.library in Lua. Just create tables with strings.
strings = {}
strings["deutsch"] = {"Anwendung", "Fenster"}
strings["english"] = {"Application", "Window"}
language = "english"
print(strings[language][1])
So I need to add "choose language" gadget or is there any function to check locale used in system? Could I use external files deutsch.lang, polski.lang etc. or these strings must be in main file? Which way is easier and which is better for these small Lua scripts?
There is an environment variable named "language" and Lua has a function to query environment variables:
language = os.getenv ("language")
if language = NIL
language = "english"
end
In MUI/ZUNE to read a string value from a ListObject the following code applies.
STRPTR *stringValue = NULL; DoMethod(list, MUIM_List_GetEntry, active, &stringValue);
Bear in mind that MUIM_List_GetEntry returns a pointer to a generic entry. A string pointer is only a special case for single column lists.
How does ZULU handle this in Lua, as according to zulu.txt none of the accessor methods (getstr, dostr, etc) appear to match this patten of passing a string pointer as an argument? I'm using local current = listobject:getint(mui.MUIA_List_Active) to query the index of the selected entry. So you could assign a StrArray to mui.MUIA_List_SourceArray then use the mui.MUIA_List_Active result as the index for that array.
Reference
Short Reference - PDF download
Cut down single page reference
http://www.andreas-rozek.de/Lua/index_en_old.html
http://www.tecgraf.puc-rio.br/~diego/professional/luasocket/
LOVE nLOVE
function love.draw()
love.graphics.print("Hello World", 400, 300)
end
- love.load
- love.update
- love.draw
- love.mousepressed
- love.mousereleased
- love.keypressed
- love.keyreleased
- love.focus
- love.quit
require("debug")
"scripts.library.enemy" instead of "scripts/library/enemy"
make a silly error that you didn't notice, like calling newImage, newImageData, newFont, etc. in love.update or love.draw?