Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

neovim/lua/bugabinga/pivi/highlight.lua

Raw
--- What the parts of a conversation look like.
--- pivi names the roles and leaves the colours to the colour scheme: every
--- group below is a default, so a theme that defines one wins without pivi
--- knowing anything about it.
---
--- The intent, which any theme should keep: a user message is the one thing
--- that has to be findable while scrolling, so it carries a background and a
--- solid bar. Everything else stays quiet, and tool work is quietest of all.

local GROUPS = {
  PiviUser = { from = 'CursorLine', take = 'bg', },
  PiviUserBar = { from = 'Special', take = 'fg', bold = true, },
  PiviUserLabel = { from = 'Special', take = 'fg', bold = true, },
  PiviAssistantLabel = { from = 'Function', take = 'fg', },
  PiviTool = { from = 'NonText', take = 'fg', },
  PiviToolIcon = { from = 'Function', take = 'fg', },
  PiviToolOutcome = { from = 'NonText', take = 'fg', },
  PiviError = { from = 'ErrorMsg', take = 'fg', },
  PiviErrorIcon = { from = 'ErrorMsg', take = 'fg', bold = true, },
}

local resolved = function( name )
  local ok, value = pcall( vim.api.nvim_get_hl, 0, { name = name, link = false, } )
  if not ok then return {} end
  return value or {}
end

--- Defines every pivi group that the colour scheme has not.
local apply = function()
  for name, recipe in pairs( GROUPS ) do
    local source = resolved( recipe.from )
    local colour = source[recipe.take]

    local options = { default = true, bold = recipe.bold, }
    if colour then options[recipe.take] = colour end

    pcall( vim.api.nvim_set_hl, 0, name, options )
  end
end

local names = function()
  local found = {}
  for name in pairs( GROUPS ) do table.insert( found, name ) end
  table.sort( found )
  return found
end

apply()

vim.api.nvim_create_autocmd( 'ColorScheme', {
  group = vim.api.nvim_create_augroup( 'pivi_highlight', { clear = true, } ),
  desc = 'Keep pivi colours in step with the colour scheme.',
  callback = apply,
} )

return {
  groups = GROUPS,
  names = names,
  apply = apply,
}