diff --git a/config/src/lua.rs b/config/src/lua.rs index 5479c1b8a..c4f3e1d0f 100644 --- a/config/src/lua.rs +++ b/config/src/lua.rs @@ -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", diff --git a/docs/config/lua/wezterm.color/get_builtin_schemes.md b/docs/config/lua/wezterm.color/get_builtin_schemes.md new file mode 100644 index 000000000..321439c65 --- /dev/null +++ b/docs/config/lua/wezterm.color/get_builtin_schemes.md @@ -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 { +} +``` diff --git a/docs/config/lua/wezterm.color/get_default_colors.md b/docs/config/lua/wezterm.color/get_default_colors.md index 4beac5d16..a54a0c72e 100644 --- a/docs/config/lua/wezterm.color/get_default_colors.md +++ b/docs/config/lua/wezterm.color/get_default_colors.md @@ -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() diff --git a/docs/config/lua/wezterm/get_builtin_color_schemes.md b/docs/config/lua/wezterm/get_builtin_color_schemes.md index cbbc26060..19f9ea854 100644 --- a/docs/config/lua/wezterm/get_builtin_color_schemes.md +++ b/docs/config/lua/wezterm/get_builtin_color_schemes.md @@ -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. diff --git a/lua-api-crates/color-funcs/src/lib.rs b/lua-api-crates/color-funcs/src/lib.rs index ccaddef8c..709085bde 100644 --- a/lua-api-crates/color-funcs/src/lib.rs +++ b/lua-api-crates/color-funcs/src/lib.rs @@ -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(()) }