Skip to content

Commit

Permalink
refactor(palette, spec): cleanup and simplify, DRY, add types
Browse files Browse the repository at this point in the history
Remove duplicate/unneeded lines of code.
  • Loading branch information
tmillr committed Jun 16, 2024
1 parent d832925 commit cfc2d13
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 65 deletions.
69 changes: 26 additions & 43 deletions lua/github-theme/palette.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
local collect = require('github-theme.lib.collect')
local config = require('github-theme.config')

local M = {}

M.themes = {
'github_dark',
'github_dark_colorblind',
'github_dark_default',
'github_dark_dimmed',
'github_dark_high_contrast',
'github_dark_tritanopia',
'github_light',
'github_light_colorblind',
'github_light_default',
'github_light_high_contrast',
'github_light_tritanopia',
local M = {
themes = {
'github_dark',
'github_dark_colorblind',
'github_dark_default',
'github_dark_dimmed',
'github_dark_high_contrast',
'github_dark_tritanopia',
'github_light',
'github_light_colorblind',
'github_light_default',
'github_light_high_contrast',
'github_light_tritanopia',
},
}

local function override(color, ovr)
Expand All @@ -24,38 +21,24 @@ local function override(color, ovr)
return color
end

function M.load(name)
---@param theme? string
---@return table palette
function M.load(theme)
local ovr = require('github-theme.override').palettes
local result = {}

local function apply_ovr(key, palette)
return ovr[key] and override(palette, ovr[key]) or palette
end

if name then
local valid = collect.contains(M.themes, name)
local raw = valid and require('github-theme.palette.' .. name)
or require('github-theme.palette.' .. config.theme)
---@diagnostic disable-next-line: redefined-local
for _, theme in ipairs(theme and { theme } or M.themes) do
local raw = require('github-theme.palette.' .. theme)
local palette = raw.palette
palette = apply_ovr('all', palette)
palette = apply_ovr(name, palette)
palette = override(palette, ovr.all or {})
palette = override(palette, ovr[theme] or {})
palette.meta = raw.meta
palette.generate_spec = raw.generate_spec

return palette
else
local result = {}
for _, mod in ipairs(M.themes) do
local raw = require('github-theme.palette.' .. mod)
local palette = raw.palette
palette = apply_ovr('all', palette)
palette = apply_ovr(mod, palette)
palette.meta = raw.meta
palette.generate_spec = raw.generate_spec
result[mod] = palette
end

return result
result[theme] = palette
end

return theme and result[theme] or result
end

return M
37 changes: 15 additions & 22 deletions lua/github-theme/spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,37 +75,30 @@ local template = require('github-theme.util.template')
local M = {}

local function override(spec, palette, ovr)
if not ovr then
return spec
end
ovr = template.parse(ovr, palette)
return collect.deep_extend(spec, ovr)
end

function M.load(name)
---@param theme? string
---@return table spec
function M.load(theme)
local ovr = require('github-theme.override').specs
local result = {}

local function apply_ovr(key, spec, palette)
return ovr[key] and override(spec, palette, ovr[key]) or spec
end

if name then
local palette = require('github-theme.palette').load(name)
---@diagnostic disable-next-line: redefined-local
for _, theme in ipairs(theme and { theme } or require('github-theme.palette').themes) do
local palette = require('github-theme.palette').load(theme)
local spec = palette.generate_spec(palette)
spec = apply_ovr('all', spec, palette)
spec = apply_ovr(name, spec, palette)
spec = override(spec, palette, ovr.all)
spec = override(spec, palette, ovr[theme])
spec.palette = palette
return spec
else
local result = {}
local themes = require('github-theme.palette').themes
for _, mod in ipairs(themes) do
local palette = require('github-theme.palette').load(mod)
local spec = palette.generate_spec(palette)
spec = apply_ovr('all', spec, palette)
spec = apply_ovr(mod, spec, palette)
spec.palette = palette
result[mod] = spec
end
return result
result[theme] = spec
end

return theme and result[theme] or result
end

return M

0 comments on commit cfc2d13

Please sign in to comment.