Перший приклад показав нам самі основи lua і дозволив перевірити, чи все працює належним чином. А тепер давайте зробимо щось дещо складніше. Спробуємо надрукувати список зображень, до яких прикріплена “червона” позначка. Але перш за все, що таке зображення?

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

Запуск наведеного вище коду дасть багато результату на виході. Ми розглянемо це за мить, але спочатку розглянемо сам код.

We know about require Ansel. Here, we need to separately require Ansel.debug which is an optional section of the API that provides helper functions to help debug lua scripts.

Ansel.database is a table provided by the API that contains all images in the library database. Each entry in the database is an image object. Image objects are complex objects that allow you to manipulate your image in various ways (all documented in the types_dt_lua_image_t section of the API manual). To display our images, we use Ansel.debug.dump which is a function that will take anything as its parameter and recursively dump its content. Since images are complex objects that indirectly reference other complex objects, the resulting output is huge. Below is a cut down example of the output.

 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

Як ми бачимо, зображення має велику кількість полів, які надають всю інформацію про нього. Тут нас цікавить “червона” позначка. Це поле має логічний тип даних, і документація повідомляє нам, що його можна записати. Тепер нам просто потрібно знайти всі зображення з цим полем і роздрукувати їх:

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

На цьому етапі цей код повинен бути досить простим для розуміння, але він містить кілька цікавих аспектів lua, які варто виділити:

  • 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.