Custom search for v2 using git grep

This is amazing!

I adapted it to use ripgrep (rg). Since I run SilverBullet in a container, I do need to make the executable available first. I do this by adding the volume mapping /usr/bin/rg:/usr/bin/rg (since I have rg installed on the host anyway).

Here’s my adaptation for ripgrep:

command.define {
  name = "Search: Custom Search",
  key = "Ctrl-s",
  run = function()
    local term = editor.prompt()
    local results = rg(term)
    local options = {}
    for result in results do
      table.insert(options, { 
        name = result.file,
        description = result.match,
        value = result
      })
    end
    local result = editor.filterBox("Select:", options)
    if result and result.value then
      local page, count = string.gsub(result.value.file, ".md", "")
      editor.navigate({ kind = "page", page = page })
      editor.moveCursorToLine(result.value.line, result.value.col, true)
    end
  end
}

function rg(term)
  local search_results = shell.run("rg", {"-nb", "--type", "markdown", term})
  local matches = string.split(search_results.stdout, "\n")
  local output = {}
  for item in matches do
    local fullpath, line, col, result = string.match(item, "^(.-):(.-):(.-):(.+)$")
    if (fullpath == nil) then
      do break end
    end
    table.insert(output, {file = fullpath, line = line, col = col, match = result })
  end

  return output
end

Which in my current theme, looks like this:

(Yes, I need to tweak my highlight colour, it’s not making the text more readable… :D)

Edit: swapping the name and description properties is actually nicer, because most of the time the matching line is more important than the page it’s on.

4 Likes