Skip to content

Commit

Permalink
Fix: Shell command now correctly handles the % sign in commands
Browse files Browse the repository at this point in the history
  • Loading branch information
hankertrix committed Dec 27, 2024
1 parent 9d4fbcf commit 55171f4
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,12 @@ local function table_pop(table, key, default)
return value or default
end

-- Function to escape a percentage sign %
-- in the string that is being replaced
local function escape_replacement_string(replacement_string)
return replacement_string:gsub("%%", "%%%%")
end

-- Function to parse the number arguments to the number type
---@param args Arguments The arguments to parse
---@return Arguments parsed_args The parsed arguments
Expand Down Expand Up @@ -2431,7 +2437,7 @@ local function fix_shell_command_containing_less(command)
-- Add the less environment variable flags to the less command
fixed_command = fixed_command:gsub(
"%f[%a]less%f[%A]",
less_command_with_modified_env_variables
escape_replacement_string(less_command_with_modified_env_variables)
)

-- Unset the LESS environment variable before calling the command
Expand Down Expand Up @@ -2581,7 +2587,9 @@ local function handle_shell(args, _, _)
-- with the quoted paths of the selected items
command = command:gsub(
shell_variable_pattern,
table.concat(get_paths_of_selected_items(true), " ")
escape_replacement_string(
table.concat(get_paths_of_selected_items(true), " ")
)
)

-- If the item group is the hovered item
Expand All @@ -2595,8 +2603,10 @@ local function handle_shell(args, _, _)

-- Replace the shell variable in the command
-- with the quoted path of the hovered item
command =
command:gsub(shell_variable_pattern, get_path_of_hovered_item(true))
command = command:gsub(
shell_variable_pattern,
escape_replacement_string(get_path_of_hovered_item(true))
)

-- Otherwise, exit the function
else
Expand Down

0 comments on commit 55171f4

Please sign in to comment.