Skip to content

Commit

Permalink
refactor: remove the use of global variables/functions
Browse files Browse the repository at this point in the history
Somehow I didn't think about the fact that the user would be able to
access those from their config/neovim cmd.

Moved Config, Print_error, Return_error to mellifluous module.
  • Loading branch information
ramojus committed Nov 4, 2023
1 parent b7d963e commit b0e7d77
Show file tree
Hide file tree
Showing 18 changed files with 128 additions and 111 deletions.
5 changes: 3 additions & 2 deletions lua/mellifluous/cli.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ function M.create()
}
local color_sets = require('mellifluous.colors').get_color_sets_table()
options = vim.tbl_deep_extend('force', options, color_sets)
local config = require('mellifluous.config').config

vim.api.nvim_create_user_command('Mellifluous', function(input)
if options[input.args] == options.toggle_transparency then
require('mellifluous').setup({
transparent_background = {
enabled = not Config.transparent_background.enabled
enabled = not config.transparent_background.enabled
}
})
elseif color_sets[input.args] then
require('mellifluous').setup({
color_set = input.args
})
else
Print_error("unrecognised cli argument: '" .. input.args .. "'")
require('mellifluous').print_error("unrecognised cli argument: '" .. input.args .. "'")
return
end

Expand Down
16 changes: 9 additions & 7 deletions lua/mellifluous/colors/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ local function tbl_extend_non_nil(base_table, overlay_table)
end

local function get_color_overrides(is_bg_dark, color_set_name)
local color_overrides = vim.tbl_get(Config, color_set_name,
local config = require('mellifluous.config').config
local color_overrides = vim.tbl_get(config, color_set_name,
'color_overrides', is_bg_dark and 'dark' or 'light') or {}

for key, color in pairs(color_overrides) do
Expand All @@ -43,7 +44,7 @@ function M.get_is_bg_dark(color_set_name)
elseif is_light_set_available then
return false
else
Return_error("Required color set is either incomplete or missing")
require('mellifluous').return_error("Required color set is either incomplete or missing")
end
end

Expand All @@ -52,15 +53,16 @@ function M.get_ui_color_base_lightness(color_set_name, is_bg_dark)
end

function M.get_colors()
if not M.get_color_sets_table()[Config.color_set] then
Return_error("Color set '" .. Config.color_set .. "' not found")
local config = require('mellifluous.config').config
if not M.get_color_sets_table()[config.color_set] then
require('mellifluous').return_error("Color set '" .. config.color_set .. "' not found")
end

local color_overrides = get_color_overrides(Config.is_bg_dark, Config.color_set)
local color_overrides = get_color_overrides(config.is_bg_dark, config.color_set)

local color_set_functions = require('mellifluous.colors.sets.' .. Config.color_set)
local color_set_functions = require('mellifluous.colors.sets.' .. config.color_set)
local colors
if Config.is_bg_dark then
if config.is_bg_dark then
colors = color_set_functions.get_colors_dark(color_overrides.bg or color_set_functions.get_bg_dark())
else
colors = color_set_functions.get_colors_light(color_overrides.bg or color_set_functions.get_bg_light())
Expand Down
3 changes: 1 addition & 2 deletions lua/mellifluous/colors/sets/alduin.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local color = require('mellifluous.color')

local M = {}
local color = require('mellifluous.color')

function M.get_bg_dark()
return color.new('#1c1c1c')
Expand Down
14 changes: 8 additions & 6 deletions lua/mellifluous/colors/sets/mellifluous.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
local color = require('mellifluous.color')

local M = {}
local color = require('mellifluous.color')

local color_set_name = 'mellifluous'

local function get_is_neutral(mellifluous_config)
local config = require('mellifluous.config').config
-- for compatibility with configs from before color set specific config was supported
if Config.neutral ~= nil then
return Config.neutral
if config.neutral ~= nil then
return config.neutral
end
return mellifluous_config.neutral
end

function M.get_bg_dark()
local mellifluous_config = Config[color_set_name]
local config = require('mellifluous.config').config
local mellifluous_config = config[color_set_name]
local is_neutral = get_is_neutral(mellifluous_config)

local brightness = 11.5
Expand All @@ -30,7 +31,8 @@ function M.get_bg_dark()
end

function M.get_bg_light()
local mellifluous_config = Config[color_set_name]
local config = require('mellifluous.config').config
local mellifluous_config = config[color_set_name]
local is_neutral = get_is_neutral(mellifluous_config)

local brightness = 91.5
Expand Down
3 changes: 1 addition & 2 deletions lua/mellifluous/colors/sets/mountain.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local color = require('mellifluous.color')

local M = {}
local color = require('mellifluous.color')

function M.get_bg_dark()
return color.new('#0f0f0f')
Expand Down
3 changes: 1 addition & 2 deletions lua/mellifluous/colors/sets/tender.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local color = require('mellifluous.color')

local M = {}
local color = require('mellifluous.color')

function M.get_bg_dark()
return color.new('#282828')
Expand Down
4 changes: 3 additions & 1 deletion lua/mellifluous/colors/shades.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
local M = {}

function M.get_recipes()
local config = require('mellifluous.config').config

local shared_recipes = {
ui_red = { target = 'red', action = 'with_li', val = 'ui' },
ui_orange = { target = 'orange', action = 'with_li', val = 'ui' },
Expand All @@ -10,7 +12,7 @@ function M.get_recipes()
}
local recipes = {}

if Config.is_bg_dark then
if config.is_bg_dark then
recipes = {
fg2 = { target = 'fg', action = 'da', val = 16 },
fg3 = { target = 'fg', action = 'da', val = 32 },
Expand Down
8 changes: 3 additions & 5 deletions lua/mellifluous/config.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
local M = {}

Config = {}

local config = {
color_set = 'mellifluous',
plugins = {
Expand Down Expand Up @@ -98,7 +96,7 @@ local function read_only(table)
local metatable = {
__index = table,
__newindex = function()
Return_error('Attempt to update readonly config')
require('mellifluous').return_error('Attempt to update readonly config')
end,
}
setmetatable(proxy, metatable)
Expand All @@ -110,11 +108,11 @@ function M.setup(user_config)
config = vim.tbl_deep_extend('force', config, user_config or {})
end

function M.prepare_global()
function M.prepare()
process_color_set()
disable_disabled()

Config = read_only(config)
M.config = read_only(config)
end

return M
Loading

0 comments on commit b0e7d77

Please sign in to comment.