feat: improved dealing with vim.o.background

This commit is contained in:
Folke Lemaitre 2022-09-10 09:35:41 +02:00
parent 54cf70dbd1
commit 6244df26b5
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
8 changed files with 47 additions and 30 deletions

View File

@ -1 +1 @@
require("tokyonight").load({ style = "day" }) require("tokyonight")._load("day")

View File

@ -1 +1 @@
require("tokyonight").load({ style = "night" }) require("tokyonight")._load("night")

View File

@ -1 +1 @@
require("tokyonight").load({ style = "storm" }) require("tokyonight")._load("storm")

View File

@ -1 +1 @@
require("tokyonight").load() require("tokyonight")._load()

View File

@ -5,7 +5,7 @@ local M = {}
---@return ColorScheme ---@return ColorScheme
function M.setup(opts) function M.setup(opts)
opts = opts or {} opts = opts or {}
local config = require("tokyonight.config").options local config = require("tokyonight.config")
-- Color Palette -- Color Palette
---@class ColorScheme ---@class ColorScheme
@ -42,12 +42,12 @@ function M.setup(opts)
red1 = "#db4b4b", red1 = "#db4b4b",
git = { change = "#6183bb", add = "#449dab", delete = "#914c54" }, git = { change = "#6183bb", add = "#449dab", delete = "#914c54" },
} }
if config.style == "night" or config.style == "day" or vim.o.background == "light" then if config.options.style == "night" or config.is_day() then
colors.bg = "#1a1b26" colors.bg = "#1a1b26"
colors.bg_dark = "#16161e" colors.bg_dark = "#16161e"
end end
util.bg = colors.bg util.bg = colors.bg
util.day_brightness = config.day_brightness util.day_brightness = config.options.day_brightness
colors.diff = { colors.diff = {
add = util.darken(colors.green2, 0.15), add = util.darken(colors.green2, 0.15),
@ -72,12 +72,12 @@ function M.setup(opts)
colors.bg_statusline = colors.bg_dark colors.bg_statusline = colors.bg_dark
-- Sidebar and Floats are configurable -- Sidebar and Floats are configurable
colors.bg_sidebar = config.styles.sidebars == "transparent" and colors.none colors.bg_sidebar = config.options.styles.sidebars == "transparent" and colors.none
or config.styles.sidebars == "dark" and colors.bg_dark or config.options.styles.sidebars == "dark" and colors.bg_dark
or colors.bg or colors.bg
colors.bg_float = config.styles.floats == "transparent" and colors.none colors.bg_float = config.options.styles.floats == "transparent" and colors.none
or config.styles.floats == "dark" and colors.bg_dark or config.options.styles.floats == "dark" and colors.bg_dark
or colors.bg or colors.bg
colors.bg_visual = util.darken(colors.blue0, 0.7) colors.bg_visual = util.darken(colors.blue0, 0.7)
@ -89,8 +89,8 @@ function M.setup(opts)
colors.info = colors.blue2 colors.info = colors.blue2
colors.hint = colors.teal colors.hint = colors.teal
config.on_colors(colors) config.options.on_colors(colors)
if opts.transform and (config.style == "day" or vim.o.background == "light") then if opts.transform and config.is_day() then
util.invert_colors(colors) util.invert_colors(colors)
end end

View File

@ -32,6 +32,7 @@ local defaults = {
---@param highlights Highlights ---@param highlights Highlights
---@param colors ColorScheme ---@param colors ColorScheme
on_highlights = function(highlights, colors) end, on_highlights = function(highlights, colors) end,
use_background = true, -- can be light/dark/auto. When auto, background will be set to vim.o.background
} }
---@type Config ---@type Config
@ -47,6 +48,10 @@ function M.extend(options)
M.options = vim.tbl_deep_extend("force", {}, M.options or defaults, options or {}) M.options = vim.tbl_deep_extend("force", {}, M.options or defaults, options or {})
end end
function M.is_day()
return M.options.style == "day" or M.options.use_background and vim.o.background == "light"
end
M.setup() M.setup()
return M return M

View File

@ -4,6 +4,17 @@ local config = require("tokyonight.config")
local M = {} local M = {}
function M._load(style)
if style and not M._style then
M._style = require("tokyonight.config").options.style
end
if not style and M._style then
require("tokyonight.config").options.style = M._style
M._style = nil
end
M.load({ style = style, use_background = style == nil })
end
---@param opts Config|nil ---@param opts Config|nil
function M.load(opts) function M.load(opts)
if opts then if opts then

View File

@ -13,11 +13,12 @@ local M = {}
---@return Theme ---@return Theme
function M.setup() function M.setup()
local config = require("tokyonight.config").options local config = require("tokyonight.config")
local options = config.options
---@class Theme ---@class Theme
---@field highlights Highlights ---@field highlights Highlights
local theme = { local theme = {
config = config, config = options,
colors = colors.setup(), colors = colors.setup(),
} }
@ -26,7 +27,7 @@ function M.setup()
theme.highlights = { theme.highlights = {
Foo = { bg = c.magenta2, fg = c.magenta2 }, Foo = { bg = c.magenta2, fg = c.magenta2 },
Comment = { fg = c.comment, style = config.styles.comments }, -- any comment Comment = { fg = c.comment, style = options.styles.comments }, -- any comment
ColorColumn = { bg = c.black }, -- used for the columns set with 'colorcolumn' ColorColumn = { bg = c.black }, -- used for the columns set with 'colorcolumn'
Conceal = { fg = c.dark5 }, -- placeholder characters substituted for concealed text (see 'conceallevel') Conceal = { fg = c.dark5 }, -- placeholder characters substituted for concealed text (see 'conceallevel')
Cursor = { fg = c.bg, bg = c.fg }, -- character under the cursor Cursor = { fg = c.bg, bg = c.fg }, -- character under the cursor
@ -47,7 +48,7 @@ function M.setup()
WinSeparator = { fg = c.border, style = "bold" }, -- the column separating vertically split windows WinSeparator = { fg = c.border, style = "bold" }, -- the column separating vertically split windows
Folded = { fg = c.blue, bg = c.fg_gutter }, -- line used for closed folds Folded = { fg = c.blue, bg = c.fg_gutter }, -- line used for closed folds
FoldColumn = { bg = c.bg, fg = c.comment }, -- 'foldcolumn' FoldColumn = { bg = c.bg, fg = c.comment }, -- 'foldcolumn'
SignColumn = { bg = config.transparent and c.none or c.bg, fg = c.fg_gutter }, -- column where |signs| are displayed SignColumn = { bg = options.transparent and c.none or c.bg, fg = c.fg_gutter }, -- column where |signs| are displayed
SignColumnSB = { bg = c.bg_sidebar, fg = c.fg_gutter }, -- column where |signs| are displayed SignColumnSB = { bg = c.bg_sidebar, fg = c.fg_gutter }, -- column where |signs| are displayed
Substitute = { bg = c.red, fg = c.black }, -- |:substitute| replacement text highlighting Substitute = { bg = c.red, fg = c.black }, -- |:substitute| replacement text highlighting
LineNr = { fg = c.fg_gutter }, -- Line number for ":number" and ":#" commands, and when 'number' or 'relativenumber' option is set. LineNr = { fg = c.fg_gutter }, -- Line number for ":number" and ":#" commands, and when 'number' or 'relativenumber' option is set.
@ -58,8 +59,8 @@ function M.setup()
-- MsgSeparator= { }, -- Separator for scrolled messages, `msgsep` flag of 'display' -- MsgSeparator= { }, -- Separator for scrolled messages, `msgsep` flag of 'display'
MoreMsg = { fg = c.blue }, -- |more-prompt| MoreMsg = { fg = c.blue }, -- |more-prompt|
NonText = { fg = c.dark3 }, -- '@' at the end of the window, characters from 'showbreak' and other characters that do not really exist in the text (e.g., ">" displayed when a double-wide character doesn't fit at the end of the line). See also |hl-EndOfBuffer|. NonText = { fg = c.dark3 }, -- '@' at the end of the window, characters from 'showbreak' and other characters that do not really exist in the text (e.g., ">" displayed when a double-wide character doesn't fit at the end of the line). See also |hl-EndOfBuffer|.
Normal = { fg = c.fg, bg = config.transparent and c.none or c.bg }, -- normal text Normal = { fg = c.fg, bg = options.transparent and c.none or c.bg }, -- normal text
NormalNC = { fg = c.fg, bg = config.transparent and c.none or config.dim_inactive and c.bg_dark or c.bg }, -- normal text in non-current windows NormalNC = { fg = c.fg, bg = options.transparent and c.none or options.dim_inactive and c.bg_dark or c.bg }, -- normal text in non-current windows
NormalSB = { fg = c.fg_sidebar, bg = c.bg_sidebar }, -- normal text in sidebar NormalSB = { fg = c.fg_sidebar, bg = c.bg_sidebar }, -- normal text in sidebar
NormalFloat = { fg = c.fg, bg = c.bg_float }, -- Normal text in floating windows. NormalFloat = { fg = c.fg, bg = c.bg_float }, -- Normal text in floating windows.
FloatBorder = { fg = c.border_highlight, bg = c.bg_float }, FloatBorder = { fg = c.border_highlight, bg = c.bg_float },
@ -101,15 +102,15 @@ function M.setup()
-- Boolean = { }, -- a boolean constant: TRUE, false -- Boolean = { }, -- a boolean constant: TRUE, false
-- Float = { }, -- a floating point constant: 2.3e10 -- Float = { }, -- a floating point constant: 2.3e10
Identifier = { fg = c.magenta, style = config.styles.variables }, -- (preferred) any variable name Identifier = { fg = c.magenta, style = options.styles.variables }, -- (preferred) any variable name
Function = { fg = c.blue, style = config.styles.functions }, -- function name (also: methods for classes) Function = { fg = c.blue, style = options.styles.functions }, -- function name (also: methods for classes)
Statement = { fg = c.magenta }, -- (preferred) any statement Statement = { fg = c.magenta }, -- (preferred) any statement
-- Conditional = { }, -- if, then, else, endif, switch, etc. -- Conditional = { }, -- if, then, else, endif, switch, etc.
-- Repeat = { }, -- for, do, while, etc. -- Repeat = { }, -- for, do, while, etc.
-- Label = { }, -- case, default, etc. -- Label = { }, -- case, default, etc.
Operator = { fg = c.blue5 }, -- "sizeof", "+", "*", etc. Operator = { fg = c.blue5 }, -- "sizeof", "+", "*", etc.
Keyword = { fg = c.cyan, style = config.styles.keywords }, -- any other keyword Keyword = { fg = c.cyan, style = options.styles.keywords }, -- any other keyword
-- Exception = { }, -- try, catch, throw -- Exception = { }, -- try, catch, throw
PreProc = { fg = c.cyan }, -- (preferred) generic Preprocessor PreProc = { fg = c.cyan }, -- (preferred) generic Preprocessor
@ -218,8 +219,8 @@ function M.setup()
-- TSFuncBuiltin = { }; -- For builtin functions: `table.insert` in Lua. -- TSFuncBuiltin = { }; -- For builtin functions: `table.insert` in Lua.
-- TSFuncMacro = { }; -- For macro defined fuctions (calls and definitions): each `macro_rules` in Rust. -- TSFuncMacro = { }; -- For macro defined fuctions (calls and definitions): each `macro_rules` in Rust.
-- TSInclude = { }; -- For includes: `#include` in C, `use` or `extern crate` in Rust, or `require` in Lua. -- TSInclude = { }; -- For includes: `#include` in C, `use` or `extern crate` in Rust, or `require` in Lua.
TSKeyword = { fg = c.purple, style = config.styles.keywords }, -- For keywords that don't fall in previous categories. TSKeyword = { fg = c.purple, style = options.styles.keywords }, -- For keywords that don't fall in previous categories.
TSKeywordFunction = { fg = c.magenta, style = config.styles.functions }, -- For keywords used to define a fuction. TSKeywordFunction = { fg = c.magenta, style = options.styles.functions }, -- For keywords used to define a fuction.
TSLabel = { fg = c.blue }, -- For labels: `label:` in C and `:label:` in Lua. TSLabel = { fg = c.blue }, -- For labels: `label:` in C and `:label:` in Lua.
-- TSMethod = { }; -- For method calls and definitions. -- TSMethod = { }; -- For method calls and definitions.
-- TSNamespace = { }; -- For identifiers referring to modules and namespaces. -- TSNamespace = { }; -- For identifiers referring to modules and namespaces.
@ -239,7 +240,7 @@ function M.setup()
-- TSSymbol = { }; -- For identifiers referring to symbols or atoms. -- TSSymbol = { }; -- For identifiers referring to symbols or atoms.
-- TSType = { }; -- For types. -- TSType = { }; -- For types.
-- TSTypeBuiltin = { }; -- For builtin types. -- TSTypeBuiltin = { }; -- For builtin types.
TSVariable = { style = config.styles.variables }, -- Any variable name that does not have another highlight. TSVariable = { style = options.styles.variables }, -- Any variable name that does not have another highlight.
TSVariableBuiltin = { fg = c.red }, -- Variable names that are defined by the languages, like `this` or `self`. TSVariableBuiltin = { fg = c.red }, -- Variable names that are defined by the languages, like `this` or `self`.
-- TSTag = { }; -- Tags like html tag names. -- TSTag = { }; -- Tags like html tag names.
@ -509,8 +510,8 @@ function M.setup()
MiniStarterCurrent = { style = "nocombine" }, MiniStarterCurrent = { style = "nocombine" },
MiniStarterFooter = { fg = c.yellow, style = "italic" }, MiniStarterFooter = { fg = c.yellow, style = "italic" },
MiniStarterHeader = { fg = c.blue }, MiniStarterHeader = { fg = c.blue },
MiniStarterInactive = { fg = c.comment, style = config.styles.comments }, MiniStarterInactive = { fg = c.comment, style = options.styles.comments },
MiniStarterItem = { fg = c.fg, bg = config.transparent and c.none or c.bg }, MiniStarterItem = { fg = c.fg, bg = options.transparent and c.none or c.bg },
MiniStarterItemBullet = { fg = c.border_highlight }, MiniStarterItemBullet = { fg = c.border_highlight },
MiniStarterItemPrefix = { fg = c.warning }, MiniStarterItemPrefix = { fg = c.warning },
MiniStarterSection = { fg = c.blue1 }, MiniStarterSection = { fg = c.blue1 },
@ -564,7 +565,7 @@ function M.setup()
theme.defer = {} theme.defer = {}
if config.hide_inactive_statusline then if options.hide_inactive_statusline then
local inactive = { style = "underline", bg = c.none, fg = c.bg, sp = c.border } local inactive = { style = "underline", bg = c.none, fg = c.bg, sp = c.border }
-- StatusLineNC -- StatusLineNC
@ -579,9 +580,9 @@ function M.setup()
theme.highlights.MiniStatuslineInactive = inactive theme.highlights.MiniStatuslineInactive = inactive
end end
config.on_highlights(theme.highlights, theme.colors) options.on_highlights(theme.highlights, theme.colors)
if config.style == "day" or vim.o.background == "light" then if config.is_day() then
util.invert_colors(theme.colors) util.invert_colors(theme.colors)
util.invert_highlights(theme.highlights) util.invert_highlights(theme.highlights)
end end