-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmodels.lua.1
More file actions
118 lines (115 loc) · 3.8 KB
/
models.lua.1
File metadata and controls
118 lines (115 loc) · 3.8 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
local Common = ...
local utils = Common.utils
local F = far.Flags
local function toggleHidden (items)
for _,item in ipairs(items) do
if item.hidden~=nil then
item.hidden = not item.hidden
end
end
end
local function _prep (fn)
return fn and setfenv(fn, setmetatable({},{__index=_G}))
end
local function chooseModel (getModels, args, fromHistory)
local models, argName = getModels(args) --not safe
local _item = models and models[1]
if _item then
local keyId
if type(_item)=="table" then
keyId = models.keyId or _item.id and "id" or _item.name and "name"
or error"Unexpected models list"
end
local filterFn, nameFn = _prep(models.filterFn), _prep(models.nameFn)
local current = args[argName or "model"]
local items = {}
for i,model in ipairs(models) do
local id = nameFn and nameFn(model) or not keyId and model or model[keyId] --not safe
assert(type(id)=="string", "model id must be string")
local hidden = filterFn and not filterFn(model,models) or nil --not safe
items[i] = {
text=id,
hidden=hidden,
grayed=hidden,
selected=id==current,
model=model,
}
end
local props = {
Title=fromHistory and "Models {from history}" or "Available models",
Bottom="Enter to choose",
HelpTopic=utils.HelpTopic"",
Id=win.Uuid "2E982903-4E60-4A44-8E78-2543ED869A63",
Flags=F.FMENU_SHOWSHORTBOX +F.FMENU_SHOWSINGLEBOX,
}
local bkeys = "CtrlH" --undoc
if pcall(require,"le") then
bkeys = bkeys.." F3"
end
table.sort(items, function(a,b) return a.text:lower() < b.text:lower() end)
repeat
local ret,pos = far.Menu(props,items,bkeys)
if ret then
if not ret.BreakKey then
return ret.text, argName
end
props.SelectIndex = pos
local item = items[pos]
if ret.BreakKey=="CtrlH" then
toggleHidden(items)
elseif item and ret.BreakKey=="F3" then
require"le"(item.model)
end
end
until not ret
end
end
return function (dialog) --main
local II,cfg = dialog.II, dialog.opts.cfg
II:add {F.DI_BUTTON, "&Models - F6", nil, F.DIF_BTNNOCLOSE +F.DIF_CENTERGROUP +(cfg.getModels and 0 or F.DIF_DISABLE),
onclick=function(_,hDlg)
local args = {}
for i=1,#cfg.params do
local param = cfg.params[i]
local ii = II[param] and II[param].idx
if ii then
local value = hDlg:GetText(ii)
args[param] = value~="" and value or nil
end
end
local getModels, fromHistory = cfg.getModels
local SHIFT = 16 -- invert action (also for F6/ShiftF6)
local isFar3 = win.GetKeyState or false
local invert = isFar3 and win.GetKeyState(SHIFT)
if not cfg.getModels or (args.modelsMeta=="none" ~= invert) then
if not II.model then return end
if isFar3 then
utils.synchro(function()
local hLst = actl.GetWindowInfo().Id
hLst:EnableRedraw(0)
hLst:ListSort(1)
local pos = hLst:ListFindString(1, {Pattern=args.model or "", Flags=F.LIFIND_EXACTMATCH})
hLst:ListSetCurPos(1, {SelectPos=pos})
hLst:EnableRedraw(1)
end)
hDlg:SetDropdownOpened(II.model.idx, 1)
return
end
fromHistory = true
function getModels ()
return utils.readHistory(II.model.History)
end
end
local success, model, argName = pcall(chooseModel, getModels, args, fromHistory)
if not success then
far.Message(model, "Error fetching models", nil, "wl")
elseif model then
local param = II[argName or "model"]
if not param then return end
hDlg:SetText(param.idx, model)
hDlg:SetFocus(param.idx)
end
end,
name="btnModels"
}
end