Install a moduli(5) manual page.
[dragonfly.git] / contrib / bsdinstaller-1.1.6 / src / backend / lua / lib / MenuItem.lua
1 -- lib/menu.lua
2 -- $Id: MenuItem.lua,v 1.8 2005/03/29 21:04:19 cpressey Exp $
3
4 -- BEGIN lib/menu.lua --
5
6 local POSIX = require("posix")
7 local FileName = require("filename")
8 local App = require("app")
9
10 --[[----------]]--
11 --[[ MenuItem ]]--
12 --[[----------]]--
13
14 -- Global "class" variable.
15 MenuItem = {}
16
17 -- Global (but private) data and functions.
18 local next_id = 0               -- next unique id to create
19 local new_id = function()       -- Create a new unique identifier.
20         next_id = next_id + 1
21         return tostring(next_id)
22 end
23
24 -- Constructor.
25 MenuItem.new = function(tab)
26         local method = {}
27         local id = tab.id or "menu_action_" .. new_id()
28         local name = tab.name or ""
29         local short_desc = tab.short_desc
30         local long_desc = tab.long_desc
31         local effect = tab.effect
32
33         method.to_action = function(item)
34                 return {
35                     id = id,
36                     name = name,
37                     short_desc = short_desc,
38                     long_desc = long_desc,
39                     effect = effect
40                 }
41         end
42
43         return method
44 end
45
46 --[[------]]--
47 --[[ Menu ]]--
48 --[[------]]--
49
50 -- Global "class" variable:
51 Menu = {}
52
53 -- Global (and public) symbolic constants:
54 Menu.CONTINUE = {}
55 Menu.DONE = {}
56
57 -- Global (but private) state variables in the class:
58 local menu_stack = {}   -- stack of the most recently displayed menus
59 local make_exit_item = function()
60         local exit_item_name = "Exit"
61
62         if table.getn(menu_stack) > 0 then
63                 exit_item_name = "Return to " ..
64                     menu_stack[table.getn(menu_stack)]:get_name()
65         end
66
67         return {
68             name = exit_item_name,
69             effect = function()
70                 return Menu.DONE
71             end
72         }
73 end
74
75 -- Create a new menu object instance.
76 Menu.new = function(opt)
77         local method = {}
78         local item = {}
79         local menu_id = opt.menu_id or "menu_form_" .. new_id()
80         local ui = opt.ui or App.ui
81         local name = opt.name or ""
82         local exit_item = opt.exit_item or make_exit_item()
83         local continue_constraint = opt.continue_constraint
84
85         -- Private functions.
86         local map_items_to_actions = function(item)
87                 local k, v
88                 local action = {}
89
90                 for k, v in item do
91                         action[k] = item[k]:to_action()
92                 end
93
94                 return action
95         end
96
97         -- Methods.
98         method.add_item = function(menu, tab)
99                 -- XXX: if tab is already a MenuItem object,
100                 -- just add it, else:
101                 table.insert(item, MenuItem.new(tab))
102         end
103
104         method.get_name = function(menu)
105                 return name
106         end
107
108         --
109         -- Populate this menu with items derived from the Lua script files
110         -- in the given directory.  Each script should end with a return
111         -- statement that returns a table describing the menu item.
112         --
113         method.from_dir = function(menu, dir)
114                 local i, filename, filenames
115         
116                 filenames = POSIX.dir(dir)
117                 table.sort(filenames)
118         
119                 for i, filename in filenames do
120                         local full_file = dir .. "/" .. filename
121         
122                         if filename ~= FileName.basename(App.current_script) and
123                            not FileName.is_dir(full_file) and
124                            string.find(filename, "^[^%.].*%.lua$") then
125                                 local tab = App.run_script(full_file)
126                                 if tab then
127                                         menu:add_item(tab)
128                                 end
129                         end
130                 end
131
132                 menu:add_item(exit_item)
133
134                 return menu
135         end
136
137         --
138         -- Present this menu to the user.
139         --
140         method.present = function(menu)
141                 table.insert(menu_stack, menu)
142                 local response = ui:present({
143                         id = menu_id,
144                         name = opt.name,
145                         short_desc = opt.short_desc,
146                         long_desc = opt.long_desc,
147                         role = "menu",
148                         actions = map_items_to_actions(item)
149                 })
150                 table.remove(menu_stack)
151                 return response
152         end
153
154         method.loop = function(menu)
155                 local result = Menu.CONTINUE
156         
157                 while result == Menu.CONTINUE do
158                         result = menu:present().result
159                         if continue_constraint then
160                                 result = continue_constraint(result)
161                         end
162                 end
163         end
164
165         return method
166 end
167
168 -- Create a new menu object instance automatically from
169 -- the Lua script files in the same directory as the Lua
170 -- script file that invoked this function.
171 Menu.auto = function(opt)
172         local menu = Menu.new(opt)
173
174         menu:from_dir(FileName.dirname(App.current_script))
175         menu:loop()
176 end
177
178 -- END of lib/menu.lua --