summaryrefslogtreecommitdiff
path: root/yazi-enter.yazi/main.lua
blob: c14d16b15493094b098177fa478e61cade0e55cb (plain)
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
--- @since 26.9.1

local MODE_DIRECTORY = "directory"
local MODE_FILE = "file"
local MODE_EITHER = "either"
local DIRECTION_FORWARD = "forward"
local DIRECTION_BACKWARD = "backward"
local CASE_SENSITIVE = "sensitive"
local CASE_INSENSITIVE = "insensitive"
local CASE_SMART = "smart"
local FIND_ACTION = "find_do"
local SORT_ACTION = "sort"
local SORT_ALPHABETICAL = "alphabetical"
local ACTION_ENTER = "enter"
local ACTION_OPEN_HOVERED = "open-hovered"
local ACTION_NONE = "none"
local EVENT_TYPE = "type"
local EVENT_SUBMIT = "submit"
local EVENT_CANCEL = "cancel"
local EVENT_CLOSED = "closed"
local UPDATE_SUCCESS = "success"
local UPDATE_FAILURE = "failure"
local ERROR_STATUS_CLEAR = false

local COMMON_PROMPT_DATA = {
	name = "find",
	history = "shared",
	value = "",
	obscure = false,
	realtime = true,
	debounce = 0.05,
	pos = { "top-center", x = 0, y = 2, w = 50, h = 3 },
}

local ACCEPTED_OPTIONS = {
	previous = true,
	smart = true,
	insensitive = true,
}

local function parse_mode(value)
	if value == MODE_DIRECTORY or value == MODE_FILE or value == MODE_EITHER then
		return value
	end

	error("yazi-enter: mode must be directory, file, or either", 0)
end

local function normalize_boolean(name, value)
	if value == nil or value == false or value == "no" then
		return false
	end
	if value == true or value == "yes" then
		return true
	end

	error(("yazi-enter: option --%s must be true, false, 'yes', or 'no'"):format(name), 0)
end

local function normalize_options(args)
	return normalize_boolean("previous", args.previous),
		normalize_boolean("smart", args.smart),
		normalize_boolean("insensitive", args.insensitive)
end

local function validate_argument_keys(args)
	for key in pairs(args) do
		local key_type = type(key)
		if key_type == "number" and key ~= 1 then
			error(("yazi-enter: unexpected positional argument at index %s"):format(key), 0)
		elseif key_type == "string" and not ACCEPTED_OPTIONS[key] then
			error(("yazi-enter: unknown option --%s"):format(key), 0)
		elseif key_type ~= "number" and key_type ~= "string" then
			error("yazi-enter: argument keys must be positional indexes or option names", 0)
		end
	end
end

local function derive_direction(previous)
	if previous then
		return DIRECTION_BACKWARD
	end
	return DIRECTION_FORWARD
end

local function derive_effective_case(smart, insensitive)
	if smart then
		return CASE_SMART
	end
	if insensitive then
		return CASE_INSENSITIVE
	end
	return CASE_SENSITIVE
end

local function parse_invocation_intent(args)
	local mode = parse_mode(args[1])
	local previous, smart, insensitive = normalize_options(args)
	validate_argument_keys(args)

	return {
		mode = mode,
		previous = previous,
		smart = smart,
		insensitive = insensitive,
		direction = derive_direction(previous),
		effective_case = derive_effective_case(smart, insensitive),
	}
end

local function decide_target(mode, is_dir)
	if mode == MODE_DIRECTORY then
		if is_dir then
			return ACTION_ENTER
		end
		return ACTION_NONE
	end

	if mode == MODE_FILE then
		if is_dir then
			return ACTION_NONE
		end
		return ACTION_OPEN_HOVERED
	end

	if mode == MODE_EITHER then
		if is_dir then
			return ACTION_ENTER
		end
		return ACTION_OPEN_HOVERED
	end

	return ACTION_NONE
end

local function new_alphabetical_sort_request()
	return {
		by = SORT_ALPHABETICAL,
		reverse = false,
	}
end

