-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfilepicker.lua
More file actions
415 lines (380 loc) · 11 KB
/
filepicker.lua
File metadata and controls
415 lines (380 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
local core = require "core"
local common = require "core.common"
local style = require "core.style"
local Widget = require "widget"
local Button = require "widget.button"
local Label = require "widget.label"
---@class widget.filepicker : widget
---@overload fun(parent?:widget, path?:string):widget.filepicker
---@field public pick_mode integer
---@field public filters table<integer,string>
---@field private path string
---@field private file widget.label
---@field private textbox widget.textbox
---@field private button widget.button
local FilePicker = Widget:extend()
---Operation modes for the file picker.
---@type table<string,integer>
FilePicker.mode = {
---Opens file browser the selected file does not has to exist.
FILE = 1,
---Opens file browser the selected file has to exist.
FILE_EXISTS = 2,
---Opens directory browser the selected directory does not has to exist.
DIRECTORY = 4,
---Opens directory browser the selected directory has to exist.
DIRECTORY_EXISTS = 8
}
---@param text string
---@return string[]
local function suggest_directory(text)
text = common.home_expand(text)
if not common.is_absolute_path(text) then
text = core.root_project().path .. PATHSEP .. text
end
return common.home_encode_list(
common.dir_path_suggest(text, core.root_project().path)
)
end
---@param path string
local function check_directory_path(path)
local abs_path = core.project_absolute_path(path)
local info = abs_path and system.get_file_info(abs_path)
if not info or info.type ~= 'dir' then return nil end
return abs_path
end
---@param str string
---@param find string
---@param replace string
local function str_replace(str, find, replace)
local start, ending = str:find(find, 1, true)
if start == 1 then
return replace .. str:sub(ending + 1)
else
return str:sub(1, start - 1) .. replace .. str:sub(ending + 1)
end
end
---@alias widget.filepicker.modes
---| `FilePicker.mode.FILE`
---| `FilePicker.mode.FILE_EXISTS`
---| `FilePicker.mode.DIRECTORY`
---| `FilePicker.mode.DIRECTORY_EXISTS`
---Constructor
---@param parent widget
---@param path? string
function FilePicker:new(parent, path)
FilePicker.super.new(self, parent)
local this = self
self.type_name = "widget.filepicker"
self.filters = {}
self.border.width = 0
self.pick_mode = FilePicker.mode.FILE
self.file = Label(self, "")
self.file.clickable = true
self.file:set_border_width(1)
function self.file:on_click(button)
if button == "left" then
this:show_picker()
end
end
function self.file:on_mouse_enter(...)
Label.super.on_mouse_enter(self, ...)
self.border.color = style.caret
end
function self.file:on_mouse_leave(...)
Label.super.on_mouse_leave(self, ...)
self.border.color = style.text
end
self.button = Button(self, "")
self.button:set_icon("D")
self.button:set_tooltip("open file browser")
function self.button:on_click(button)
if button == "left" then
this:show_picker()
end
end
local label_width = self.file:get_width()
if label_width <= 10 then
label_width = 200 + (self.file.border.width * 2)
self.file:set_size(
200, self.button:get_height() - self.button.border.width * 2
)
end
self:set_size(
label_width + self.button:get_width(),
math.max(self.file:get_height(), self.button:get_height())
)
self:set_path(path)
end
---Set the filepicker size
---@param width? number
---@param height? number
function FilePicker:set_size(width, height)
FilePicker.super.set_size(self, width, height)
self.file:set_position(0, 0)
self.file:set_size(
self:get_width() - self.button:get_width(),
self.button:get_height()
)
self.button:set_position(self.file:get_right(), 0)
self.size.y = math.max(
self.file:get_height(),
self.button:get_height()
-- something is off on calculation since adding border width should not
-- be needed to display whole rendered control at all...
) + self.button.border.width
end
---Add a lua pattern to the filters list
---@param pattern string
function FilePicker:add_filter(pattern)
table.insert(self.filters, pattern)
end
---Clear the filters list
function FilePicker:clear_filters()
self.filters = {}
end
---Set the operation mode for the file picker.
---@param mode widget.filepicker.modes | string | integer
function FilePicker:set_mode(mode)
if type(mode) == "string" then
---@type integer
local intmode = FilePicker.mode[mode:upper()]
self.pick_mode = intmode
else
self.pick_mode = mode
end
end
---Set the full path including directory and filename.
---@param path? string
function FilePicker:set_path(path)
if path then
self.path = path or ""
if common.path_belongs_to(path, core.root_project().path) then
self.file.label = path ~= "" and
common.relative_path(core.root_project().path, path)
or
""
else
self.file.label = path
end
else
self.path = ""
self.file.label = ""
end
end
---Get the full path including directory and filename.
---@return string | nil
function FilePicker:get_path()
if self.path ~= "" then
return self.path
end
return nil
end
---Get the full path relative to current project dir or absolute if it doesn't
---belongs to the current project directory.
---@return string
function FilePicker:get_relative_path()
if
self.path ~= ""
and
common.path_belongs_to(self.path, core.root_project().path)
then
return common.relative_path(core.root_project().path, self.path)
end
return self.path or ""
end
---Set the filename part only.
---@param name string
function FilePicker:set_filename(name)
local dir_part = common.dirname(self.path)
if dir_part then
self:set_path(dir_part .. PATHSEP .. name)
else
self:set_path(name)
end
end
---Get the filename part only.
---@return string | nil
function FilePicker:get_filename()
local dir_part = common.dirname(self.path)
if dir_part then
local filename = str_replace(self.path, dir_part .. PATHSEP, "")
return filename
elseif self.path ~= "" then
return self.path
end
return nil
end
---Set the directory part only.
---@param dir string
function FilePicker:set_directory(dir)
local filename = self:get_filename()
if filename then
self:set_path(dir:gsub("[\\/]$", "") .. PATHSEP .. filename)
else
self:set_path(dir:gsub("[\\/]$", ""))
end
end
---Get the directory part only.
---@return string | nil
function FilePicker:get_directory()
if self.path ~= "" then
local dir_part = common.dirname(self.path)
if dir_part then return dir_part end
end
return nil
end
---Filter a list of directories by applying currently set filters.
---@param self widget.filepicker
---@param list table<integer, string>
---@return table<integer,string>
local function filter(self, list)
for i=1, #list do
local path = common.home_expand(list[i])
if common.path_belongs_to(path, core.root_project().path) then
list[i] = common.relative_path(core.root_project().path, path)
end
end
if #self.filters > 0 then
local new_list = {}
for _, value in ipairs(list) do
if common.match_pattern(value, self.filters) then
table.insert(new_list, value)
elseif
self.pick_mode == FilePicker.mode.FILE
or
self.pick_mode == FilePicker.mode.FILE_EXISTS
then
local path = common.home_expand(value)
local abs_path = check_directory_path(path)
if abs_path then
table.insert(new_list, value)
end
end
end
return new_list
end
return list
end
---@param self widget.filepicker
local function show_file_picker(self)
core.command_view:enter("Choose File", {
text = self:get_relative_path(),
submit = function(text)
---@type string
local filename = text
local dirname = common.dirname(common.home_expand(text))
if dirname then
filename = common.home_expand(text)
filename = core.project_absolute_path(dirname)
.. PATHSEP
.. str_replace(filename, dirname .. PATHSEP, "")
elseif filename ~= "" then
filename = core.root_project().path .. PATHSEP .. filename
end
self:set_path(filename)
self:on_change(filename ~= "" and filename or nil)
end,
suggest = function (text)
return filter(
self,
common.home_encode_list(
common.path_suggest(
common.home_expand(text), core.root_project().path
)
)
)
end,
validate = function(text)
if
#self.filters > 0 and text ~= ""
and
not common.match_pattern(text, self.filters)
then
core.error(
"File does not match the filters: %s",
table.concat(self.filters, ", ")
)
return false
end
local filename = common.home_expand(text)
local path_stat, err = system.get_file_info(filename)
if path_stat and path_stat.type == 'dir' then
core.error("Cannot open %s, is a folder", text)
return false
end
if self.pick_mode == FilePicker.mode.FILE_EXISTS then
if not path_stat then
core.error("Cannot open file %s: %s", text, err)
return false
end
else
local dirname = common.dirname(filename)
local dir_stat = dirname and system.get_file_info(dirname)
if dirname and not dir_stat then
core.error("Directory does not exists: %s", dirname)
return false
end
end
return true
end,
})
end
---@param self widget.filepicker
local function show_dir_picker(self)
core.command_view:enter("Choose Directory", {
text = self:get_relative_path(),
submit = function(text)
local path = common.home_expand(text)
local abs_path = check_directory_path(path)
self:set_path(abs_path or text)
self:on_change(abs_path or (text ~= "" and text or nil))
end,
suggest = function(text)
return filter(self, suggest_directory(text))
end,
validate = function(text)
if
#self.filters > 0 and text ~= ""
and
not common.match_pattern(text, self.filters)
then
core.error(
"Directory does not match the filters: %s",
table.concat(self.filters, ", ")
)
return false
end
if self.pick_mode == FilePicker.mode.DIRECTORY_EXISTS then
local path = common.home_expand(text)
local abs_path = check_directory_path(path)
if not abs_path then
core.error("Cannot open directory %q", path)
return false
end
end
return true
end
})
end
---Show the command view file or directory browser depending on the
---current file picker mode.
function FilePicker:show_picker()
if
self.pick_mode == FilePicker.mode.FILE
or
self.pick_mode == FilePicker.mode.FILE_EXISTS
then
show_file_picker(self)
else
show_dir_picker(self)
end
end
function FilePicker:update_size_position()
FilePicker.super.update_size_position(self)
self:set_size(
self.file:get_width() + self.button:get_width(),
self.button:get_height()
)
end
return FilePicker