From 32727039a97ece7ee4eb6d889be0b3e50b570daa Mon Sep 17 00:00:00 2001 From: shadmansaleh <13149513+shadmansaleh@users.noreply.github.com> Date: Sat, 14 Aug 2021 12:02:30 +0600 Subject: [PATCH] |DEPRECATION| enhance(themes): Auto colorscheme detection and more - `auto` theme now auto loads available theme for colorschme based on g:color_name . If that fails then it generates a theme for that colorschme like before. - Theme from external source is preferred over bundled version in lualine . This means if user has a colorschme that provided the same theme as lualine the one from colorschme will be loaded. - Default value of theme is changed to `auto` from `gruvbox`. - Now when users theme from config fails lualine will fallbavk to `auto`. If `auto` fails then lualine will fallback to `gruvbox` - Some themes have been renamed to their actual name (g:color_name). older names have been deprecated. [DEPRECATION] oceanicnext -> OceanicNext papercolor -> PaperColor tomorrow -> Tomorrow gruvbox_material -> gruvbox-material modus_vivendi -> modus-vivendi - A few more adaptive themes added (aye, gruvbox, iceberg) - Updated THEMES.md to reflect changes in theme name --- THEMES.md | 26 ++++++-- lua/lualine/config.lua | 2 +- lua/lualine/init.lua | 66 +++++++++++++++++-- .../{oceanicnext.lua => OceanicNext.lua} | 0 .../themes/{papercolor.lua => PaperColor.lua} | 0 .../themes/{tomorrow.lua => Tomorrow.lua} | 0 lua/lualine/themes/auto.lua | 9 +++ lua/lualine/themes/ayu.lua | 7 ++ ...vbox_material.lua => gruvbox-material.lua} | 0 lua/lualine/themes/gruvbox.lua | 53 ++------------- lua/lualine/themes/gruvbox_dark.lua | 48 ++++++++++++++ lua/lualine/themes/iceberg.lua | 7 ++ .../{modus_vivendi.lua => modus-vivendi.lua} | 0 lua/lualine/utils/loader.lua | 22 ++++++- lua/tests/spec/config_spec.lua | 2 +- 15 files changed, 181 insertions(+), 61 deletions(-) rename lua/lualine/themes/{oceanicnext.lua => OceanicNext.lua} (100%) rename lua/lualine/themes/{papercolor.lua => PaperColor.lua} (100%) rename lua/lualine/themes/{tomorrow.lua => Tomorrow.lua} (100%) create mode 100644 lua/lualine/themes/ayu.lua rename lua/lualine/themes/{gruvbox_material.lua => gruvbox-material.lua} (100%) create mode 100644 lua/lualine/themes/gruvbox_dark.lua create mode 100644 lua/lualine/themes/iceberg.lua rename lua/lualine/themes/{modus_vivendi.lua => modus-vivendi.lua} (100%) diff --git a/THEMES.md b/THEMES.md index 6c21cec..6b9886e 100644 --- a/THEMES.md +++ b/THEMES.md @@ -37,6 +37,10 @@ lualine try to match your colorscheme.

+### ayu +It's a combination of ayu_light, ayu_dark & ayu_mirage. It loads one of these +them based you your `g:ayucolor` option. + ### codedark

@@ -61,7 +65,7 @@ lualine try to match your colorscheme.

-### gruvbox +### gruvbox_dark

@@ -77,7 +81,11 @@ lualine try to match your colorscheme.

-### gruvbox_material +### gruvbox +It's a combination of gruvbox_light and gruvbox_dark. It loads either of +them based you your `background` option. + +### gruvbox-material

@@ -109,6 +117,10 @@ lualine try to match your colorscheme.

+### iceberg +It's a combination of icrberg_light and icrberg_dark. It loads either of +them based you your `background` option. + ### jellybeans

@@ -125,7 +137,7 @@ lualine try to match your colorscheme.

-### modus_vivendi +### modus-vivendi

@@ -157,7 +169,7 @@ lualine try to match your colorscheme.

-### oceanicnext +### OceanicNext