local acquire_transient_sort = ya.sync(function(state)
	local lease = state.transient_sort_lease
	if lease ~= nil then
		lease.count = lease.count + 1
		return
	end

	local preference = cx.active.pref
	state.transient_sort_lease = {
		count = 1,
		restore = {
			by = tostring(preference.sort_by),
			reverse = preference.sort_reverse,
			dir_first = preference.sort_dir_first,
			sensitive = preference.sort_sensitive,
			translit = preference.sort_translit,
			fallback = tostring(preference.sort_fallback),
		},
	}
end)

local release_transient_sort = ya.sync(function(state)
	local lease = state.transient_sort_lease
	if lease == nil or type(lease.count) ~= "number" or lease.count < 1 then
		error("yazi-enter: transient sort lease is invalid", 0)
	end

	if lease.count > 1 then
		lease.count = lease.count - 1
		return
	end

	ya.emit(SORT_ACTION, lease.restore)
	state.transient_sort_lease = nil
end)

local commit = ya.sync(function(_, mode, submitted_query)
	local tab = cx.active
	local finder = tab.finder
	local target = tab.current.hovered
	if finder == nil then
		return 0
	end
	if target == nil then
		return 0
	end
	if tostring(finder) ~= submitted_query then
		return 0
	end
	if target:highlights() == nil then
		return 0
	end
	local is_dir = target.cha.is_dir
	local decision = decide_target(mode, is_dir)
	if decision == ACTION_NONE then
		return 0
	end
	if decision == ACTION_ENTER then
		ya.emit("enter", {})
	elseif decision == ACTION_OPEN_HOVERED then
		ya.emit("open", { hovered = true })
	end
	return 1
end)

local function new_find_request(query, search_policy)
	return {
		query,
		previous = search_policy.previous,
		smart = search_policy.smart,
		insensitive = search_policy.insensitive,
	}
end

local function apply_find(query, search_policy)
	local ok, reply_or_error = pcall(ya.exec, FIND_ACTION, new_find_request(query, search_policy))
	if ok then
		return UPDATE_SUCCESS, ERROR_STATUS_CLEAR
	end
	return UPDATE_FAILURE, reply_or_error
end

local function apply_alphabetical_sort()
	return ya.exec(SORT_ACTION, new_alphabetical_sort_request())
end

local function new_prompt_specification(direction)
	return {
		name = COMMON_PROMPT_DATA.name,
		history = COMMON_PROMPT_DATA.history,
		value = COMMON_PROMPT_DATA.value,
		obscure = COMMON_PROMPT_DATA.obscure,
		realtime = COMMON_PROMPT_DATA.realtime,
		debounce = COMMON_PROMPT_DATA.debounce,
		pos = {
			COMMON_PROMPT_DATA.pos[1],
			x = COMMON_PROMPT_DATA.pos.x,
			y = COMMON_PROMPT_DATA.pos.y,
			w = COMMON_PROMPT_DATA.pos.w,
			h = COMMON_PROMPT_DATA.pos.h,
		},
		title = direction == DIRECTION_BACKWARD and "Find previous:" or "Find next:",
	}
end

local function require_plain_data(label, value, active_tables)
	local value_type = type(value)
	if value_type == "string" or value_type == "boolean" or value_type == "number" then
		return
	end
	if value_type ~= "table" or getmetatable(value) ~= nil then
		error(("yazi-enter: %s must use plain boundary data"):format(label), 0)
	end

	active_tables = active_tables or {}
	if active_tables[value] then
		error(("yazi-enter: %s must not contain a table cycle"):format(label), 0)
	end
	active_tables[value] = true

	for key, item in pairs(value) do
		local key_type = type(key)
		if key_type ~= "string" and key_type ~= "number" then
			error(("yazi-enter: %s must use string or number table keys"):format(label), 0)
		end
		require_plain_data(label, item, active_tables)
	end

	active_tables[value] = nil
end

local function require_string(label, value)
	if type(value) ~= "string" then
		error(("yazi-enter: %s must be a string"):format(label), 0)
	end
end

