Skip to content

Commit

Permalink
feat: implemented backend for theme variants
Browse files Browse the repository at this point in the history
refactor(colors, theme, ansi): moved existing setup to variants backend

refactor(plugins, languages): transitioned plugins & languages to
variant structure

feat(plugins): added custom integration loaders to preserve existing
configs
  • Loading branch information
ficcdaf committed Jan 26, 2025
1 parent 8979c1e commit f5859ee
Show file tree
Hide file tree
Showing 33 changed files with 369 additions and 157 deletions.
39 changes: 2 additions & 37 deletions lua/ashen/ansi.lua
Original file line number Diff line number Diff line change
@@ -1,42 +1,5 @@
local M = {}

local palette = {
"background",
"red_ember",
"orange_glow",
"orange_smolder",
"blue",
"g_4",
"g_3",
"g_2",
"g_5",
"red_ember",
"orange_glow",
"orange_smolder",
"blue",
"g_4",
"g_3",
"g_2",
}
-- local reference_palette = {
-- "#121212",
-- "#B14242",
-- "#D87C4A",
-- "#E49A44",
-- "#4A8B8B",
-- "#a7a7a7",
-- "#b4b4b4",
-- "#d5d5d5",
-- "#949494",
-- "#B14242",
-- "#D87C4A",
-- "#E49A44",
-- "#4A8B8B",
-- "#a7a7a7",
-- "#b4b4b4",
-- "#d5d5d5",
-- }

---@alias AnsiMap table<integer, HexCode> -- where integer [0, 15]

M.colors = nil
Expand All @@ -50,6 +13,8 @@ local generate_colors = function()
override = opts.terminal.colors or {}
end
local c = require("ashen.colors")
local variant = require("ashen.state").variant
local palette = require("ashen.variants." .. variant .. ".ansi")
for i, name in ipairs(palette) do
local new = ""
if override[i - 1] ~= nil then
Expand Down
92 changes: 5 additions & 87 deletions lua/ashen/colors.lua
Original file line number Diff line number Diff line change
@@ -1,88 +1,6 @@
---@alias ColorName string
local function get_palette()
local variant = require("ashen.state").variant
return require("ashen.variants." .. variant .. ".colors")
end

---@alias HexCode string # Hexadecimal color code preceded by a hashtag
---@alias Palette table<ColorName, HexCode>

---@type Palette
local M = {
-- used for errors
red_flame = "#C53030", -- Brightest, most intense red
-- standard red colors
red_glowing = "#DF6464", -- Slightly deeper glowing red
red_ember = "#B14242", -- Deep, smoldering ember red

-- standard orange colors
orange_glow = "#D87C4A", -- Bright, glowing orange
orange_blaze = "#C4693D", -- Vibrant blaze orange
orange_smolder = "#E49A44",

-- used for warnings
orange_golden = "#E5A72A",

-- NOTE: These reds are not widely used and will likely be fazed out in a future update.
red_kindling = "#BD4C4C", -- Light, warm red
red_burnt_crimson = "#A84848", -- Muted crimson red
red_brick = "#853D3D", -- Muted, earthy brick red
red_deep_ember = "#7A2E2E", -- Dark, deep ember red
red_ashen = "#6F2929", -- Cool, ashen red

-- standard misc colors
blue = "#4A8B8B", -- Muted teal, soft and unobtrusive
blue_dark = "#3A6E6E", -- Dark teal, ideal for subtle backgrounds
-- strong contender: #629C7D
green_light = "#629C7D",
green = "#1E6F54",

-- standard grayscale palette
background = "#121212",
-- duplicate for compatibility
-- g_0 will be removed in future
g_0 = "#e5e5e5",
g_1 = "#e5e5e5",
g_2 = "#d5d5d5",
g_3 = "#b4b4b4",
g_4 = "#a7a7a7",
g_5 = "#949494",
g_6 = "#737373",
g_7 = "#535353",
g_8 = "#323232",
g_9 = "#212121",
g_10 = "#1d1d1d",
g_11 = "#191919",
g_12 = "#151515",

-- WARNING: These colors are not part of the Ashen palette!
-- Rather, they're here as reference, to have a list of "normal"
-- colors that still fit nicely with the theme -- for example, in case
-- you *really* need something to be, say, purple, and want it to
-- look decent with the rest of the theme. Currently, the only place
-- these are used is in the mini.icons integration!
-- Please don't use them
-- if you're porting Ashen to another program -- I can't guarantee
-- that it'll look good! --ficcdaf
red = "#C53030", -- Brightest, most intense red
yellow = "#F4CA64", -- Bright sunflower yellow
orange = "#D87C4A", -- Bright, glowing orange
purple = "#7A3D82", -- Rich violet purple
pink = "#D1728C", -- Deep rose pink
brown = "#853D3D", -- Muted, earthy brick red
black = "#121212", -- Deep background black
white = "#FFFFFF", -- Pure white
gray = "#A7A7A7", -- Neutral mid-gray
cyan = "#6E91C4", -- Sky-like cyan
magenta = "#C9347C", -- Vibrant fuchsia
lime = "#8CD437", -- Bright lime green
teal = "#1A3F3F", -- Dark teal
navy = "#223A70", -- Deep navy blue
maroon = "#7A2E2E", -- Dark, deep ember red
olive = "#708238", -- Muted olive green
indigo = "#502E5F", -- Deep, muted indigo
violet = "#8E5E93", -- Soft mauve violet
gold = "#D7A933", -- Rich golden yellow
silver = "#D5D5D5", -- Soft, muted silver
beige = "#F5F5DC", -- Light, warm beige
aqua = "#4AC4C4", -- Bright aqua
coral = "#E492B4", -- Soft coral pink
}