@@ -205,6 +217,10 @@ lualine try to match your colorscheme.

+### PaperColor +It's a combination of papercolor_light and papercolor_dark. It loads either of +them based you your `background` option. + ### powerline

@@ -237,7 +253,7 @@ lualine try to match your colorscheme.

-### tomorrow +### Tomorrow

diff --git a/lua/lualine/config.lua b/lua/lualine/config.lua index e387e94..ad2c189 100644 --- a/lua/lualine/config.lua +++ b/lua/lualine/config.lua @@ -3,7 +3,7 @@ local config = { options = { icons_enabled = true, - theme = 'gruvbox', + theme = 'auto', component_separators = {'', ''}, section_separators = {'', ''}, disabled_filetypes = {} diff --git a/lua/lualine/init.lua b/lua/lualine/init.lua index a2574eb..dd4865b 100644 --- a/lua/lualine/init.lua +++ b/lua/lualine/init.lua @@ -171,20 +171,74 @@ end local function tabline() return statusline(config.tabline, true) end +local function check_theme_name_deprecation(theme_name) + local deprection_table = { + oceanicnext = 'OceanicNext', + papercolor = 'PaperColor', + tomorrow = 'Tomorrow', + gruvbox_material = 'gruvbox-material', + modus_vivendi = 'modus-vivendi', + } + if deprection_table[theme_name] then + local correct_name = deprection_table[theme_name] + utils_notices.add_notice(string.format([[ +### options.theme +You're using `%s` as theme name . +It has recently been renamed to `%s`. +Please update your config to follow that. + +You have something like this in your config. +```lua +options = { + theme = '%s' +} +``` + +You'll have to change it to something like this. +```lua +options = { + theme = '%s' +} +``` + +]], theme_name, correct_name, theme_name, correct_name)) + return correct_name + end + return theme_name +end + +local function notify_theme_error(theme_name) + local message_template = theme_name ~= 'auto' and [[ +### options.theme +Theme `%s` not found, falling back to `auto`. Check if spelling is right. +]] or [[ +### options.theme +Theme `%s` failed, falling back to `gruvbox`. +This shouldn't happen. +Please report the issue at https://github.com/shadmansaleh/lualine.nvim/issues . +Also provide what colorscheme you're using. +]] + utils_notices.add_notice(string.format(message_template, theme_name)) +end + local function setup_theme() local function get_theme_from_config() local theme_name = config.options.theme if type(theme_name) == 'string' then - package.loaded['lualine.themes.'..theme_name] = nil - local ok, theme = pcall(require, 'lualine.themes.' .. theme_name) - if ok then return theme end + theme_name = check_theme_name_deprecation(theme_name) + local ok, theme = pcall(loader.load_theme, theme_name) + if ok and theme then return theme end elseif type(theme_name) == 'table' then -- use the provided theme as-is return config.options.theme end - vim.api.nvim_err_writeln('theme ' .. tostring(theme_name) .. - ' not found, defaulting to gruvbox') - return require 'lualine.themes.gruvbox' + if theme_name ~= 'auto' then + notify_theme_error(theme_name) + local ok, theme = pcall(loader.load_theme, 'auto') + if ok and theme then return theme end + end + notify_theme_error('auto') + return loader.load_theme('gruvbox') end local theme = get_theme_from_config() highlight.create_highlight_groups(theme) diff --git a/lua/lualine/themes/oceanicnext.lua b/lua/lualine/themes/OceanicNext.lua similarity index 100% rename from lua/lualine/themes/oceanicnext.lua rename to lua/lualine/themes/OceanicNext.lua diff --git a/lua/lualine/themes/papercolor.lua b/lua/lualine/themes/PaperColor.lua similarity index 100% rename from lua/lualine/themes/papercolor.lua rename to lua/lualine/themes/PaperColor.lua diff --git a/lua/lualine/themes/tomorrow.lua b/lua/lualine/themes/Tomorrow.lua similarity index 100% rename from lua/lualine/themes/tomorrow.lua rename to lua/lualine/themes/Tomorrow.lua diff --git a/lua/lualine/themes/auto.lua b/lua/lualine/themes/auto.lua index 6a80744..0b5cc8d 100644 --- a/lua/lualine/themes/auto.lua +++ b/lua/lualine/themes/auto.lua @@ -1,4 +1,13 @@ local utils = require 'lualine.utils.utils' +local loader = require 'lualine.utils.loader' + +local color_name = vim.g.colors_name +if color_name then + -- Check if there's a theme for current colorscheme + -- If there is load that instead of genarating a new one + local ok, theme = pcall(loader.load_theme, color_name) + if ok and theme then return theme end +end --------------- -- Constents -- diff --git a/lua/lualine/themes/ayu.lua b/lua/lualine/themes/ayu.lua new file mode 100644 index 0000000..380e3a4 --- /dev/null +++ b/lua/lualine/themes/ayu.lua @@ -0,0 +1,7 @@ +-- Copyright (c) 2020-2021 shadmansaleh +-- MIT license, see LICENSE for more details. +-- Credit: itchyny(lightline) +-- License: MIT License +local style = vim.g.ayucolor or 'dark' + +return require('lualine.themes.ayu_' .. style) diff --git a/lua/lualine/themes/gruvbox_material.lua b/lua/lualine/themes/gruvbox-material.lua similarity index 100% rename from lua/lualine/themes/gruvbox_material.lua rename to lua/lualine/themes/gruvbox-material.lua diff --git a/lua/lualine/themes/gruvbox.lua b/lua/lualine/themes/gruvbox.lua index 05dd333..c284b0f 100644 --- a/lua/lualine/themes/gruvbox.lua +++ b/lua/lualine/themes/gruvbox.lua @@ -1,48 +1,7 @@ --- Copyright (c) 2020-2021 hoob3rt +-- Copyright (c) 2020-2021 shadmansaleh -- MIT license, see LICENSE for more details. --- LuaFormatter off -local colors = { - black = '#282828', - white = '#ebdbb2', - red = '#fb4934', - green = '#b8bb26', - blue = '#83a598', - yellow = '#fe8019', - gray = '#a89984', - darkgray = '#3c3836', - lightgray = '#504945', - inactivegray = '#7c6f64', -} --- LuaFormatter on -return { - normal = { - a = {bg = colors.gray, fg = colors.black, gui = 'bold'}, - b = {bg = colors.lightgray, fg = colors.white}, - c = {bg = colors.darkgray, fg = colors.gray} - }, - insert = { - a = {bg = colors.blue, fg = colors.black, gui = 'bold'}, - b = {bg = colors.lightgray, fg = colors.white}, - c = {bg = colors.lightgray, fg = colors.white} - }, - visual = { - a = {bg = colors.yellow, fg = colors.black, gui = 'bold'}, - b = {bg = colors.lightgray, fg = colors.white}, - c = {bg = colors.inactivegray, fg = colors.black} - }, - replace = { - a = {bg = colors.red, fg = colors.black, gui = 'bold'}, - b = {bg = colors.lightgray, fg = colors.white}, - c = {bg = colors.black, fg = colors.white} - }, - command = { - a = {bg = colors.green, fg = colors.black, gui = 'bold'}, - b = {bg = colors.lightgray, fg = colors.white}, - c = {bg = colors.inactivegray, fg = colors.black} - }, - inactive = { - a = {bg = colors.darkgray, fg = colors.gray, gui = 'bold'}, - b = {bg = colors.darkgray, fg = colors.gray}, - c = {bg = colors.darkgray, fg = colors.gray} - } -} +-- Credit: itchyny(lightline) +-- License: MIT License +local background = vim.opt.background:get() + +return require('lualine.themes.gruvbox_' .. background) diff --git a/lua/lualine/themes/gruvbox_dark.lua b/lua/lualine/themes/gruvbox_dark.lua new file mode 100644 index 0000000..05dd333 --- /dev/null +++ b/lua/lualine/themes/gruvbox_dark.lua @@ -0,0 +1,48 @@ +-- Copyright (c) 2020-2021 hoob3rt +-- MIT license, see LICENSE for more details. +-- LuaFormatter off +local colors = { + black = '#282828', + white = '#ebdbb2', + red = '#fb4934', + green = '#b8bb26', + blue = '#83a598', + yellow = '#fe8019', + gray = '#a89984', + darkgray = '#3c3836', + lightgray = '#504945', + inactivegray = '#7c6f64', +} +-- LuaFormatter on +return { + normal = { + a = {bg = colors.gray, fg = colors.black, gui = 'bold'}, + b = {bg = colors.lightgray, fg = colors.white}, + c = {bg = colors.darkgray, fg = colors.gray} + }, + insert = { + a = {bg = colors.blue, fg = colors.black, gui = 'bold'}, + b = {bg = colors.lightgray, fg = colors.white}, + c = {bg = colors.lightgray, fg = colors.white} + }, + visual = { + a = {bg = colors.yellow, fg = colors.black, gui = 'bold'}, + b = {bg = colors.lightgray, fg = colors.white}, + c = {bg = colors.inactivegray, fg = colors.black} + }, + replace = { + a = {bg = colors.red, fg = colors.black, gui = 'bold'}, + b = {bg = colors.lightgray, fg = colors.white}, + c = {bg = colors.black, fg = colors.white} + }, + command = { + a = {bg = colors.green, fg = colors.black, gui = 'bold'}, + b = {bg = colors.lightgray, fg = colors.white}, + c = {bg = colors.inactivegray, fg = colors.black} + }, + inactive = { + a = {bg = colors.darkgray, fg = colors.gray, gui = 'bold'}, + b = {bg = colors.darkgray, fg = colors.gray}, + c = {bg = colors.darkgray, fg = colors.gray} + } +} diff --git a/lua/lualine/themes/iceberg.lua b/lua/lualine/themes/iceberg.lua new file mode 100644 index 0000000..ca24133 --- /dev/null +++ b/lua/lualine/themes/iceberg.lua @@ -0,0 +1,7 @@ +-- Copyright (c) 2020-2021 shadmansaleh +-- MIT license, see LICENSE for more details. +-- Credit: itchyny(lightline) +-- License: MIT License +local background = vim.opt.background:get() + +return require('lualine.themes.iceberg_' .. background) diff --git a/lua/lualine/themes/modus_vivendi.lua b/lua/lualine/themes/modus-vivendi.lua similarity index 100% rename from lua/lualine/themes/modus_vivendi.lua rename to lua/lualine/themes/modus-vivendi.lua diff --git a/lua/lualine/utils/loader.lua b/lua/lualine/utils/loader.lua index dbe2761..de90993 100644 --- a/lua/lualine/utils/loader.lua +++ b/lua/lualine/utils/loader.lua @@ -75,4 +75,24 @@ local function load_all(config) load_extensions(config) end -return {load_all = load_all} +local function load(patern) + local files = vim.fn.uniq(vim.api.nvim_get_runtime_file(patern, true)) + local n_files = #files + if n_files == 0 then return nil + elseif n_files == 1 then return dofile(files[1]) + else + for _, file in ipairs(files) do + if not file:find('lualine.nvim') then return dofile(file) end + end + end +end + +local function load_theme(theme_name) + return load(table.concat( + {'lua', 'lualine', 'themes', theme_name..'.lua'}, package.config:sub(1, 1))) +end + +return { + load_all = load_all, + load_theme = load_theme +} diff --git a/lua/tests/spec/config_spec.lua b/lua/tests/spec/config_spec.lua index 4050ce3..472b859 100644 --- a/lua/tests/spec/config_spec.lua +++ b/lua/tests/spec/config_spec.lua @@ -19,7 +19,7 @@ describe('config parsing', function() describe('theme', function() it('default', function() local config = config_module.apply_configuration({}) - eq(config.options.theme, 'gruvbox') + eq(config.options.theme, 'auto') end) it('custom', function() local config = {options = {theme = 'nord'}}