local function new_search_policy(intent)
	if
		type(intent.previous) ~= "boolean"
		or type(intent.smart) ~= "boolean"
		or type(intent.insensitive) ~= "boolean"
	then
		error("yazi-enter: search policy values must be Booleans", 0)
	end

	return {
		previous = intent.previous,
		smart = intent.smart,
		insensitive = intent.insensitive,
	}
end

local function new_application_boundaries(
	prompt_capability,
	find_update_capability,
	commit_capability,
	target_dispatch_capability,
	sort_acquire_capability,
	alphabetical_sort_capability,
	sort_release_capability
)
	return {
		prompt = function(prompt_data)
			if type(prompt_data) ~= "table" then
				error("yazi-enter: prompt data must be a table", 0)
			end
			require_plain_data("prompt data", prompt_data)
			return prompt_capability(prompt_data)
		end,
		find_update = function(query, search_policy)
			require_string("Find query", query)
			if type(search_policy) ~= "table" then
				error("yazi-enter: search policy must be a table", 0)
			end
			require_plain_data("search policy", search_policy)

			local result, error_status = find_update_capability(query, search_policy)
			if result ~= UPDATE_SUCCESS and result ~= UPDATE_FAILURE then
				error("yazi-enter: Find update capability returned an invalid result", 0)
			end
			return result, error_status
		end,
		commit = function(mode, submitted_query)
			require_string("commit mode", mode)
			require_string("submitted query", submitted_query)

			local dispatch_count = commit_capability(mode, submitted_query)
			if dispatch_count ~= 0 and dispatch_count ~= 1 then
				error("yazi-enter: commit capability returned an invalid dispatch count", 0)
			end
			return dispatch_count
		end,
		target_dispatch = function(decision)
			if decision ~= ACTION_ENTER and decision ~= ACTION_OPEN_HOVERED then
				error("yazi-enter: target dispatch capability received an invalid decision", 0)
			end
			target_dispatch_capability(decision)
		end,
		sort_acquire = function()
			return sort_acquire_capability()
		end,
		sort_alphabetically = function()
			return alphabetical_sort_capability()
		end,
		sort_release = function()
			return sort_release_capability()
		end,
	}
end

local function new_invocation_state(intent, receiver)
	require_plain_data("invocation intent", intent)
	return {
		intent = intent,
		receiver = receiver,
		current_query = "",
		error_status = ERROR_STATUS_CLEAR,
	}
end

local function run_selection_session(intent, boundaries)
	local receiver = boundaries.prompt(new_prompt_specification(intent.direction))
	local state = new_invocation_state(intent, receiver)

	while true do
		local query, event = state.receiver:recv()
		if event == 3 then
			state.current_query = query
			local result, error_status = boundaries.find_update(
				state.current_query,
				new_search_policy(state.intent)
			)
			if result == UPDATE_SUCCESS then
				state.error_status = ERROR_STATUS_CLEAR
			else
				state.error_status = error_status
			end
		elseif event == 1 then
			state.current_query = query
			local result, error_status = boundaries.find_update(
				state.current_query,
				new_search_policy(state.intent)
			)
			state.error_status = error_status
			if result == UPDATE_SUCCESS then
				return boundaries.commit(state.intent.mode, state.current_query)
			end
			return 0
		elseif event == 2 then
			return 0
		else
			return 0
		end
	end
end

local function run_transiently_sorted_session(intent, boundaries)
	boundaries.sort_acquire()

	local session_ok, result_or_error = pcall(function()
		boundaries.sort_alphabetically()
		return run_selection_session(intent, boundaries)
	end)

	local release_ok, release_error = pcall(boundaries.sort_release)
	if not release_ok then
		error(release_error, 0)
	end
	if not session_ok then
		error(result_or_error, 0)
	end

	return result_or_error
end

local function run_entry(job)
	local intent = parse_invocation_intent(job.args)
	local boundaries = new_application_boundaries(
		ya.input,
		apply_find,
		commit,
		function()
			error("yazi-enter: target dispatch is owned by the commit transaction", 0)
		end,
		acquire_transient_sort,
		apply_alphabetical_sort,
		release_transient_sort
	)
	return run_transiently_sorted_session(intent, boundaries)
end

return {
	entry = function(_, job)
		return run_entry(job)
	end,
}