Pierwszy przykład pokazał podstawy lua i pozwolił sprawdzić, czy wszystko działa poprawnie. Zróbmy teraz coś bardziej skomplikowanego. Spróbujmy wydrukować listę zdjęć, mających dołączoną “czerwoną” (ang. red) etykietę. Ale… co to jest zdjęcie?

1local Ansel = require "Ansel"
2local debug = require "Ansel.debug"
3print(Ansel.debug.dump(Ansel.database[1]))

Uruchomienie tego kodu wygeneruje wiele komunikatów. Przyjrzymy się im za chwilę, na razie przeanalizujmy sam kod:

Poznaliśmy require Ansel. Nadszedł czas na require Ansel.debug, będące opcjonalną sekcją API, dostarczającą funkcji pomocniczych do debugowania skryptów lua.

Ansel.database jest tablicą, dostarczaną przez API, zawierającą wszystkie zdjęcia z bazy danych. Każdy zapis w bazie jest obiektem zdjęcia. Obiekty zdjęć są złożone i pozwalają na edycję w różnorakie sposoby (wszystkie opisane szczegółowo w sekcji types_dt_lua_image_t podręcznika API). W celu wyświetlenia zdjęć skorzystamy z Ansel.debug.dump, będącym funkcją przyjmującą jako parametr dowolny argument i rekursywnie zwracającą jego wartość. Ponieważ zdjęcia to złożone obiekty, pośrednio odwołujące się do innych złożonych obiektów, wynik bywa duży. Poniżej skrócony przykład wyjścia polecenia.

 1toplevel (userdata,dt_lua_image_t) : /images/100.JPG
 2   publisher (string) : ""
 3   path (string) : "/images"
 4   move (function)
 5   exif_aperture (number) : 2.7999999523163
 6   rights (string) : ""
 7   make_group_leader (function)
 8   exif_crop (number) : 0
 9   duplicate_index (number) : 0
10   is_raw (boolean) : false
11   exif_iso (number) : 200
12   is_ldr (boolean) : true
13   rating (number) : 1
14   description (string) : ""
15   red (boolean) : false
16   get_tags (function)
17   duplicate (function)
18   creator (string) : ""
19   latitude (nil)
20   blue (boolean) : false
21   exif_datetime_taken (string) : "2014:04:27 14:10:27"
22   exif_maker (string) : "Panasonic"
23   drop_cache (function)
24   title (string) : ""
25   reset (function)
26   create_style (function)
27   apply_style (function)
28   film (userdata,dt_lua_film_t) : /images
29      1 (userdata,dt_lua_image_t): .toplevel
30      [......]
31   exif_exposure (number) : 0.0062500000931323
32   exif_lens (string) : ""
33   detach_tag (function): toplevel.film.2.detach_tag
34   exif_focal_length (number) : 4.5
35   get_group_members (function): toplevel.film.2.get_group_members
36   id (number) : 1
37   group_with (function): toplevel.film.2.group_with
38   delete (function): toplevel.film.2.delete
39   purple (boolean) : false
40   is_hdr (boolean) : false
41   exif_model (string) : "DMC-FZ200"
42   green (boolean) : false
43   yellow (boolean) : false
44   longitude (nil)
45   filename (string) : "100.JPG"
46   width (number) : 945
47   attach_tag (function): toplevel.film.2.attach_tag
48   exif_focus_distance (number) : 0
49   height (number) : 648
50   local_copy (boolean) : false
51   copy (function): toplevel.film.2.copy
52   group_leader (userdata,dt_lua_image_t): .toplevel

Jak widać, obraz ma wiele pól, opisujących wszystkie jego cechy. W tym przykładzie interesuje nas etykieta “red” (ang. czerwony). To pole jest typu logicznego, a dokumentacja mówi nam, że może być ono do zapisu. Teraz potrzebujemy tylko znaleźć wszystkie zdjęcia z tym polem i wydrukować je.

1Ansel = require "Ansel"
2for _,v in ipairs(Ansel.database) do
3  if v.red then
4    print(tostring(v))
5  end
6end

Kod powinien być łatwy do zrozumienia, ale zawiera też kilka aspektów lua, nad którymi warto się zatrzymać.

  • ipairs is a standard lua function that will iterate through all numeric indices of a table. We use it here because Ansel’s database has non-numeric indices which are functions to manipulate the database itself (adding or deleting images, for example).

  • Iterating through a table will return both the key and the value used. It is conventional in lua to use a variable named “_” to store values that we don’t care about.

  • Note that we use the standard lua function tostring here and not the Ansel-specific Ansel.debug.dump. The standard function will return a name for the object whereas the debug function will print the content. The debug function would be too verbose here. Once again, it is a great debug tool but it should not be used for anything else.