Skip to content
pynappo edited this page Jan 20, 2024 · 7 revisions

Exotic recipes

Users are welcome to add their preferred components snippets here! Please make sure that your snippets are as portable as possible.

Search Results

local SearchResults = {
    condition = function(self)
        local lines = vim.api.nvim_buf_line_count(0)
        if lines > 50000 then return end

        local query = vim.fn.getreg("/")
        if query == "" then return end

        if query:find("@") then return end

        local search_count = vim.fn.searchcount({ recompute = 1, maxcount = -1 })
        local active = false
        if vim.v.hlsearch and vim.v.hlsearch == 1 and search_count.total > 0 then
            active = true
        end
        if not active then return end

        query = query:gsub([[^\V]], "")
        query = query:gsub([[\<]], ""):gsub([[\>]], "")

        self.query = query
        self.count = search_count
        return true
    end,
    {
        provider = function(self)
            return table.concat({
                ' ', self.query, ' ', self.count.current, '/', self.count.total, ' '
            })
        end,
        hl = nil -- your highlight goes here
    },
    Space -- A separator after, if section is active, without highlight.
}

Screenshot from 2022-04-27 16-50-27

Neotest and Overseer.nvim

local Overseer = {
    condition = function()
        local ok, _ = pcall(require, "overseer")
        if ok then return true end
    end,
    init = function(self)
        self.overseer = require("overseer")
        self.tasks = self.overseer.task_list
        self.STATUS = self.overseer.constants.STATUS
    end,
    static = {
        symbols = {
            ["FAILURE"] = "",
            ["CANCELED"] = "",
            ["SUCCESS"] = "",
            ["RUNNING"] = "",
        },
        colors = {
            ["FAILURE"] = "red",
            ["CANCELED"] = "gray",
            ["SUCCESS"] = "green",
            ["RUNNING"] = "yellow",
        },
    },
    {
        condition = function(self) return #self.tasks.list_tasks() > 0 end,
        {
            provider = function(self)
                local tasks_by_status = self.overseer.util.tbl_group_by(self.tasks.list_tasks({ unique = true }),
                    "status")

                for _, status in ipairs(self.STATUS.values) do
                    local status_tasks = tasks_by_status[status]
                    if self.symbols[status] and status_tasks then
                        self.color = self.colors[status]
                        return self.symbols[status]
                    end
                end
            end,
            hl = function(self) return { fg = self.color } end,
        },
    },
}

Screen Shot 2022-10-28 at 17 16 07

Lazy plugin manager

local Lazy = {
    condition = require("lazy.status").has_updates,
    update = { "User", pattern = "LazyUpdate" },
    provider = function() return "" .. require("lazy.status").updates() .. " " end,
    on_click = {
      callback = function() require("lazy").update() end,
      name = "update_plugins",
    },
    hl = { fg = "gray" },
  }

Disable dropbar in its options. For example in lazy.nvim, use opts = {{general = {enable = false}}}

local Dropbar = {
  condition = function(self)
    self.data = vim.tbl_get(dropbar.bars or {}, vim.api.nvim_get_current_buf(), vim.api.nvim_get_current_win())
    return self.data
  end,
  static = { dropbar_on_click_string = 'v:lua.dropbar.callbacks.buf%s.win%s.fn%s' },
  init = function(self)
    local components = self.data.components
    local children = {}
    for i, c in ipairs(components) do
      local child = {
        {
          hl = c.icon_hl,
          provider = c.icon:gsub('%%', '%%%%'),
        },
        {
          hl = c.name_hl,
          provider = c.name:gsub('%%', '%%%%'),
        },
        on_click = {
          callback = self.dropbar_on_click_string:format(self.data.buf, self.data.win, i),
          name = 'heirline_dropbar',
        },
      }
      if i < #components then
        local sep = self.data.separator
        table.insert(child, {
          provider = sep.icon,
          hl = sep.icon_hl,
          on_click = {
              callback = self.dropbar_on_click_string:format(self.data.buf, self.data.win, i + 1),
          },
        })
      end
      table.insert(children, child)
    end
    self.child = self:new(children, 1)
  end,
  provider = function(self) return self.child:eval() end,
}
Clone this wiki locally