-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpresets.lua.1
More file actions
238 lines (234 loc) · 8.66 KB
/
Copy pathpresets.lua.1
File metadata and controls
238 lines (234 loc) · 8.66 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
local Common = ... -- name, json
local State = Common.State
local utils = Common.utils
local cfgpath = Common.cfgpath
local F = far.Flags
-- menu ------------
local function toggleHidden (items)
for _,item in ipairs(items) do
if item.hidden~=nil then
item.hidden = not item.hidden
end
end
end
local optional = {modelsMeta=true, extra_body=true}
local function loadPreset (pathname, itemsNames, cfgInfo)
local export = setmetatable({}, {__index=Common})
local env = {}
local ret = utils.loadLua(pathname,env)(export) -- cfgpath, State, json, utils: .HK, .check, .loadMoon
for k in pairs(env) do
if not itemsNames[k] then
if not optional[k] then
-- Look for corresponding environment param
local key = cfgInfo.env and cfgInfo.env[k]
if not key then return end
env[key] = env[k]
end
env[k] = nil
end
end
return env, ret and ret~=cfgInfo.name:sub(1, ret:len()) and ret
end
local presetsTopic = utils.HelpTopic "Presets"
local function choosePreset (itemsNames, cfgInfo)
local items = {}
local hk = utils.HK.new(".")
local function prepItem (item, pathname)
local success, data, limit = pcall(loadPreset, pathname, itemsNames, cfgInfo)
local err; if not success then err,data = data,nil end
local hidden = (item.fileHidden or not data) and success or nil
local empty = data and not next(data) and "empty"
local extra = err or limit or empty
local text = extra and ("%s [%s]"):format(item.name, extra) or item.name
item.hk = item.hk or hk:iter(hidden)
item.text = item.hk.." "..text
item.grayed = not data or empty
item.data = not empty and data
item.err = err
item.empty = empty
return hidden -- to hide
end
far.RecursiveSearch(cfgpath, "*.preset>>D", function (file, pathname)
table.insert(items,{
name=file.FileName:sub(1,-(".preset"):len()-1),
selected=State.lastPreset==pathname,
pathname=pathname,
fileHidden=file.FileAttributes:find"h",
})
end)
table.sort(items, function(a,b) return a.name:lower() < b.name:lower() end)
for _,item in ipairs(items) do
item.hidden = prepItem(item, item.pathname)
end
hk:finish()
local props = {
Title="Load preset",
Bottom="Ctrl+H, F1, ...",
HelpTopic=presetsTopic,
Id=win.Uuid "6B5DB1AE-B377-4A37-8F73-7DCEBE8C332C",
Flags=F.FMENU_SHOWSHORTBOX +F.FMENU_SHOWSINGLEBOX,
}
local bkeys = "ShiftF4 Ins F4 F5 ShiftF5 F6 ShiftF6 F8 Del CtrlH"
repeat
local item, pos = far.Menu(props, items, bkeys)
if not item then break end -- Esc (or Enter in empty list)
local bk = item.BreakKey
if bk then
item = items[pos]
else -- Enter
State.lastPreset = item.pathname
return item.data
end
if bk=="CtrlH" then
toggleHidden(items)
elseif bk=="ShiftF4" or bk=="Ins" then
local name = far.InputBox(win.Uuid "B45A2CFB-6C4F-49E9-A1EE-4721BB4AAFA4",
Common.name..": create preset", "Enter new name",
"AskAI preset", nil, nil, presetsTopic, F.FIB_NOUSELASTHISTORY)
if name then
name = name:gsub("[/\\]","-")
local pathname = utils.pathjoin(cfgpath, name..".preset")
if F.EEC_MODIFIED==editor.Editor(pathname) then
pos = pos~=0 and bk=="Ins" and pos or 1
local newitem = { name=name, pathname=pathname, }
prepItem(newitem, pathname)
table.insert(items, pos, newitem)
end
end
elseif not item then -- luacheck: ignore 542
-- noop
elseif bk=="F4" then
if F.EEC_MODIFIED==editor.Editor(item.pathname) then
prepItem(item, item.pathname)
end
elseif bk=="F5" or bk=="ShiftF5" then
local name = far.InputBox(win.Uuid "F2366621-CF3A-4953-B297-D85EA0BD0287",
Common.name..": copy preset", "Enter new name",
"AskAI preset", nil, nil, presetsTopic, F.FIB_NOUSELASTHISTORY)
if name then
name = name:gsub("[/\\]","-")
local pathname = utils.pathjoin(cfgpath, name..".preset")
local res,err = win.CopyFile(item.pathname, pathname, true)
if res then
pos = pos+1
local newitem = { name=name, pathname=pathname, }
prepItem(newitem, pathname)
table.insert(items, pos, newitem)
else
err = err:match"^(.-)\r\n$" or err
local msg = ('Cannot copy\n"%s"\nto\n"%s"\n\1\n%s'):format(item.name..".preset", pathname, err)
far.Message(msg,Common.name.." copy preset error",
";Ok", "w", presetsTopic,
win.Uuid "610D35D5-6CBF-4AC4-81D0-99BF791CA3B4")
end
end
elseif bk=="F6" or bk=="ShiftF6" then
local name = far.InputBox(win.Uuid "9374B901-94EC-4FD4-9B01-A73010A344D5",
Common.name..": rename preset", "Enter new name",
"AskAI preset", item.name, nil, presetsTopic, F.FIB_NOUSELASTHISTORY)
if name then
name = name:gsub("[/\\]","-")
local pathname = utils.pathjoin(cfgpath, name..".preset")
local res,err = win.MoveFile(item.pathname, pathname)
if res then
item.name, item.pathname = name, pathname
prepItem(item, pathname)
else
err = err:match"^(.-)\r\n$" or err
local msg = ('Cannot rename\n"%s"\nto\n"%s"\n\1\n%s'):format(item.name..".preset", pathname, err)
far.Message(msg,Common.name.." rename preset error",
";Ok", "w", presetsTopic,
win.Uuid "2559CAD9-82AD-4297-99B9-D3BA144AA3AE")
end
end
elseif bk=="F8" or bk=="Del" then
local ret = far.Message("Do you want to delete the preset?\n"..item.name, Common.name,
"Delete;Cancel", "w", presetsTopic,
win.Uuid "E4486D00-4751-4AAA-97D4-D30A9D618BEF")
if ret==1 and win.DeleteFile(item.pathname) then
table.remove(items, pos)
if pos>#items then pos = pos-1 end
end
end
props.SelectIndex = pos
until false
end
-- main -----------------------
local function _pcall (fn, ...)
local success, res = pcall(fn, ...)
if success then return res end
far.Message(res,"Error in preset",nil,"wl")
end
local DATA = 10
return function (dialog)
local II,cfg = dialog.II, dialog.opts.cfg -- .processSetsHistories, .populateHistory
II:add {F.DI_BUTTON, "&Presets - F5", nil, F.DIF_BTNNOCLOSE +F.DIF_CENTERGROUP,
onclick=function(_,hDlg)
local preset = choosePreset(II, cfg.info)
if preset then
hDlg:EnableRedraw(0)
local secondary,primary = {},{}
for k,v in pairs(preset) do
local item = II[k]
local dependants = cfg.info.sets and cfg.info.sets[k]
if dependants then
local cur = hDlg:GetText(item.idx)
if type(v)=="function" then
v = _pcall(v,cur)
if not v then goto exitPresets end
end
if v==cur then
primary[k] = cur
else
primary[k] = v
hDlg:SetText(item.idx, v)
hDlg:AddHistory(item.idx, v)
dialog.processSetsHistories(hDlg, v, dependants)
end
else
secondary[k] = v
end
end
for k,v in pairs(secondary) do
local item = II[k]
local exV = hDlg:GetText(item.idx)
local isFn = type(v)=="function"
if isFn then
v = _pcall(v, exV, primary)
if not v then goto continue end
end
if type(v)=="table" then
dialog.populateHistory(item, hDlg, v)
v = v[1]
isFn = false
end
local isHidden = cfg.info.hidden and cfg.info.hidden[item.name]
local isService = cfg.info.serviceFields and cfg.info.serviceFields[item.name]
if isHidden or isService or isFn or exV=="" or exV:match" env variable!$" then
hDlg:SetText(item.idx, v)
hDlg:AddHistory(item.idx, v)
end
if item.name=="apidef" and v~=exV then
local name = exV:match"^%s-class (%w+) extends OpenAI"
if name then
Common.restapi.OpenAI[name] = nil
end
end
::continue::
end
if cfg.info.serviceFields then
for _,k in ipairs(cfg.info.serviceFields) do
local item = II[k]
if not preset[k] and item[DATA]~="" then
hDlg:SetText(item.idx, "")
hDlg:AddHistory(item.idx, "")
end
end
end
::exitPresets::
hDlg:EnableRedraw(1)
end
end,
name="btnPreset"
}
end