summaryrefslogtreecommitdiff
path: root/yazi-enter.yazi/main.lua
blob: ef1641f7592bedc408934405e41129b5ed96d54b (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
--- @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 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 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 commit = ya.sync(function(_, mode, submitted_query)
	return 0
end)

local function new_search_policy(intent)
	return {
		previous = intent.previous,
		smart = intent.smart,
		insensitive = intent.insensitive,
	}
end

local function new_application_boundaries(prompt_capability, find_update_capability, commit_capability)
	return {
		prompt = function(prompt_data)
			return prompt_capability(prompt_data)
		end,
		find_update = function(query, 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)
			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,
	}
end

return {
	entry = function(_, job)
		return parse_invocation_intent(job.args)
	end,
}