Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ require("one_monokai").setup({
| ------------- | -------------------------------- | -------------------------- | ---------------- |
| `transparent` | Enables a transparent background | `boolean` | N/A |
| `colors` | Custom color definitions | `table<string, string>` | N/A |
| `themes` | Custom highlight groups | `function(colors): tables` | `:h nvim_set_hl` |
| `highlights` | Custom highlight groups | `function(colors): table` | `:h nvim_set_hl` |
| `italics` | Enables italic | `boolean` | N/A |

### Default Configuration
Expand All @@ -64,7 +64,7 @@ require("one_monokai").setup({
require("one_monokai").setup({
transparent = false,
colors = {},
themes = function(colors)
highlights = function(colors)
return {}
end,
italics = true,
Expand All @@ -73,10 +73,10 @@ require("one_monokai").setup({

### Customization Examples

#### Available Colors and Themes
#### Available Colors and Highlights

1. [Colors](lua/one_monokai/colors.lua)
2. [Themes](lua/one_monokai/themes/groups.lua)
2. [Highlights](lua/one_monokai/highlights/groups.lua)

The color module provides two methods, `darken` and `lighten`, to blend colors based on an alpha value (between 0 and 1). You can use them as follows:

Expand All @@ -94,7 +94,7 @@ require("one_monokai").setup({
lmao = "#ffffff", -- Define a new color
pink = "#ec6075", -- Override a default color
},
themes = function(colors)
highlights = function(colors)
-- Customize highlight groups
-- The key-value pairs are passed to "nvim_set_hl"
return {
Expand Down
8 changes: 4 additions & 4 deletions lua/one_monokai/colors.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---@class colors
local colors = {}

---@type colors
---@class colors
local defaults = {
fg = "#abb2bf",
bg = "#282c34",
Expand All @@ -28,6 +27,7 @@ local defaults = {
none = "NONE",
}

---@type colors
colors = vim.deepcopy(defaults)

---Convert hex value to rgb
Expand Down Expand Up @@ -81,7 +81,7 @@ local function get_hex_value(name, value)
})

if not type_ok then
logs.error.notify(err)
logs.notify.error(err)

return defaults[name]
end
Expand All @@ -93,7 +93,7 @@ local function get_hex_value(name, value)
local rgb = vim.api.nvim_get_color_by_name(value)

if rgb == -1 then
logs.error.notify("colors(%s): %q is not a valid color", name, value)
logs.notify.error("colors(%s): %q is not a valid color", name, value)

return defaults[name]
end
Expand Down
41 changes: 30 additions & 11 deletions lua/one_monokai/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,46 @@ local config = {}
---@class options
---@field transparent boolean #Whether to enable transparent background
---@field colors colors #Custom colors
---@field themes fun(colors:colors):groups #Custom highlight groups
---@field italics boolean #Whether to italicize some highlight groups
---@field highlights? fun(colors:colors):groups #Custom highlight groups
---@field italics boolean #Whether to apply italics to certain highlight groups
local defaults = {
transparent = false,
colors = {},
themes = function(_)
return {}
end,
---@deprecated
themes = nil,
highlights = nil,
italics = true,
}

config.options = vim.deepcopy(defaults)
---@type options
local options = vim.deepcopy(defaults)

---Extend default with user's config
---@param opts options
function config:extend(opts)
self.options = vim.tbl_deep_extend("force", self.options, opts or {})
function config.extend(opts)
if not opts or vim.tbl_isempty(opts) then
return
end

local logs = require "one_monokai.logs"

if opts.themes then
logs.notify.warning(
"config: %q option has been deprecated and will be removed soon. Please update your config to use %q instead.",
"themes",
"highlights"
)

opts.highlights = opts.themes
end

options = vim.tbl_deep_extend("force", options, opts)
end

return setmetatable(config, {
__index = function(_, key)
return config.options[key]
setmetatable(config, {
__index = function(_, k)
return options[k]
end,
})

return config
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
---@class groups
local groups = {}

local colors = require "one_monokai.colors"
local config = require "one_monokai.config"

---@type groups
---@class groups
local defaults = {
Boolean = { fg = colors.cyan },
Character = { fg = colors.yellow },
Expand Down Expand Up @@ -582,6 +581,7 @@ for _, semantic_group in ipairs(vim.fn.getcompletion("@lsp", "highlight")) do
end
end

---@type groups
groups = vim.deepcopy(defaults)

return groups
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local themes = {}
local highlights = {}

---Set highlight groups
---@param groups groups
---@param groups groups Highlight groups
local function set_highlight(groups)
local logs = require "one_monokai.logs"
local set_hl = vim.api.nvim_set_hl
Expand All @@ -10,18 +10,22 @@ local function set_highlight(groups)
local status_ok, err = pcall(set_hl, 0, name, attrs)

if not status_ok then
logs.error.notify("themes(%s): %s", name, err)
logs.notify.error("highlights(%s): %s", name, err)
end
end
end

function themes.load()
---Load all highlight groups
function highlights.load()
local config = require "one_monokai.config"
local colors = require "one_monokai.colors"
local default = require "one_monokai.themes.groups"
local default = require "one_monokai.highlights.groups"

set_highlight(default)
set_highlight(config.themes(colors))

if config.highlights then
set_highlight(config.highlights(colors))
end
end

return themes
return highlights
6 changes: 3 additions & 3 deletions lua/one_monokai/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local M = {}
function M.setup(opts)
local set = vim.cmd
local config = require "one_monokai.config"
local themes = require "one_monokai.themes"
local highlights = require "one_monokai.highlights"

if vim.g.colors_name then
set.hi "clear"
Expand All @@ -17,8 +17,8 @@ function M.setup(opts)
vim.o.termguicolors = true
vim.g.colors_name = "one_monokai"

config:extend(opts)
themes.load()
config.extend(opts)
highlights.load()
end

return M
31 changes: 20 additions & 11 deletions lua/one_monokai/logs.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
---@class logs
local logs = {}

logs.error = {
---Display error message.
---
---NOTE: This function receives params that will be passed to `string.format`.
---@param s string
---@param ... any
notify = function(s, ...)
local message = string.format(s, ...)
---Display a notification to the user
---@param log_level string
---@param s string
local function notify(log_level, s, ...)
local message = string.format(s, ...)

vim.notify_once(message, vim.log.levels.ERROR, {
title = "One Monokai",
})
vim.notify_once(message, log_level, {
title = "One Monokai",
})
end

---@class notify
---@field error fun(s:string, ...) #Show error message
---@field warning fun(s: string, ...) #Show warning message
logs.notify = {
error = function(s, ...)
notify(vim.log.levels.ERROR, s, ...)
end,
warning = function(s, ...)
notify(vim.log.levels.WARNING, s, ...)
end,
}

Expand Down
10 changes: 5 additions & 5 deletions tests/spec/config_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("Override config", function()
pink = "#61afef",
lmao = "#dedeff",
},
themes = function(c)
highlights = function(c)
return {
Normal = { fg = c.lmao, italic = true },
}
Expand All @@ -28,7 +28,7 @@ describe("Override config", function()
it("should change the default config", function()
assert.is_false(config.transparent)
assert.are.same(expected.colors, config.colors)
assert.are.same(expected.themes(colors), config.themes(colors))
assert.are.same(expected.highlights(colors), config.highlights(colors))
end)

it("should change default colors", function()
Expand All @@ -37,7 +37,7 @@ describe("Override config", function()
assert.equal(expected.colors.lmao, colors.lmao)
end)

it("should change default themes", function()
it("should change default highlights", function()
local hl = vim.api.nvim_get_hl(0, { name = "Normal" })

assert.equal(expected.colors.lmao, ("#%06x"):format(hl.fg))
Expand All @@ -58,7 +58,7 @@ describe("Override wrong config", function()
colors = {
aqua = true,
},
themes = function()
highlights = function()
return {
Normal = { fg = "lmao", italic = true },
}
Expand All @@ -78,7 +78,7 @@ describe("Override wrong config", function()
end)

it("should fallback to default highlights", function()
local default = require "one_monokai.themes.groups"
local default = require "one_monokai.highlights.groups"
local hl = vim.api.nvim_get_hl(0, { name = "Normal" })

assert.equal(default.Normal.fg, ("#%06x"):format(hl.fg))
Expand Down
Loading