Skip to content

Commit 1045168

Browse files
authored
fix: fix bad colorscheme name in 'ColorScheme' event (#186)
1 parent 4e450b2 commit 1045168

File tree

5 files changed

+706
-30
lines changed

5 files changed

+706
-30
lines changed

lua/colorbox.lua

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,14 @@ local function _primary_score(color_name, spec)
120120
or strings.startswith(handle2, normalized_color, { ignorecase = true })
121121
or strings.endswith(handle1, normalized_color, { ignorecase = true })
122122
or strings.endswith(handle2, normalized_color, { ignorecase = true })
123-
logger:debug(
124-
"|_primary_score| unique:%s, shortest:%s (current:%s, minimal:%s), matched:%s",
125-
vim.inspect(unique),
126-
vim.inspect(shortest),
127-
vim.inspect(current_name_len),
128-
vim.inspect(minimal_name_len),
129-
vim.inspect(matched)
130-
)
123+
-- logger:debug(
124+
-- "|_primary_score| unique:%s, shortest:%s (current:%s, minimal:%s), matched:%s",
125+
-- vim.inspect(unique),
126+
-- vim.inspect(shortest),
127+
-- vim.inspect(current_name_len),
128+
-- vim.inspect(minimal_name_len),
129+
-- vim.inspect(matched)
130+
-- )
131131
local n = 0
132132
if unique then
133133
n = n + 3
@@ -250,8 +250,8 @@ local function _init()
250250
for i, color_name in ipairs(FilteredColorNamesList) do
251251
FilteredColorNameToIndexMap[color_name] = i
252252
end
253-
logger:debug("|_init| FilteredColorNamesList:%s", vim.inspect(FilteredColorNamesList))
254-
logger:debug("|_init| FilteredColorNameToIndexMap:%s", vim.inspect(FilteredColorNameToIndexMap))
253+
-- logger:debug("|_init| FilteredColorNamesList:%s", vim.inspect(FilteredColorNamesList))
254+
-- logger:debug("|_init| FilteredColorNameToIndexMap:%s", vim.inspect(FilteredColorNameToIndexMap))
255255
end
256256

257257
local function _force_sync_syntax()
@@ -819,10 +819,8 @@ local function setup(opts)
819819

820820
vim.api.nvim_create_autocmd("ColorSchemePre", {
821821
callback = function(event)
822-
-- logger.debug(
823-
-- "|colorbox.setup| ColorSchemePre event:%s",
824-
-- vim.inspect(event)
825-
-- )
822+
local logger = logging.get("colorbox") --[[@as commons.logging.Logger]]
823+
logger:debug("|colorbox.setup| ColorSchemePre event:%s", vim.inspect(event))
826824
local ColorNameToColorSpecsMap =
827825
require("colorbox.db").get_color_name_to_color_specs_map()
828826
if type(event) ~= "table" or ColorNameToColorSpecsMap[event.match] == nil then
@@ -839,7 +837,6 @@ local function setup(opts)
839837
local home_dir = vim.fn["colorbox#base_dir"]()
840838
local ok, setup_err = pcall(Configs.setup[spec.handle], home_dir, spec)
841839
if not ok then
842-
local logger = logging.get("colorbox") --[[@as commons.logging.Logger]]
843840
logger:err(
844841
"failed to setup colorscheme:%s, error:%s",
845842
vim.inspect(spec.handle),
@@ -859,7 +856,9 @@ local function setup(opts)
859856
-- "|colorbox.setup| ColorScheme event:%s",
860857
-- vim.inspect(event)
861858
-- )
862-
_save_track(event.match)
859+
vim.schedule(function()
860+
_save_track(vim.g.colors_name)
861+
end)
863862
end,
864863
})
865864

lua/colorbox/commons/fileios.lua

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,51 @@ M.FileLineReader = FileLineReader
136136

137137
-- FileLineReader }
138138

139+
-- CachedFileReader {
140+
141+
--- @class commons.CachedFileReader
142+
--- @field filename string
143+
--- @field cache string?
144+
local CachedFileReader = {}
145+
146+
--- @param filename string
147+
--- @return commons.CachedFileReader
148+
function CachedFileReader:open(filename)
149+
local o = {
150+
filename = filename,
151+
cache = nil,
152+
}
153+
setmetatable(o, self)
154+
self.__index = self
155+
return o
156+
end
157+
158+
--- @param opts {trim:boolean?}?
159+
--- @return string?
160+
function CachedFileReader:read(opts)
161+
opts = opts or {}
162+
opts.trim = type(opts.trim) == "boolean" and opts.trim or false
163+
164+
if self.cache == nil then
165+
self.cache = M.readfile(self.filename)
166+
end
167+
if self.cache == nil then
168+
return self.cache
169+
end
170+
return opts.trim and vim.trim(self.cache) or self.cache
171+
end
172+
173+
--- @return string?
174+
function CachedFileReader:reset()
175+
local saved = self.cache
176+
self.cache = nil
177+
return saved
178+
end
179+
180+
M.CachedFileReader = CachedFileReader
181+
182+
-- CachedFileReader }
183+
139184
--- @param filename string
140185
--- @param opts {trim:boolean?}?
141186
--- @return string?
@@ -227,8 +272,8 @@ end
227272
--- @param filename string
228273
--- @return string[]|nil
229274
M.readlines = function(filename)
230-
local reader = M.FileLineReader:open(filename) --[[@as commons.FileLineReader]]
231-
if not reader then
275+
local ok, reader = pcall(M.FileLineReader.open, M.FileLineReader, filename) --[[@as commons.FileLineReader]]
276+
if not ok or reader == nil then
232277
return nil
233278
end
234279
local results = {}

lua/colorbox/commons/numbers.lua

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,8 @@ end
123123
--- @return number
124124
M.random = function(m, n)
125125
local rand_result, rand_err = require("colorbox.commons.uv").random(4)
126-
if rand_result == nil then
127-
if m == nil and n == nil then
128-
return math.random()
129-
elseif m ~= nil and n == nil then
130-
return math.random(m)
131-
else
132-
return math.random(m --[[@as integer]], n --[[@as integer]])
133-
end
134-
end
126+
assert(rand_result ~= nil, rand_err)
127+
135128
local bytes = {
136129
string.byte(rand_result --[[@as string]], 1, -1),
137130
}

0 commit comments

Comments
 (0)