Skip to content

Commit

Permalink
feat: Allow custom terminal-mode highlight colors
Browse files Browse the repository at this point in the history
  • Loading branch information
polirritmico committed May 14, 2024
1 parent 5747617 commit df2682e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
22 changes: 14 additions & 8 deletions lua/monokai-nightasty/config.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
local M = {}

---@class Config
---@field dark_style_background string default, dark, transparent, #color
---@field light_style_background string default, dark, transparent, #color
---@field on_colors fun(colors: ColorScheme)
---@field on_highlights fun(highlights: Highlights, colors: ColorScheme)
---@field transparent boolean?
---@field terminal_colors? boolean|table|fun(colors: ColorScheme):table
local defaults = {
dark_style_background = "default", -- default, dark, transparent, #color
light_style_background = "default", -- default, dark, transparent, #color
terminal_colors = true, -- Set the colors used when opening a `:terminal`
color_headers = false, -- Enable header colors for each header level (h1, h2, etc.)
sidebars = { "qf", "help" }, -- Set a darker background on sidebar-like windows. For example: `["qf", "vista_kind", "terminal", "packer"]`
hl_styles = {
-- Style to be applied to different syntax groups. See `:help nvim_set_hl`
comments = { italic = true },
Expand All @@ -19,21 +20,26 @@ local defaults = {
floats = "default", -- default, dark, transparent
sidebars = "default", -- default, dark, transparent
},
sidebars = { "qf", "help" }, -- Set a darker background on sidebar-like windows. For example: `["qf", "vista_kind", "terminal", "packer"]`

hide_inactive_statusline = false, -- Hide inactive statuslines and replace them with a thin border instead. Should work with the standard **StatusLine** and **LuaLine**.
color_headers = false, -- Enable header colors for each header level (h1, h2, etc.)
dim_inactive = false, -- dims inactive windows
hide_inactive_statusline = false, -- Hide inactive statuslines and replace them with a thin border instead. Should work with the standard **StatusLine** and **LuaLine**.
lualine_bold = true, -- Lualine headers will be bold or regular.
lualine_style = "default", -- "dark", "light" or "default" (Follows dark/light style)
lualine_style = "default", -- "dark", "light" or "default" (default follows dark/light style)
markdown_header_marks = false, -- Add headers marks highlights (the `#` character) to Treesitter highlight query

-- Set the colors for terminal-mode. Could be a boolean, a table or a function that returns a table.
-- Could be `true` to enable defaults, a function like `function(colors) return { Normal = { fg = colors.fg_dark } }`
-- or directly a table like `{ Normal = { fg = "#e6e6e6" } }`.
terminal_colors = true,

--- You can override specific color groups to use other groups or a hex color
--- function will be called with a ColorScheme table
--- function will be called with the Monokai ColorScheme table
---@param colors ColorScheme
on_colors = function(colors) end,

--- You can override specific highlights to use other groups or a hex color
--- function will be called with a Highlights and ColorScheme table
--- function will be called with the Monokai Highlights and ColorScheme table
---@param highlights Highlights
---@param colors ColorScheme
on_highlights = function(highlights, colors) end,
Expand Down
35 changes: 29 additions & 6 deletions lua/monokai-nightasty/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ M.bg = "#2b2b2b" -- charcoal_medium
M.fg = "#ffffff"
M.brightness = 0.3

---@param c string
---@param c string
local function hexToRgb(c)
c = string.lower(c)
return { tonumber(c:sub(2, 3), 16), tonumber(c:sub(4, 5), 16), tonumber(c:sub(6, 7), 16) }
Expand Down Expand Up @@ -52,15 +52,17 @@ function M.highlight(group, hl)
end

---@param config Config
function M.autocmds(config)
local group = vim.api.nvim_create_augroup("monokai-nightasty", { clear = true })
---@param colors ColorScheme
function M.autocmds(config, colors)
local group = vim.api.nvim_create_augroup("MonokaiNightasty", { clear = true })

vim.api.nvim_create_autocmd("ColorSchemePre", {
group = group,
callback = function()
vim.api.nvim_del_augroup_by_id(group)
end,
})

local function set_whl()
local win = vim.api.nvim_get_current_win()
local whl = vim.split(vim.wo[win].winhighlight, ",")
Expand All @@ -76,12 +78,34 @@ function M.autocmds(config)
pattern = table.concat(config.sidebars, ","),
callback = set_whl,
})

if vim.tbl_contains(config.sidebars, "terminal") then
vim.api.nvim_create_autocmd("TermOpen", {
group = group,
callback = set_whl,
})
end

if config.terminal_colors then
local opt_type = type(config.terminal_colors)
local term_hl = opt_type == "table" and config.terminal_colors
or opt_type == "function" and config.terminal_colors(colors)
or {}

---@cast term_hl table
if next(term_hl) ~= nil then
vim.api.nvim_create_autocmd("TermOpen", {
group = group,
callback = function()
for name, hl in pairs(term_hl) do
local new_hl = "MonokaiNightastyTerminal" .. name
vim.api.nvim_set_hl(0, new_hl, hl)
vim.cmd.setlocal(string.format("winhighlight=%s:%s", name, new_hl))
end
end,
})
end
end
end

-- Simple string interpolation.
Expand Down Expand Up @@ -141,12 +165,11 @@ function M.load(theme)

M.syntax(theme.highlights)

-- vim.api.nvim_set_hl_ns(M.ns)
if theme.config.terminal_colors then
if theme.config.terminal_colors ~= false then
M.terminal(theme.colors)
end

M.autocmds(theme.config)
M.autocmds(theme.config, theme.colors)

vim.defer_fn(function()
M.syntax(theme.defer)
Expand Down

0 comments on commit df2682e

Please sign in to comment.