-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
142 lines (134 loc) · 4.14 KB
/
init.lua
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
-- Function to check if flag is valid
local function is_flag_in_allowed(test_flag, allowed_list)
for _, flag in ipairs(allowed_list) do
if flag == test_flag then
return true
end
end
return false
end
-- Function to send error notifications
local function notify_error(message)
ya.notify({
title = "Yazi Command",
content = message,
level = "warn",
timeout = 5,
})
end
return {
entry = function()
-- Define all commands and their flags
local command_flags = {
escape = { "--all", "--find", "--visual", "--select", "--filter", "--search" },
quit = { "--no-cwd-file" },
leave = {},
enter = {},
back = {},
seek = {},
cd = { "--interactive" },
reveal = {},
select = { "--state" },
select_all = { "--state" },
toggle = { "--state" },
toggle_all = { "--state" },
visual_mode = { "--unset" },
open = { "--interactive", "--hovered" },
yank = { "--cut" },
unyank = {},
paste = { "--force", "--follow", "--before" },
link = { "--relative", "--force" },
remove = { "--force", "--permanently", "--hovered" },
create = { "--force", "--dir" },
rename = { "--hovered", "--force", "--empty", "--cursor" },
copy = {},
shell = { "--confirm", "--block", "--orphan", "--interactive" },
hidden = {},
linemode = {},
search = {},
find = { "--previous", "--smart", "--insensitive" },
find_arrow = { "--previous" },
filter = { "--smart", "--insensitive" },
sort = { "--reverse", "--dir-first", "--translit" },
tab_create = { "--current" },
tab_switch = { "--relative" },
tab_swap = {},
tasks_show = {},
close = { "--submit" },
arrow = {},
inspect = {},
cancel = {},
plugin = { "--args" },
move = { "--in-operating" },
backword = {},
forward = { "--end-of-word" },
insert = { "--append" },
visual = {},
delete = { "--cut", "--insert" },
undo = {},
redo = {},
help = {},
backspace = { "--under" },
kill = {},
close_input = {},
peek = { "--force" },
hardlink = { "--follow", "--force" },
bulk_rename = {},
hover = {},
refresh = {},
suspend = {},
tab_close = {},
update_files = {},
update_mimetype = {},
update_paged = {},
update_yanked = {},
spot = {},
}
-- Get input from the user with a prompt window
local command, event = ya.input({
title = "Command:", -- Title of the input prompt
position = { "top-center", y = 3, w = 40 }, -- Positioning details
})
-- Check if the input was cancelled or closed
if event ~= 1 then
return -- Exit if input was not provided
end
-- Extract the main command (the first word before any space)
local main_command = command:match("^[^%s]+") or ""
-- Extract the secondary command
-- (First and last double quotes in the string or
-- the first word after a space that is not a flag --)
local secondary_command = command:match('"(.*)"') or command:match("%s+([^%s-][^%s]*)") or ""
-- Get the list of allowed flags for this command
local allowed_flags = command_flags[main_command]
-- Store flags along with their arguments
local flags = {}
local current_flag = nil
-- If the command is not valid, display an error and exit
if not allowed_flags then
notify_error(string.format('Command "%s" is not a valid command', main_command))
return
end
command = command:gsub("=", " "):gsub("'", ""):gsub('".-"', "")
for word in command:gmatch("[^%s]+") do
if word:match("^%-%-") then
if not is_flag_in_allowed(word, allowed_flags) then
notify_error(string.format("%s is not a valid flag for %s", word, main_command))
return
end
current_flag = word:sub(3) -- Remove the leading --
flags[current_flag] = "yes" -- Initialize the flag in the table
elseif current_flag then
if flags[current_flag] == "yes" then
flags[current_flag] = word:gsub("\\%-", "-") -- Allow escaping --
else
flags[current_flag] = string.format("%s %s", flags[current_flag], word:gsub("\\%-", "-"))
end
end
end
-- Insert the secondary command into params table
table.insert(flags, tostring(secondary_command))
-- Execute the main command with the parsed parameters
ya.manager_emit(tostring(main_command), flags)
end,
}