Skip to content

Commit

Permalink
Merge pull request #2 from mistweaverco/feat/args
Browse files Browse the repository at this point in the history
feat: ability to pass rg flags dynamically
  • Loading branch information
gorillamoe authored Aug 7, 2024
2 parents 990f893 + c5e1e1b commit a95d40c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
35 changes: 28 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ require('lazy').setup({
config = function()
-- Setup is required, even if you don't pass any options
require('tafuta').setup({
-- The user command to run the search e.g. `:Tf <query>`
-- The user command to run the search e.g. `:Tf <flags> <query>`
-- Default: "Tf", but it can be anything you want.
-- If you don't want a command, you can set it to `nil`
user_command_prompt = "Tf",
Expand All @@ -74,23 +74,44 @@ You can either search for *any text* or search for the word under the cursor.
Search for text in your project via the command:

```
:Tf <search-term>
:Tf <rg-flags> <search-term>
```

or via calling a lua function:

```lua
require('tafuta').run("[search term here, can be regex too]")
require('tafuta').run("[rg-flags] [search_term]")
```

If you omit the search term,
and just run
#### Example

Search for "Neovim version" in the project:

> [!NOTE]
> You need to escape all spaces in the search term,
> when using the user command.
```
:Tf --hidden --no-ignore Neovim\ version
```

or via calling a lua function:

```lua
require('tafuta').run()
require('tafuta').run({"--hidden", "--no-ignore", "Neovim version"})
```

it will prompt you for a search term.
Also with default options:

```
:Tf Neovim\ version
```

or via calling a lua function:

```lua
require('tafuta').run({"Neovim version"})
```

### Search for word under cursor

Expand Down
2 changes: 1 addition & 1 deletion lua/tafuta/globals/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local M = {}

M.VERSION = "1.1.0"
M.VERSION = "1.2.0"

return M
31 changes: 18 additions & 13 deletions lua/tafuta/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ local function get_word_under_cursor()
end
end

local search = function(search_query)
local search = function(search_opts)
-- the search query is the last item in the table
local search_query = search_opts[#search_opts]
-- search flags are all the items in the table except the last one
local search_flags = search_opts
table.remove(search_flags, #search_flags)
local search_command = { "rg", "--vimgrep" }
local rg_options = CONFIG.get().rg_options
if rg_options ~= nil then
for _, option in ipairs(rg_options) do
table.insert(search_command, option)
end
local rg_options = CONFIG.get().rg_options or {}
rg_options = vim.tbl_deep_extend("force", search_flags, rg_options)
for _, option in ipairs(rg_options) do
table.insert(search_command, option)
end
table.insert(search_command, search_query)
vim.system(search_command, { text = true }, async_run)
Expand All @@ -60,9 +64,9 @@ M.setup = function(config)
vim.notify("❌ No search query provided", vim.log.levels.INFO, { title = "Tafuta" })
return
end
M.run(a.args)
M.run(a.fargs)
end, {
nargs = "?",
nargs = "+",
desc = "Tf, blazingly fast ⚑ search πŸ” using ripgrep πŸ¦€",
})
end
Expand All @@ -76,15 +80,16 @@ M.setup = function(config)
end
end

M.run = function(search_query)
M.run = function(search_opts)
if not rg_installed then
vim.notify("❌ ripgrep not found on the system", vim.log.levels.WARN, { title = "Tafuta" })
return
end
if search_query == nil then
search_query = vim.fn.input("Search: ")
if search_opts == nil then
vim.notify("❌ no search query provided", vim.log.levels.INFO, { title = "Tafuta" })
return
end
search(search_query)
search(search_opts)
end

M.cursor = function()
Expand All @@ -93,7 +98,7 @@ M.cursor = function()
vim.notify("❌ no word under cursor", vim.log.levels.INFO, { title = "Tafuta" })
return
end
search(word)
search({ word })
end

M.version = function()
Expand Down

0 comments on commit a95d40c

Please sign in to comment.