Skip to content

Commit c633659

Browse files
authored
fix(colors): fallback to default colors on wrong config (#72)
1 parent 801e051 commit c633659

File tree

3 files changed

+12
-47
lines changed

3 files changed

+12
-47
lines changed

colors/one_monokai.lua

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
package.loaded["one_monokai.colors"] = nil
2-
31
require("one_monokai").setup()

lua/one_monokai/colors.lua

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1+
---@class palette
12
local colors = {}
23

3-
local config = require "one_monokai.config"
4-
local logs = require "one_monokai.logs"
5-
6-
---@class palette
7-
colors.default = {
4+
---@type palette
5+
local defaults = {
86
fg = "#abb2bf",
97
bg = "#282c34",
108
gray = "#676e7b",
@@ -30,6 +28,8 @@ colors.default = {
3028
none = "NONE",
3129
}
3230

31+
colors = vim.deepcopy(defaults)
32+
3333
---Convert hex value to rgb
3434
---@param color string
3535
local function hex2rgb(color)
@@ -74,14 +74,16 @@ end
7474
---@param value any #value of the color
7575
---@return string? #hex string or `nil` if invalid
7676
local function get_hex_value(name, value)
77+
local logs = require "one_monokai.logs"
78+
7779
local type_ok, err = pcall(vim.validate, {
7880
["colors(" .. name .. ")"] = { value, "string" },
7981
})
8082

8183
if not type_ok then
8284
logs.error.notify(err)
8385

84-
return nil
86+
return defaults[name]
8587
end
8688

8789
if value:lower() == "none" then
@@ -93,17 +95,16 @@ local function get_hex_value(name, value)
9395
if rgb == -1 then
9496
logs.error.notify("colors(%s): %q is not a valid color", name, value)
9597

96-
return nil
98+
return defaults[name]
9799
end
98100

99101
return ("#%06x"):format(rgb)
100102
end
101103

102-
---@type palette
103-
colors.extended = vim.deepcopy(colors.default)
104+
local config = require "one_monokai.config"
104105

105106
for name, value in pairs(config.colors) do
106-
colors.extended[name] = get_hex_value(name, value)
107+
colors[name] = get_hex_value(name, value)
107108
end
108109

109-
return colors.extended
110+
return colors

tests/spec/config_spec.lua

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -51,38 +51,4 @@ describe("Override config", function()
5151
assert.is_nil(comment_hl.italic)
5252
assert.is_nil(paremeter_hl.italic)
5353
end)
54-
55-
describe("with errors", function()
56-
local logs = require "one_monokai.logs"
57-
58-
before_each(function()
59-
logs.error.msg = ""
60-
end)
61-
62-
it("should log error on wrong colors", function()
63-
local wrong_config = {
64-
colors = {
65-
lmao = true,
66-
},
67-
}
68-
69-
one_monokai.setup(wrong_config)
70-
71-
assert.is_not.equal("", logs.error.msg)
72-
end)
73-
74-
it("should log error on wrong highlight groups", function()
75-
local wrong_config = {
76-
themes = function()
77-
return {
78-
lmao = true,
79-
}
80-
end,
81-
}
82-
83-
one_monokai.setup(wrong_config)
84-
85-
assert.is_not.equal("", logs.error.msg)
86-
end)
87-
end)
8854
end)

0 commit comments

Comments
 (0)