return M
return get_palette()
3 changes: 2 additions & 1 deletion lua/ashen/languages/init.lua → lua/ashen/languages.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ local function get_autoload_languages()
local sub = require("ashen.submodules")
local languages = {}
local names = sub.get_valid_integrations("languages")
local variant = require("ashen.state").variant
for _, name in ipairs(names) do
if not util.is_in(disabled, name) then
table.insert(languages, require("ashen.languages." .. name))
table.insert(languages, require("ashen.variants." .. variant .. ".languages." .. name))
end
end
return languages
Expand Down
15 changes: 14 additions & 1 deletion lua/ashen/plugins/init.lua → lua/ashen/plugins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ end
local function get_autoload_plugins()
local plugins = {}
local names = M.get_valid_plugins()
local variant = require("ashen.state").variant
for _, name in ipairs(names) do
if not util.is_in(disabled, name) then
table.insert(plugins, require("ashen.plugins." .. name))
table.insert(plugins, require("ashen.variants." .. variant .. ".plugins." .. name))
end
end
return plugins
Expand All @@ -54,6 +55,17 @@ M.load_plugin = function(plugin)
require("ashen.submodules").load_submodule(plugin, "plugins")
end

---Registers `require` loaders in the form `ashen.plugins.plugin_name`
---pointing to `ashen.variants.variant_name.plugins.plugin_name`
M.register_loaders = function()
for _, plugin in ipairs(M.get_valid_plugins()) do
package.preload["ashen.plugins." .. plugin] = function()
local variant = require("ashen.state").variant
return require("ashen.variants." .. variant .. ".plugins." .. plugin)
end
end
end

---Load plugin integrations
M.load = function()
local p = require("ashen.state").opts.plugins
Expand All @@ -71,6 +83,7 @@ M.load = function()
for _, plugin in ipairs(plugins) do
M.load_plugin(plugin)
end
M.register_loaders()
end

return M
4 changes: 4 additions & 0 deletions lua/ashen/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ local M = {}
---Settings configuration.
---@class Options table
M.opts = {
-- default only option for now
variant = "default",
-- toggle text style options
---@type table<string, boolean>
style = {},
Expand Down Expand Up @@ -50,4 +52,6 @@ M.opts = {
},
}

M.variant = "default"

return M
8 changes: 5 additions & 3 deletions lua/ashen/submodules.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ local M = {}
M.get_valid_integrations = function(module)
-- we get the path of the plugins module file
local success, result = pcall(function()
return debug.getinfo(require("ashen." .. module).load).source:sub(2)
local variant = require("ashen.state").variant
return debug.getinfo(require("ashen.variants." .. variant .. "." .. module)).source:sub(2)
end)

