@@ -25,9 +25,20 @@ local widgets =require 'gui.widgets'
25
25
local guiScript = require ' gui.script'
26
26
local args = {... }
27
27
28
+ find_funcs = find_funcs or (function ()
29
+ local t = {}
30
+ for k in pairs (df ) do
31
+ pcall (function ()
32
+ t [k ] = df [k ].find
33
+ end )
34
+ end
35
+ return t
36
+ end )()
37
+
28
38
local keybindings = {
29
39
offset = {key = " CUSTOM_ALT_O" ,desc = " Show current items offset" },
30
40
find = {key = " CUSTOM_F" ,desc = " Find a value by entering a predicate" },
41
+ find_id = {key = " CUSTOM_I" ,desc = " Find object with this ID" },
31
42
lua_set = {key = " CUSTOM_ALT_S" ,desc = " Set by using a lua function" },
32
43
insert = {key = " CUSTOM_ALT_I" ,desc = " Insert a new value to the vector" },
33
44
delete = {key = " CUSTOM_ALT_D" ,desc = " Delete selected entry" },
@@ -62,6 +73,24 @@ function getTargetFromScreens()
62
73
end
63
74
return my_trg
64
75
end
76
+ function search_relevance (search , candidate )
77
+ local function clean (str )
78
+ return ' ' .. str :lower ():gsub (' [^a-z0-9]' ,' ' ) .. ' '
79
+ end
80
+ search = clean (search )
81
+ candidate = clean (candidate )
82
+ local ret = 0
83
+ while # search > 0 do
84
+ local pos = candidate :find (search :sub (1 , 1 ), 1 , true )
85
+ if pos then
86
+ ret = ret + (# search - pos )
87
+ candidate = candidate :sub (pos + 1 )
88
+ end
89
+ search = search :sub (2 )
90
+ end
91
+ return ret
92
+ end
93
+
65
94
66
95
GmEditorUi = defclass (GmEditorUi , gui .FramedScreen )
67
96
GmEditorUi .ATTRS = {
@@ -135,7 +164,7 @@ function GmEditorUi:find(test)
135
164
136
165
local e ,what = load (" return function(k,v) return " .. test .. " end" )
137
166
if e == nil then
138
- dialog .showMessage (" Error!" ," function failed to compile\n " .. what ,COLOR_RED )
167
+ dialog .showMessage (" Error!" ," function failed to compile\n " .. what ,COLOR_LIGHTRED )
139
168
end
140
169
141
170
if trg .target and trg .target ._kind and trg .target ._kind == " container" then
@@ -157,6 +186,29 @@ function GmEditorUi:find(test)
157
186
end
158
187
end
159
188
end
189
+ function GmEditorUi :find_id ()
190
+ local key = self :getSelectedKey ()
191
+ local id = tonumber (self :getSelectedValue ())
192
+ if not id then return end
193
+ local opts = {}
194
+ for name , func in pairs (find_funcs ) do
195
+ table.insert (opts , {text = name , callback = func , weight = search_relevance (key , name )})
196
+ end
197
+ table.sort (opts , function (a , b )
198
+ return a .weight > b .weight
199
+ end )
200
+ guiScript .start (function ()
201
+ local ret ,idx ,choice = guiScript .showListPrompt (" Choose type:" ,nil ,3 ,opts ,nil ,true )
202
+ if ret then
203
+ local obj = choice .callback (id )
204
+ if obj then
205
+ self :pushTarget (obj )
206
+ else
207
+ dialog .showMessage (" Error!" , (' %s with ID %d not found' ):format (choice .text , id ), COLOR_LIGHTRED )
208
+ end
209
+ end
210
+ end )
211
+ end
160
212
function GmEditorUi :insertNew (typename )
161
213
local tp = typename
162
214
if typename == nil then
@@ -165,7 +217,7 @@ function GmEditorUi:insertNew(typename)
165
217
end
166
218
local ntype = df [tp ]
167
219
if ntype == nil then
168
- dialog .showMessage (" Error!" ," Type '" .. tp .. " not found" ,COLOR_RED )
220
+ dialog .showMessage (" Error!" ," Type '" .. tp .. " not found" ,COLOR_LIGHTRED )
169
221
return
170
222
end
171
223
@@ -187,20 +239,23 @@ end
187
239
function GmEditorUi :getSelectedKey ()
188
240
return self :currentTarget ().keys [self .subviews .list_main :getSelected ()]
189
241
end
242
+ function GmEditorUi :getSelectedValue ()
243
+ return self :currentTarget ().target [self :getSelectedKey ()]
244
+ end
190
245
function GmEditorUi :currentTarget ()
191
246
return self .stack [# self .stack ]
192
247
end
193
248
function GmEditorUi :getSelectedEnumType ()
194
249
local trg = self :currentTarget ()
195
250
local trg_key = trg .keys [self .subviews .list_main :getSelected ()]
196
-
251
+
197
252
local ok ,ret = pcall (function () -- super safe way to check if the field has enum
198
253
return trg .target ._field == nil or trg .target :_field (trg_key )== nil
199
254
end )
200
255
if not ok or ret == true then
201
256
return nil
202
257
end
203
-
258
+
204
259
local enum = trg .target :_field (trg_key )._type
205
260
if enum ._kind == " enum-type" then
206
261
return enum
@@ -298,7 +353,7 @@ function GmEditorUi:set(key,input)
298
353
end
299
354
local e ,what = load (" return function(v) return " .. input .. " end" )
300
355
if e == nil then
301
- dialog .showMessage (" Error!" ," function failed to compile\n " .. what ,COLOR_RED )
356
+ dialog .showMessage (" Error!" ," function failed to compile\n " .. what ,COLOR_LIGHTRED )
302
357
return
303
358
end
304
359
trg .target [key ]= e ()(trg )
@@ -331,6 +386,8 @@ function GmEditorUi:onInput(keys)
331
386
self :openOffseted (self .subviews .list_main :getSelected ())
332
387
elseif keys [keybindings .find .key ] then
333
388
self :find ()
389
+ elseif keys [keybindings .find_id .key ] then
390
+ self :find_id ()
334
391
elseif keys [keybindings .lua_set .key ] then
335
392
self :set (self :getSelectedKey ())
336
393
elseif keys [keybindings .insert .key ] then -- insert
0 commit comments