Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config)!: resolve config.variant in favour of uniq config.theme (DEV-132) #61

Merged
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
5 changes: 3 additions & 2 deletions .github/workflows/check_pr_meta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
check-pr-meta:
runs-on: ubuntu-20.04
env:
REGEX_VALIDATION_PATTERN: '^(feat|themes|highlights|utils|plugins|config|fix|refactor|chore|tests|docs|style|perf|build|ci|revert|deps)(\(.*\))?:\ .*\ \(DEV-[0-9]+\)$'
REGEX_VALIDATION_PATTERN: '^(feat|themes|highlights|utils|plugins|config|fix|refactor|chore|tests|docs|style|perf|build|ci|revert|deps)(\(.*\))?(!)?:\ .*\ \(DEV-[0-9]+\)$'

steps:
- name: Check PR Title
Expand Down Expand Up @@ -43,7 +43,8 @@ jobs:
fi

for commit_sha in $(git rev-list --no-merges "${commit_range}"); do
commit_message=$(git log --format=%B -n 1 "${commit_sha}")
commit_message=$(git log --format=%B -n 1 "${commit_sha}" | head -n 1)
echo "Checking commit message: \"${commit_message}\""
if [[ ! "${commit_message}" =~ ${{ env.REGEX_VALIDATION_PATTERN }} ]]; then
echo "Error: Commit message in wrong format: \"${commit_message}\""
echo "Please suffix the commit message with the issue number followed by a space, a conventional commit prefix, and a colon (e.g. feat: <commit message> (DEV-XXXX))"
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ return {
priority = 1000,
---@type BlackAtomCore.Config
opts = {
-- Set up your default theme and variant here
theme = "fall",
variant = "night",
-- Set up your default theme
theme = "terra_summer_night",
},
keys = {
-- You can also select a theme via a keymap
Expand Down
5 changes: 1 addition & 4 deletions colors/terra_fall_night.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
require("black-atom-core").setup({
theme = "fall",
variant = "night",
})
require("black-atom-core").setup({ theme = "terra_fall_night" })
require("black-atom-core").load_colorscheme("terra_fall_night")
5 changes: 1 addition & 4 deletions colors/terra_spring_night.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
require("black-atom-core").setup({
theme = "spring",
variant = "night",
})
require("black-atom-core").setup({ theme = "terra_spring_night" })
require("black-atom-core").load_colorscheme("terra_spring_night")
5 changes: 1 addition & 4 deletions colors/terra_summer_night.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
require("black-atom-core").setup({
theme = "summer",
variant = "night",
})
require("black-atom-core").setup({ theme = "terra_summer_night" })
require("black-atom-core").load_colorscheme("terra_summer_night")
5 changes: 1 addition & 4 deletions colors/terra_winter_night.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
require("black-atom-core").setup({
theme = "winter",
variant = "night",
})
require("black-atom-core").setup({ theme = "terra_winter_night" })
require("black-atom-core").load_colorscheme("terra_winter_night")
3 changes: 1 addition & 2 deletions lua/black-atom-core/colors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ local M = {}
---@return BlackAtomCore.Colors
function M.get()
local theme = BlackAtomCoreConfig.theme
local variant = BlackAtomCoreConfig.variant

return require(require("black-atom-core.themes")[theme].variants[variant].path)
return require(require("black-atom-core.themes")[theme].path)
end

return M
4 changes: 2 additions & 2 deletions lua/black-atom-core/config.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
local M = {}

-- Escaped plugin name. Used to find its own installation path.
M.plugin_name = "black%-atom%-core%.nvim"

---@type BlackAtomCore.Config
M.default_config = {
debug = false,
theme = "spring",
variant = "night",
theme = "terra_spring_night",
term_colors = true,
styles = {
ending_tildes = false,
Expand Down
4 changes: 2 additions & 2 deletions lua/black-atom-core/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ local lib = require("black-atom-core.lib")

local M = {}

---@param colorscheme_name BlackAtomCore.Config.ColorSchemeName
---TODO: Handle `vim.background`: https://linear.app/black-atom-industries/issue/DEV-119/proper-handling-of-vimoptbackground-=-darklight
---@param colorscheme_name BlackAtomCore.Config.ThemeKey
---@return nil
function M.load_colorscheme(colorscheme_name)
lib.hls.reset()

lib.config.sync_vim_bg_with_variant(BlackAtomCoreConfig.variant)
vim.o.termguicolors = true
vim.g.colors_name = colorscheme_name

Expand Down
145 changes: 16 additions & 129 deletions lua/black-atom-core/lib/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,24 @@ function M.set_config(options)
BlackAtomCoreConfig = vim.tbl_deep_extend("force", BlackAtomCoreConfig, options)
end

---Syncs `vim.o.background` of Vim with `variant` of BlackAtomCore.Config
---@param variant BlackAtomCore.Config.ThemeVariantKey
---@return nil
function M.sync_vim_bg_with_variant(variant)
if variant == "day" then
vim.o.background = "light"
else
vim.o.background = "dark"
end
end

---Syncs `variant` of BlackAtomCore.Config with `vim.o.background` of Vim
---@param background "light" | "dark"
---@return nil
function M.sync_variant_with_vim_bg(background)
if background == "light" then
M.set_config({
variant = "day",
})
end
end

---Notify the user about the status of the current theme
---@param themes BlackAtomCore.Config.ThemeDefinitionMap
---@param theme_key? BlackAtomCore.Config.ThemeKey
---@param variant_key? BlackAtomCore.Config.ThemeVariantKey
---@param on_allow? fun(): nil Callback which is called when the theme is allowed to use
---@return nil
---@diagnostic disable-next-line: redefined-local
function M.dev_status_warning(themes, theme_key, variant_key, on_allow)
function M.dev_status_warning(themes, theme_key, on_allow)
theme_key = theme_key or BlackAtomCoreConfig.theme
variant_key = variant_key or BlackAtomCoreConfig.variant

local theme = themes[theme_key]
local status = theme.variants[variant_key].status
local theme = require("black-atom-core.lib.themes").get_theme_definition(themes, theme_key)
local status = theme.status

if status == "development" then
local error_message = string.format(
"Theme '%s %s' is currently in %s status.\nThis is not ready to be used. Theme switch aborted.",
theme.label,
theme.variants[variant_key].label,
theme.variants[variant_key].status
theme.label,
theme.status
)

require("black-atom-core.lib.ui").notify(error_message, vim.log.levels.ERROR, {
Expand All @@ -66,8 +42,8 @@ function M.dev_status_warning(themes, theme_key, variant_key, on_allow)
local error_message = string.format(
"Theme '%s %s' is currently in %s status.\nBugs and Errors are to be expected. Use at your own risk.",
theme.label,
theme.variants[variant_key].label,
theme.variants[variant_key].status
theme.key.label,
theme.key.status
)

require("black-atom-core.lib.ui").notify(error_message, vim.log.levels.WARN, {
Expand Down Expand Up @@ -102,25 +78,13 @@ function M.select_theme()
prompt = "Black Atom Core - Please select a Theme: ",
format_item = function(theme_key)
---@type BlackAtomCore.Config.ThemeDefinition
local theme = themes[theme_key]

---@type BlackAtomCore.Config.ThemeVariantKey
local variant_key = BlackAtomCoreConfig.variant
local theme = require("black-atom-core.lib.themes").get_theme_definition(themes, theme_key)

return string.format(
"%s %s %s %s",
theme.icon,
theme.variants[variant_key].icon,
theme.label,
theme.variants[variant_key].label
)
return string.format("%s %s %s %s", theme.icon, theme.label)
end,
},
---@param selected_theme_key BlackAtomCore.Config.ThemeKey|nil
function(selected_theme_key)
---@type BlackAtomCore.Config.ThemeDefinition
local themeConfig = themes[selected_theme_key]

if selected_theme_key == nil then
require("black-atom-core.lib.ui").notify("Aborted: No Theme selected!", vim.log.levels.INFO, {
title = "Warning",
Expand All @@ -129,103 +93,26 @@ function M.select_theme()
return
end

M.dev_status_warning(themes, selected_theme_key, BlackAtomCoreConfig.variant, function()
---@type BlackAtomCore.Config.ThemeDefinition
local theme = require("black-atom-core.lib.themes").get_theme_definition(themes, selected_theme_key)

M.dev_status_warning(themes, selected_theme_key, function()
M.set_config({
theme = selected_theme_key,
})

local colorscheme_name = require("black-atom-core.lib.themes").get_variant_value(
themes,
selected_theme_key,
BlackAtomCoreConfig.variant,
"colorscheme_name"
)

require("black-atom-core").load_colorscheme(colorscheme_name)

require("black-atom-core.lib.ui").notify(
"You selected '" .. themeConfig.label .. "'!",
vim.log.levels.INFO,
{
title = themeConfig.label,
icon = themeConfig.icon,
}
)
end)
end
)
end

---Select a variant during runtime
---@return nil
function M.select_variant()
---@param theme BlackAtomCore.Config.ThemeDefinition
---@param variant_key BlackAtomCore.Config.ThemeVariantKey
local function formatted_variant(theme, variant_key)
return string.format(
"%s %s %s %s",
theme.icon,
theme.variants[variant_key].icon,
theme.label,
theme.variants[variant_key].label
)
end

local sorted_variant_keys = require("black-atom-core.lib.themes").get_sorted_variant_keys(themes)

-- filter out current variant_key from the list of variants
local variant_select_items = vim.tbl_filter(function(variant_key)
return variant_key ~= BlackAtomCoreConfig.variant
end, sorted_variant_keys)

vim.ui.select(
variant_select_items,
{
prompt = "Black Atom Core - Please select a variant for your current theme: ",
format_item = function(variant_key)
return formatted_variant(themes[BlackAtomCoreConfig.theme], variant_key)
end,
},

---@param selected_variant_key BlackAtomCore.Config.ThemeVariantKey|nil
function(selected_variant_key)
if selected_variant_key == nil then
require("black-atom-core.lib.ui").notify("Aborted: No variant selected!", vim.log.levels.INFO, {
title = "Info",
icon = " ",
require("black-atom-core.lib.ui").notify("You selected '" .. theme.label .. "'!", vim.log.levels.INFO, {
title = theme.label,
icon = theme.icon,
})
return
end

---@type BlackAtomCore.Config.ThemeKey
local current_theme_key = BlackAtomCoreConfig.theme
local current_theme = themes[current_theme_key]
local formatted_variant_label = formatted_variant(current_theme, selected_variant_key)

M.dev_status_warning(themes, current_theme_key, selected_variant_key, function()
require("black-atom-core.lib.ui").notify(
string.format("You selected '%s'!", formatted_variant_label),
vim.log.levels.INFO,
{
title = formatted_variant_label,
icon = current_theme.icon,
}
)

M.set_config({
variant = selected_variant_key,
})

M.sync_vim_bg_with_variant(BlackAtomCoreConfig.variant)

local colorscheme_name = require("black-atom-core.lib.themes").get_variant_value(
themes,
BlackAtomCore.Config.theme,
selected_variant_key,
"colorscheme_name"
)

require("black-atom-core").load_colorscheme(colorscheme_name)
end)
end
)
Expand Down
34 changes: 19 additions & 15 deletions lua/black-atom-core/lib/themes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,35 @@ function M.get_ordered_theme_keys(themes)
return ordered_theme_keys
end

--- Get all unique variant keys from all themes
--- Get all unique theme keys from all theme definitions
---@param themes BlackAtomCore.Config.ThemeDefinitionMap
---@return BlackAtomCore.Config.ThemeVariantKey[]
function M.get_sorted_variant_keys(themes)
local sorted_variant_keys = {}
---@return BlackAtomCore.Config.ThemeKey[]
function M.get_sorted_theme_keys(themes)
local sorted_theme_keys = {}

for _, theme in pairs(themes) do
for _, variant in pairs(theme.variants) do
-- if the variant is not already in the array, add it
if not vim.tbl_contains(sorted_variant_keys, variant.key) then
table.insert(sorted_variant_keys, variant.key)
end
for theme_key in pairs(themes) do
if not vim.tbl_contains(sorted_theme_keys, theme_key) then
table.insert(sorted_theme_keys, theme_key)
end
end

return sorted_variant_keys
return sorted_theme_keys
end

---Get a property value from a theme variant
---Get a property value from a theme definition
---@param themes BlackAtomCore.Config.ThemeDefinitionMap
---@param theme_key BlackAtomCore.Config.ThemeKey
---@param variant_key BlackAtomCore.Config.ThemeVariantKey
---@param property string
function M.get_variant_value(themes, theme_key, variant_key, property)
return themes[theme_key].variants[variant_key][property]
function M.get_variant_value(themes, theme_key, property)
return themes[theme_key][property]
end

---Get the theme definition for a given theme key
---@param themes BlackAtomCore.Config.ThemeDefinitionMap
---@param theme_key BlackAtomCore.Config.ThemeKey
---@return BlackAtomCore.Config.ThemeDefinition
function M.get_theme_definition(themes, theme_key)
return themes[theme_key]
end

return M
Loading
Loading