local module_path
if success then
module_path = result
else
error("Fatal error in ashen.nvim: could not retrieve " .. module .. " path")
error("Fatal error in ashen.nvim: could not retrieve " .. module .. " path\n" .. result)
end
-- we get its parent directory
local mod_dir = vim.fn.fnamemodify(module_path, ":h")
Expand Down Expand Up @@ -58,13 +59,14 @@ end
---@param module string
M.load_submodule = function(integration, module)
local opts = require("ashen.state").opts
local variant = require("ashen.state").variant
local submodule
if type(integration) == "string" then
if not util.is_in(M.get_valid_integrations(module), integration) then
vim.notify("Ashen: " .. integration .. " is not a valid integration!", vim.log.levels.ERROR)
return
end
submodule = require("ashen." .. module .. "." .. integration)
submodule = require("ashen.variants." .. variant .. "." .. module .. "." .. integration)
elseif type(integration) == "table" then
submodule = integration
end
Expand Down
35 changes: 8 additions & 27 deletions lua/ashen/theme.lua
Original file line number Diff line number Diff line change
Expand Up @@ -202,51 +202,32 @@ M.transparent_bg = {
"LineNr",
}

-- ISSUE: for some reason, the merging only works properly
-- if the fg is set explicitly... will need to fix this later!
local style_presets = {
functions = {
["Function"] = { fg = "g_3", bold = true },
["@function"] = { fg = "g_3", bold = true },
["@function.builtin"] = { fg = "g_3", bold = true },
["@function.call"] = { fg = "g_3", bold = true },
["@function.macro"] = { fg = "g_3", bold = true },
["@lsp.type.function"] = { fg = "g_3", bold = true },
["@method"] = { fg = "g_3", bold = true },
["@method.call"] = { fg = "g_3", bold = true },
["@method.builtin"] = { fg = "g_3", bold = true },
["@lsp.type.method"] = { fg = "g_3", bold = true },
},
comments = {
["@comment"] = { fg = "g_6", italic = true },
["Comment"] = { fg = "g_6", italic = true },
},
}

M.load = function()
local util = require("ashen.util")
local opts = require("ashen.state").opts
local variant = require("ashen.state").variant
local theme = require("ashen.variants." .. variant .. ".theme")
vim.api.nvim_command(string.format("set background=%s", "dark"))
if opts.transparent then
for _, name in ipairs(M.transparent_bg) do
M.map[name] = util.remove_bg(M.map[name])
theme.map[name] = util.remove_bg(theme.map[name])
end
end
if opts.style_presets.bold_functions then
opts.hl.merge_override = vim.tbl_deep_extend("force", opts.hl.merge_override, style_presets.functions)
opts.hl.merge_override = vim.tbl_deep_extend("force", opts.hl.merge_override, theme.style_presets.functions)
end
if opts.style_presets.italic_comments then
opts.hl.merge_override = vim.tbl_deep_extend("force", opts.hl.merge_override, style_presets.comments)
opts.hl.merge_override = vim.tbl_deep_extend("force", opts.hl.merge_override, theme.style_presets.comments)
end

util.map_override(M.map, opts)
util.map_override(theme.map, opts)

-- set theme highlights
for name, spec in pairs(M.map) do
for name, spec in pairs(theme.map) do
util.hl(name, spec)
end
-- set theme links
for from, to in pairs(M.link) do
for from, to in pairs(theme.link) do
util.link(from, to)
end
-- set user links
Expand Down
3 changes: 2 additions & 1 deletion lua/ashen/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ end
---@param spec HighlightSpec|HighlightNormalized?
---@return boolean
M.hl = function(name, spec)
local theme = require("ashen.theme")
local variant = require("ashen.state").variant
local theme = require("ashen.variants." .. variant .. ".theme")
if spec == {} or spec == nil or theme.link[name] ~= nil then
return false
end
Expand Down
18 changes: 18 additions & 0 deletions lua/ashen/variants/default/ansi.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
return {
"background",
"red_ember",
"orange_glow",
"orange_smolder",
"blue",
"g_4",
"g_3",
"g_2",
"g_5",
"red_ember",
"orange_glow",
"orange_smolder",
"blue",
"g_4",
"g_3",
"g_2",
}
Loading

0 comments on commit f5859ee

Please sign in to comment.