-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils-shell.lua
More file actions
357 lines (282 loc) · 10.9 KB
/
Copy pathutils-shell.lua
File metadata and controls
357 lines (282 loc) · 10.9 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
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
-- The module containing the utilities to deal with shell commands
-- Import the utilities module
local utils = require(".utils")
-- Import the constants required
local ItemGroup = require(".constants").ItemGroup
-- The module table
local M = {}
-- The pattern to get the shell variables in a command
---@type string
local shell_variable_pattern = "%%[hs]%d?"
-- The pattern to match the pager argument for the bat command
---@type string
local bat_pager_pattern = "(%-%-pager)%s+(%S+)"
-- The default bat pager command without the -F flag
---@type string
local bat_default_pager_command_without_f_flag = "less -RX"
-- Function to match a binary name against a search string
---@param binary_name string The name of the binary
---@param search_string string The string to search for the binary name
---@return string binary_pattern The pattern for the binary
---@return string? binary_path The path to the binary
local function match_binary_name(binary_name, search_string)
-- The binary pattern
local binary_pattern = "%f[%w_%-%.].*" .. binary_name .. "%f[%W%s]"
-- Get the binary path
local binary_path = search_string:match(binary_pattern)
-- Escape the binary path if it's not nil
if binary_path ~= nil then
binary_path = utils.escape_replacement_string(binary_path)
end
-- Return the binary pattern and the path
return binary_pattern, binary_path
end
-- Function to remove the F flag from the less command
---@param command string The shell command containing the less command
---@param less_binary_pattern string The pattern to match the less binary
---@return string command The command with the F flag removed
---@return boolean f_flag_found Whether the F flag was found
local function remove_f_flag_from_less_command(command, less_binary_pattern)
-- Initialise the variable to store if the F flag is found
local f_flag_found = false
-- Initialise the variable to store the replacement count
local replacement_count = 0
-- Initialised the modified command
local modified_command = command
-- Remove the F flag when it is passed at the start
-- of the flags given to the less command
modified_command, replacement_count =
modified_command:gsub("(" .. less_binary_pattern .. ".*)%-F", "%1")
-- If the replacement count is not 0,
-- set the f_flag_found variable to true
if replacement_count ~= 0 then f_flag_found = true end
-- Remove the F flag when it is passed in the middle
-- or end of the flags given to the less command command
modified_command, replacement_count = modified_command:gsub(
"(" .. less_binary_pattern .. ".*%-)(%a*)F(%a*)",
"%1%2%3"
)
-- If the replacement count is not 0,
-- set the f_flag_found variable to true
if replacement_count ~= 0 then f_flag_found = true end
-- Return the command and whether or not the F flag was found
return modified_command, f_flag_found
end
-- Function to fix a command containing less.
-- All this function does is remove
-- the F flag from a command containing less.
---@param command string The shell command containing the less command
---@param less_binary_pattern string The pattern to match the less binary
---@param less_binary_path string The path to the less binary
---@return string command The fixed shell command
local function fix_shell_command_containing_less(
command,
less_binary_pattern,
less_binary_path
)
-- Remove the F flag from the given command
local fixed_command =
remove_f_flag_from_less_command(command, less_binary_pattern)
-- Get the LESS environment variable
local less_environment_variable = os.getenv("LESS")
-- If the LESS environment variable is not set,
-- then return the given command with the F flag removed
if not less_environment_variable then return fixed_command end
-- Otherwise, remove the F flag from the LESS environment variable
-- and check if the F flag was found
local less_command_with_modified_env_variables, f_flag_found =
remove_f_flag_from_less_command(
string.format("%s %s", less_binary_path, less_environment_variable),
less_binary_pattern
)
-- If the F flag isn't found,
-- then return the given command with the F flag removed
if not f_flag_found then return fixed_command end
-- Add the less environment variable flags to the less command
fixed_command = fixed_command:gsub(
less_binary_pattern,
utils.escape_replacement_string(
less_command_with_modified_env_variables
)
)
-- Unset the LESS environment variable before calling the command
fixed_command = "unset LESS; " .. fixed_command
-- Return the fixed command
return fixed_command
end
-- Function to fix the bat default pager command
---@param command string The command containing the bat default pager command
---@param bat_binary_pattern string The pattern to match the bat binary
---@param bat_binary_path string The path to the bat binary
---@return string command The fixed bat command
local function fix_shell_command_containing_bat(
command,
bat_binary_pattern,
bat_binary_path
)
-- Get the pager argument for the bat command
local _, pager_argument = command:match(bat_pager_pattern)
-- If there is a pager argument
--
-- We don't need to do much if the pager argument already exists,
-- as we can rely on the function that fixes the less command to
-- remove the -F flag that is executed after this function is called.
--
-- There's only work to be done if the pager argument isn't quoted,
-- as we need to quote it so the function that fixes the less command
-- can execute cleanly without causing shell syntax errors.
--
-- The reason why we don't quote the less command in the function
-- to fix the less command is to not deal with using backslashes
-- to escape the quotes, which can get really messy and really confusing,
-- so we just naively replace the less command with the fixed version
-- without caring about whether the less command is passed as an
-- argument, or is called as a shell command.
if pager_argument then
-- If the pager argument is quoted, return the command immediately
if pager_argument:find("['\"].+['\"]") then return command end
-- Otherwise, quote the pager argument with single quotes
--
-- It should be fine to quote with single quotes
-- as the user passing the argument probably isn't
-- using a shell variable, as they would have quoted
-- the shell variable in double quotes instead of
-- omitting the quotes.
pager_argument = string.format("'%s'", pager_argument)
-- Replace the pager argument with the quoted version
local modified_command =
command:gsub(bat_pager_pattern, "%1 " .. pager_argument)
-- Return the modified command
return modified_command
end
-- Replace the bat command with the command to use
-- the bat default pager command without the F flag
local modified_command = command:gsub(
bat_binary_pattern,
string.format(
"%s --pager '%s'",
bat_binary_path,
bat_default_pager_command_without_f_flag
),
1
)
-- Return the modified command
return modified_command
end
-- Function to fix the shell commands given to work properly with Yazi
---@param command string A shell command
---@return string command The fixed shell command
local function fix_shell_command(command)
-- Get the bat binary pattern and path from the command
local bat_binary_pattern, bat_binary_path =
match_binary_name("bat", command)
-- Initialise the fixed command
local fixed_command = command
-- If the bat binary is in the command
if bat_binary_path ~= nil then
-- Calls the command to fix the bat command
fixed_command = fix_shell_command_containing_bat(
command,
bat_binary_pattern,
bat_binary_path
)
end
-- Get the less binary pattern and path from the fixed command
local less_binary_pattern, less_binary_path =
match_binary_name("less", fixed_command)
-- If the less binary is in the command
if less_binary_path ~= nil then
-- Fix the command containing less
fixed_command = fix_shell_command_containing_less(
fixed_command,
less_binary_pattern,
less_binary_path
)
end
-- Return the fixed command
return fixed_command
end
-- Function to handle a shell command
---@param args ParsedArgs The arguments to pass to the command
---@param config Configuration The configuration object
function M.handle(args, config)
-- Get the first item of the arguments given
-- and set it to the command variable
local command = table.remove(args, 1)
-- Get the type of the command variable
local command_type = type(command)
-- If the command isn't a string,
-- show an error message and exit the function
if command_type ~= "string" then
return utils.throw_error(
"Shell command given is not a string, "
.. "instead it is a '%s', "
.. "with value '%s'",
command_type,
tostring(command)
)
end
-- Fix the given command
command = fix_shell_command(command)
-- Call the function to get the item group
local item_group = utils.get_item_group(config)
-- If no item group is returned, exit the function
if not item_group then return end
-- Get whether the exit if directory flag is passed
local exit_if_dir = utils.table_pop(args, "exit_if_dir", false)
-- If the item group is the selected items
if item_group == ItemGroup.Selected then
-- Get the paths of the selected items
local selected_items = utils.get_paths_of_selected_items(true)
-- If there are no selected items, exit the function
if not selected_items then return end
-- If the exit if directory flag is passed
if exit_if_dir then
-- Initialise the number of files
local number_of_files = 0
-- Iterate over all of the selected items
for _, item in pairs(selected_items) do
-- Get the cha object of the item
local item_cha = fs.cha(Url(item), false)
-- If the item isn't a directory
if not (item_cha or {}).is_dir then
-- Increment the number of files
number_of_files = number_of_files + 1
end
end
-- If the number of files is 0, then exit the function
if number_of_files == 0 then return end
end
-- Replace the shell variable in the command
-- with the quoted paths of the selected items
command = command:gsub(
shell_variable_pattern,
utils.escape_replacement_string(table.concat(selected_items, " "))
)
-- If the item group is the hovered item
elseif item_group == ItemGroup.Hovered then
-- Get the hovered item path
local hovered_item_path = utils.get_path_of_hovered_item(true)
-- If the hovered item path is nil, exit the function
if not hovered_item_path then return end
-- If the exit if directory flag is passed,
-- and the hovered item is a directory,
-- then exit the function
if exit_if_dir and utils.hovered_item_is_dir() then return end
-- Replace the shell variable in the command
-- with the quoted path of the hovered item
command = command:gsub(
shell_variable_pattern,
utils.escape_replacement_string(hovered_item_path)
)
-- Otherwise, exit the function
else
return
end
-- Merge the command back into the arguments given
args = utils.merge_tables({ command }, args)
-- Emit the command to operate on the hovered item
ya.emit("shell", args)
end
-- Return the module table
return M