Repository navigation
Expand file tree
/
Copy pathsnippets.lua
More file actions
309 lines (278 loc) · 8.41 KB
/
Copy pathsnippets.lua
File metadata and controls
309 lines (278 loc) · 8.41 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
---@module 'sai.snippets'
local M = {}
local e = require 'sai.api.eventloop'
local S = require 'sai.bridge.shell'
local remapper = require 'sai.lib.remapper'
local editor = require 'sai.mode.editor'
function M.update()
local bridge_dir = debug.getinfo(1, 'S').source:match '/.+/sai/' .. 'bridge/'
sai.exec(('cd %s && git pull'):format(bridge_dir))
local pipe = io.popen('ls -1 ' .. bridge_dir .. '*.so') or error('Could not list cpp modules in: ' .. bridge_dir)
for so_path in pipe:lines() do
if os.execute(string.format('[ %s -nt %s ]', so_path:gsub('.so$', '.cpp'), so_path)) ~= 1 then
S.compile_so(so_path)
local old = package.loaded['sai.bridge.' .. so_path:match '([^/]+)%.so$']
for k, v in pairs(old and S.load_so(so_path) or {}) do
old[k] = v
end
end
end
pipe:close()
end
function M.load_dir_if_single()
local function check_and_load()
local imagelist = sai.imagelist
if imagelist.size ~= 1 then return end
local dir = imagelist.get_current().path:match '.+/'
local was_recursive = imagelist.recursive
imagelist.recursive = false
imagelist.add(dir) -- to preserve fsmon on the directory
imagelist.recursive = was_recursive
end
if sai.initialized then
check_and_load()
else
e.subscribe { event = 'SwiEnter', once = true, callback = check_and_load }
end
end
---@param timeout? integer how many seconds to display the output for, negative for #out/-timeout
function M.print_shell_output(timeout)
timeout = timeout or -10
e.subscribe {
event = 'User',
match = 'ShellCmdPost',
callback = function(ev)
if #ev.data.stderr > 0 then
sai.notify(
('===ERROR STREAM===\n%s\n\n===STDOUT===\n%s'):format(ev.data.stderr, ev.data.stdout),
timeout < 0 and math.ceil((2 * #ev.data.stderr + #ev.data.stdout) / -timeout) or 2 * timeout
)
elseif #ev.data.stdout > 0 then
sai.notify(ev.data.stdout, timeout < 0 and math.ceil(#ev.data.stdout / -timeout) or timeout)
end
end,
}
end
---@param timeout integer|false? how long it should show the message for (s)
function M.print_option_changes(timeout)
if timeout == false then
e.unsubscribe { event = 'OptionSet', group = 'print_var_change' }
return
end
local function register_printer()
-- register after base config has been loaded
e.subscribe {
event = 'OptionSet',
pattern = { '!sai.imagelist.size', '!sai.text.status', '^' },
group = 'print_var_change',
callback = function(ev)
if e.ignore_opts then return end
local v = ev.data
if type(v) == 'number' then
if math.floor(v * 100) == v * 100 then
v = '' .. v
else
v = ('%.2f'):format(v)
end
elseif type(v) == 'table' or type(v) == 'function' then
return -- ignore window size and position changes, and function values
end
local name = ev.match:match '([^.]+%.[^.]+)$'
sai.notify(
('%s%s: %s'):format(
name:sub(1, 1):upper(),
name:sub(2):gsub('[_.](.)', function(c) return ' ' .. c:upper() end),
v
),
timeout
)
end,
}
end
if sai.initialized then
register_printer()
else
e.subscribe { event = 'SwiEnter', once = true, callback = register_printer }
end
end
function M.resize_image_with_window()
e.subscribe {
event = 'WinResized',
mode = { 'viewer', 'slideshow' },
callback = function(ev)
---@diagnostic disable-next-line: assign-type-mismatch
local viewer = sai[ev.mode] ---@type sai.api.viewer
if type(viewer.scale) == 'string' then viewer.super.set_fix_scale(viewer.scale) end
end,
}
end
---Auto-play videos in an external player (mpv by default), killing it on image change.
---@param cmd? string string to run with % or %f as the template for the video file
function M.auto_open_video(cmd)
cmd = cmd or 'mpv --no-terminal %f'
sai.formats.video = { size = 100, columns = 1, rows = 1 }
local kill_player = function() end
e.subscribe {
event = 'ImgChanged',
match = { 'viewer', 'slideshow' },
callback = function(ev)
kill_player()
if ev.data.format:find('Video', 1, true) then
sai.exec(cmd, true)
kill_player = function()
os.execute(("pkill -f '%s'"):format(cmd:gsub('%%[f]?', ev.data.path)))
kill_player = function() end
end
end
end,
}
e.subscribe { event = 'ModeChanged', match = { 'v:g', 's:g' }, callback = function() kill_player() end }
e.subscribe {
event = 'SwiLeavePre',
mode = { 'viewer', 'slideshow' },
callback = function()
kill_player()
return true
end,
}
end
---@generic O
---@param values `O`[]
---@param current O
---@return O?
function M.cycle_values(values, current)
for i, value in ipairs(values) do
if value == current then return values[i % #values + 1] end
end
end
function M.cycle_scale()
local api = sai.modes[1] ---@cast api sai.api.viewer
---@type fixed_scale_t[]
local values = {
'optimal',
'width',
'height',
'fit',
'fill',
'real',
'keep',
}
for k, _ in pairs(api.custom_scale_handlers) do
if type(k) == 'string' then values[#values + 1] = k end
end
local current = api.scale
if type(current) ~= 'string' then current = 'keep' end
api.scale = M.cycle_values(values, current)
end
function M.cycle_position()
local api = sai.modes[1] ---@cast api sai.api.viewer
---@type fixed_position_t[]
local values = {
'center',
'topcenter',
'leftcenter',
'rightcenter',
'bottomcenter',
'topleft',
'topright',
'bottomleft',
'bottomright',
}
local current = type(api.position) == 'string' and api.position or 'center'
api.position = M.cycle_values(values, current)
end
function M.cycle_auto_pos()
local api = sai.modes[1] ---@cast api sai.api.viewer
---@type autopos_t[]
local values = {
'center',
'follow',
'free',
}
api.auto_pos = M.cycle_values(values, api.auto_pos)
end
---@param key string?
---@return sai.lib.remapper
function M.two_pane_mode(key)
local super = remapper
---@class tp: sai.lib.remapper
local tp = { _path = 'snippets.two_pane_mode' }
function tp:set_enabled(val)
if self._enabled == val then return end
if val and not self.sai.gallery.thumb_size then
self.sai.gallery.thumb_size = sai.get_window_size().width / 2
end
return super.set_enabled(self, val)
end
super.new(tp)
tp.sai.save_user_changes = true
tp.sai.mode = 'gallery'
tp.sai.gallery(function(g) ---@param g sai.gallery
g.padding_size = 0
g.cache_size = 0
g.preload = false
g.border_size = 5
g.selected_scale = 1
g.window_color = 0xff808080
g.hover = true
tp.sai.eventloop.subscribe {
event = 'WinResized',
callback = function(ev) g.thumb_size = ev.data.width / 2 end,
}
end)
key = key or 't'
v.map(key, function() tp.enabled = true end, 'Enable Two-pane mode')
tp.map(key, function() tp.enabled = false end, 'Disable Two-pane mode')
return tp
end
---@return sai.mode.editor lua prompt with its own history
function M.lua_mode()
---@param out string[]|false
---@return boolean?|false verdict
---@return string? msg message the editor notifies after the mode settles
local on_confirm = function(_, out)
if not out then return end
local text = table.concat(out, '\n')
if text == '' then return end
-- the editor owns the lifecycle and emits the message after the
-- mode settles; this only runs and decides
local cb, syntax = S.make_runnable(text)
if not cb then return false, syntax end
local ran, result = pcall(cb)
if not ran then return false, 'Runtime error: ' .. result:gsub('^.-:%d:%s*', '') end
return true, result
end
---@diagnostic disable-next-line: missing-fields
return editor.new {
_path = 'snippets.lua_mode',
_prompt = 'Lua',
-- TODO: add autocompletion, likely just for paths and potentially variables
on_confirm = on_confirm,
}
end
---@return sai.mode.editor shell prompt with its own history
function M.shell_mode()
---@param out string[]|false
---@return boolean?|false verdict
---@return string? msg message the editor notifies after the mode settles
local on_confirm = function(_, out)
if not out then return end
local text = table.concat(out, '\n')
if text == '' then return end
-- the editor owns the lifecycle and emits the message; a throw
-- (bad % expansion, no shell to run in) counts as a failed run
local ok, out, code, err = pcall(sai.exec, text)
if not ok then return false, out end
if tonumber(code) ~= 0 then
return false, ('Shell exited %s: %s'):format(tostring(code), err ~= '' and err or out)
end
return true, out
end
---@diagnostic disable-next-line: missing-fields
return editor.new {
_path = 'snippets.shell_mode',
_prompt = 'Shell',
on_confirm = on_confirm,
}
end
return M