colors: move wezterm.get_builtin_color_schemes to wezterm.colors.get_builtin_schemes

This commit is contained in:
Wez Furlong committed 2022-07-14 21:52:49 -07:00
1 parent 548adf484b
commit e566885e7f
5 files changed
+126 -6

No files matched your search

-5
View File
@@ -180,11 +180,6 @@ end
lua.create_function(|_, ()| Ok(crate::WslDomain::default_domains()))?,
)?;
wezterm_mod.set(
"get_builtin_color_schemes",
lua.create_function(|_, ()| Ok(crate::COLOR_SCHEMES.clone()))?,
)?;
wezterm_mod.set("font", lua.create_function(font)?)?;
wezterm_mod.set(
"font_with_fallback",
@@ -0,0 +1,108 @@
# `wezterm.color.get_builtin_schemes()`
*Since: nightly builds only*
(In earlier releases, you can use [wezterm.get_builtin_color_schemes()](../wezterm/get_builtin_color_schemes.md))
Returns a lua table keyed by color scheme name and whose values are
the color scheme definition of the builtin color schemes.
This is useful for programmatically deciding things about the scheme
to use based on its color, or for taking a scheme and overriding a
couple of entries just from your `wezterm.lua` configuration file.
This example shows how to make wezterm pick a random color scheme for
each newly created window:
```lua
local wezterm = require 'wezterm'
-- The set of schemes that we like and want to put in our rotation
local schemes = {}
for name, scheme in pairs(wezterm.color.get_builtin_schemes()) do
table.insert(schemes, name)
end
wezterm.on("window-config-reloaded", function(window, pane)
-- If there are no overrides, this is our first time seeing
-- this window, so we can pick a random scheme.
if not window:get_config_overrides() then
-- Pick a random scheme name
local scheme = schemes[math.random(#schemes)]
window:set_config_overrides({
color_scheme = scheme
})
end
end)
return {
}
```
This example shows how to take an existing scheme, modify a color, and
then use that new scheme to override the default:
```lua
local wezterm = require 'wezterm'
local scheme = wezterm.color.get_builtin_schemes()["Gruvbox Light"]
scheme.background = "red"
return {
color_schemes = {
-- Override the builtin Gruvbox Light scheme with our modification.
["Gruvbox Light"] = scheme,
-- We can also give it a different name if we don't want to override
-- the default
["Gruvbox Red"] = scheme,
},
color_scheme = "Gruvbox Light",
}
```
This example shows how to analyze the colors in the builtin schemes and
use that to select just the dark schemes and then randomly pick one
of those for each new window:
```lua
local wezterm = require 'wezterm'
local function dark_schemes()
local schemes = wezterm.color.get_builtin_schemes()
local dark = {}
for name, scheme in pairs(schemes) do
-- parse into a color object
local bg = wezterm.color.parse(scheme.background)
-- and extract HSLA information
local h, s, l, a = bg:hsla()
-- `l` is the "lightness" of the color where 0 is darkest
-- and 1 is lightest.
if l < 0.4 then
table.insert(dark, name)
end
end
table.sort(dark)
return dark
end
local dark = dark_schemes()
wezterm.on("window-config-reloaded", function(window, pane)
-- If there are no overrides, this is our first time seeing
-- this window, so we can pick a random scheme.
if not window:get_config_overrides() then
-- Pick a random scheme name
local scheme = dark[math.random(#dark)]
window:set_config_overrides({
color_scheme = scheme
})
end
end)
return {
}
```
@@ -14,7 +14,7 @@ other is one of the many built-in schemes:
```lua
local wezterm = require 'wezterm'
local gruvbox = wezterm.get_builtin_color_schemes()["Gruvbox Light"]
local gruvbox = wezterm.color.get_builtin_schemes()["Gruvbox Light"]
gruvbox.background = "red"
local my_default = wezterm.color.get_default_colors()
@@ -104,3 +104,10 @@ end)
return {
}
```
*Since: nightly builds only*
This function moved to
[wezterm.color.get_builtin_schemes()](../wezterm.color/get_builtin_schemes.md)
but can still be called as `wezterm.get_builtin_color_schemes()`. See that page
for more examples.
+10
View File
@@ -126,6 +126,16 @@ pub fn register(lua: &Lua) -> anyhow::Result<()> {
let wezterm_mod = get_or_create_module(lua, "wezterm")?;
wezterm_mod.set("gradient_colors", lua.create_function(gradient_colors)?)?;
color.set("gradient", lua.create_function(gradient_colors)?)?;
wezterm_mod.set(
"get_builtin_color_schemes",
lua.create_function(|_, ()| Ok(config::COLOR_SCHEMES.clone()))?,
)?;
color.set(
"get_builtin_schemes",
lua.create_function(|_, ()| Ok(config::COLOR_SCHEMES.clone()))?,
)?;
Ok(())
}