Reduce differences with root_skels in contrib.
[dragonfly.git] / contrib / bsdinstaller-1.1.6 / src / lib / lua / dfui / dfui.lua
blob9ab353c3a6fbdbf22f54c6df12c5962fb3c95aa6
1 -- $Id: dfui.lua,v 1.32 2005/04/03 20:46:01 cpressey Exp $
2 -- Wrapper/helper/extra abstractions for DFUI.
4 --[[------]]--
5 --[[ DFUI ]]--
6 --[[------]]--
8 --
9 -- This is a wrapper object around DFUI.Connection and DFUI.Progress,
10 -- intended to be used as a "UI adapter" for the App object.
13 module("dfui")
15 DFUI = require "ldfui"
16 local POSIX = require "posix"
18 DFUI.log = function(fmt, ...)
19 print(string.format(fmt, unpack(arg)))
20 end
22 DFUI.new = function(tab)
23 local dfui = {}
24 local transport = tab.transport or "tcp"
25 local rendezvous = tab.rendezvous or "9999"
26 local connection
28 dfui.start = function(dfui)
29 connection = DFUI.Connection.new(transport, rendezvous)
30 if connection:start() == 0 then
31 connection:stop()
32 DFUI.log("Could not establish DFUI connection " ..
33 " on %s:%s", transport, rendezvous)
34 return false
35 end
36 DFUI.log("DFUI connection on %s:%s successfully established",
37 transport, rendezvous)
38 return true
39 end
41 dfui.stop = function(dfui)
42 return connection:stop()
43 end
45 dfui.present = function(dfui, tab)
46 return connection:present(tab)
47 end
50 -- Handy dialogs. (Perhaps a bit too handy?)
53 dfui.inform = function(dfui, msg)
54 return connection:present({
55 id = "inform",
56 name = "Information",
57 short_desc = msg,
58 role = "informative",
59 actions = {
61 id = "ok",
62 name = "OK"
66 end
68 dfui.confirm = function(dfui, msg)
69 return connection:present({
70 id = "confirm",
71 name = "Are you SURE?",
72 short_desc = msg,
73 role = "alert",
74 actions = {
76 id = "ok",
77 name = "OK"
80 id = "cancel",
81 name = "Cancel"
84 }).action_id == "ok"
85 end
87 dfui.select = function(dfui, msg, map)
88 local action = {}
89 local consequence = {}
90 local id_num = 0
91 local k, v
93 for k, v in map do
94 table.insert(action, {
95 id = tostring(id_num),
96 name = k
98 consequence[tostring(id_num)] = v
99 id_num = id_num + 1
102 return consequence[connection:present({
103 id = "select",
104 name = "Please Select",
105 short_desc = msg,
106 role = "informative",
107 actions = action
108 }).action_id]
111 dfui.select_file = function(dfui, tab)
112 local title = tab.title or "Select File"
113 local short_desc = tab.short_desc or title
114 local long_desc = tab.long_desc or ""
115 local cancel_desc = tab.cancel_desc or "Cancel"
116 local dir = assert(tab.dir)
117 local ext = tab.ext or nil
118 local files, i, filename
120 local form = {
121 id = "select_file",
122 name = title,
123 short_desc = short_desc,
124 long_desc = long_desc,
126 role = "menu",
128 actions = {}
131 files = POSIX.dir(dir)
132 table.sort(files)
133 for i, filename in files do
134 if not ext or string.find(filename, "%." .. ext .. "$") then
135 table.insert(form.actions, {
136 id = filename,
137 name = filename
142 table.insert(form.actions, {
143 id = "cancel",
144 name = cancel_desc
147 return connection:present(form).action_id
151 -- Constructor within a constructor, here...
153 dfui.new_progress_bar = function(dfui, tab)
154 local method = {}
155 local pr
156 local title = tab.title or "Working..."
157 local short_desc = tab.short_desc or title
158 local long_desc = tab.long_desc or ""
159 local amount = 0
161 pr = DFUI.Progress.new(connection,
162 title, short_desc, long_desc, amount)
164 method.start = function(method)
165 return pr:start()
168 method.set_amount = function(method, new_amount)
169 return pr:set_amount(new_amount)
172 method.set_short_desc = function(method, new_short_desc)
173 return pr:set_short_desc(new_short_desc)
176 method.update = function(method)
177 return pr:update()
180 method.stop = function(method)
181 return pr:stop()
184 return method
187 return dfui
190 return DFUI