Skip to content

Commit

Permalink
fixing args order
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Badragan committed May 6, 2024
1 parent 483a186 commit b6ac064
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions lua/grug-far/rg/getArgs.lua
Original file line number Diff line number Diff line change
@@ -1,45 +1,64 @@
local colors = require('grug-far/rg/colors')

-- TODO (sbadragan): might need to disable some flags, like:
-- --no-include-zero --no-byte-offset
-- --hyperlink-format=none
-- --max-columns=0
-- --no-max-columns-preview --no-trim
-- blacklist: --help --quiet
-- Hmmm, there are just too many things that could completely screw it up ... I think we need a whitelist of useful
-- flags that we allow the user to pass, otherwise replacing would not work
local function isValidArg(arg)
return vim.startswith(arg, '-') and arg ~= '--'
end

local function getArgs(inputs, options)
local args = nil
if #inputs.search < (options.minSearchChars or 1) then
return nil
end

args = { inputs.search }
if #inputs.replacement > 0 then
table.insert(args, '--replace=' .. inputs.replacement)
end

if #inputs.filesGlob > 0 then
table.insert(args, '--glob=' .. inputs.filesGlob)
end

table.insert(args, '--heading')
-- user overridable args
table.insert(args, '--line-number')
table.insert(args, '--column')

-- colors so that we can show nicer output
table.insert(args, '--color=ansi')
for k, v in pairs(colors.rg_colors) do
table.insert(args, '--colors=' .. k .. ':none')
table.insert(args, '--colors=' .. k .. ':fg:' .. v.rgb)
end

-- user overrides
local extraRgArgs = options.extraRgArgs and vim.trim(options.extraRgArgs) or ''
if #extraRgArgs > 0 then
for arg in string.gmatch(extraRgArgs, "%S+") do
table.insert(args, arg)
if isValidArg(arg) then
table.insert(args, arg)
end
end
end

-- user provided flags should come last
if #inputs.flags > 0 then
for flag in string.gmatch(inputs.flags, "%S+") do
table.insert(args, flag)
if isValidArg(flag) then
table.insert(args, flag)
end
end
end

-- required args
table.insert(args, '--heading')

if #inputs.replacement > 0 then
table.insert(args, '--replace=' .. inputs.replacement)
end

if #inputs.filesGlob > 0 then
table.insert(args, '--glob=' .. inputs.filesGlob)
end

table.insert(args, '--color=ansi')
for k, v in pairs(colors.rg_colors) do
table.insert(args, '--colors=' .. k .. ':none')
table.insert(args, '--colors=' .. k .. ':fg:' .. v.rgb)
end

return args
end

Expand Down

0 comments on commit b6ac064

Please sign in to comment.