-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollback.lua
More file actions
309 lines (259 loc) · 7.16 KB
/
rollback.lua
File metadata and controls
309 lines (259 loc) · 7.16 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
require "menu"
require "windows"
local preferences = inline:getDefaultSharedPreferences()
local lastString
local lastState
local results = {}
local cursor = 1
local offset = 1
local buffer = {}
local viewer = false
local DEFAULT_BUFFER_SIZE = 2000
local bufferSize = preferences:getInt("rollback_buffer_size", DEFAULT_BUFFER_SIZE)
if bufferSize < 1 then
bufferSize = DEFAULT_BUFFER_SIZE
end
local function startsWith(text, start)
return text:sub(1, #start) == start
end
local function watcher(input)
local text = inline:getText(input)
if text == nil or text == "" or viewer then
return
end
if lastString ~= nil and startsWith(text, lastString) then
text = text:sub(#lastString + 1, #text)
lastString = lastString .. text
buffer[#buffer] = buffer[#buffer] .. text
else
lastString = text
buffer[#buffer + 1] = text
if #buffer > bufferSize then
table.remove(buffer, 1)
end
end
end
local createMenu
local function createSetStepMenu(_, query)
local result = { "Step: " }
for i = 1, 20 do
table.insert(result, {
caption = "[" .. i .. "]",
action = function(input, query_)
offset = i
createMenu(input, query_)
end
})
table.insert(result, " ")
end
menu.create(query, result, createMenu)
end
function createMenu(_, query)
if cursor > #results then
cursor = 1
elseif cursor < 1 then
cursor = #results
end
viewer = true
local offsetText = offset == 1 and "" or tostring(offset)
local result = {
results[cursor],
"\n",
tostring(cursor),
"/",
tostring(#results),
" ",
{
caption = "[ <" .. offsetText .. " ]",
action = function(input_, query_)
cursor = cursor - offset
createMenu(input_, query_)
end,
},
" ",
{
caption = "[ " .. offsetText .. "> ]",
action = function(input_, query_)
cursor = cursor + offset
createMenu(input_, query_)
end
},
" ",
{
caption = "[Set step]",
action = function(input_, query_)
createSetStepMenu(input_, query_)
end
},
" ",
{
caption = "[Ok]",
action = function(input_, _)
viewer = false
inline:setText(input_, results[cursor])
end
}
}
menu.create(query, result, function(input_)
viewer = false
inline:setText(input_, lastState)
end)
end
local function findResults(query)
if query:getArgs() == "" then
results = buffer
else
results = {}
for _, v in ipairs(buffer) do
if v:find(query:getArgs()) ~= nil then
results[#results + 1] = v
end
end
end
if #results == 0 then
query:answer("Not found")
return false
end
cursor = #results
return true
end
local function back(input, query)
if not findResults(query) then
return
end
lastState = query:replaceExpression("")
createMenu(input, query)
end
local function showCurrentResult(input)
if cursor > #results then
cursor = 1
elseif cursor < 1 then
cursor = #results
end
inline:setText(input, results[cursor] .. "\n[" .. cursor .. "/" .. #results .. "]")
end
local function fback(input, query)
if not findResults(query) then
return
end
showCurrentResult(input)
viewer = true
windows.createAligned(input, {
noBackground = true,
position = "below",
onClose = function()
viewer = false
end
}, function(ui)
local offsetText = offset == 1 and "" or " " .. offset
local undo = ui.button("Undo" .. offsetText, function()
cursor = cursor - offset
showCurrentResult(input)
end)
local redo = ui.button("Redo" .. offsetText, function()
cursor = cursor + offset
showCurrentResult(input)
end)
local ok = ui.button("Ok", function()
inline:setText(input, results[cursor])
ui:close()
end)
undo:setMinimumWidth(0)
redo:setMinimumWidth(0)
ok:setMinimumWidth(0)
undo:setMinWidth(0)
redo:setMinWidth(0)
ok:setMinWidth(0)
local setSteps = ui.seekBar(nil, 19)
:setOnProgressChanged(function(i)
offset = i + 1
local offsetText_ = offset == 1 and "" or offset
undo:setText("Undo " .. offsetText_)
redo:setText("Redo " .. offsetText_)
end)
setSteps:setVisibility(setSteps.GONE)
local function showSteps()
setSteps:setVisibility(setSteps.VISIBLE)
end
ui.setOnLongClickListener(redo, showSteps)
ui.setOnLongClickListener(undo, showSteps)
return {
{
undo,
ui.spacer(4),
redo,
ui.spacer(4),
ok,
},
ui.spacer(4),
setSteps
}
end)
end
local function ftime(input, query)
if not findResults(query) then
return
end
viewer = true
lastState = query:replaceExpression("")
local box = windows.getBoundsInScreen(input)
windows.createAligned(input, {
position = "below",
onClose = function()
viewer = false
end
}, function(ui)
local seekBar = ui.seekBar(nil, #results)
:setOnProgressChanged(function(i)
cursor = i
inline:setText(input, results[cursor])
end)
seekBar:setProgress(#results)
return {
seekBar,
ui.spacer(8),
{
ui.button("Cancel", function()
inline:setText(input, lastState)
ui:close()
end),
ui.spacer(8),
ui.button("Apply", function()
ui:close()
end)
}
}
end)
end
local function finder()
if viewer then
return function()
end
end
end
local function getPreferences(prefs)
return {
prefs.textInput("rollback_buffer_size", "Buffer size (edits count)")
:setDefault(DEFAULT_BUFFER_SIZE)
:setListener(function(s)
if bufferSize > 1 then
bufferSize = tonumber(s)
end
end)
:useInt()
:setInputType({ "TYPE_CLASS_NUMBER", "TYPE_NUMBER_FLAG_SIGNED" }),
prefs.spacer(8)
}
end
return function(module)
module:setCategory("Roll")
module:registerCommand("back", back)
if (windows.isSupported()) then
module:registerCommand("fback", fback)
module:registerCommand("ftime", ftime)
end
module:setCategory("Rollback")
module:registerWatcher(watcher)
module:registerCommandFinder(finder)
module:registerPreferences(getPreferences)
end