1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-23 15:04:36 +03:00

add lua functions for importing color scheme files

This commit is contained in:
Wez Furlong 2022-07-14 23:03:18 -07:00
parent e566885e7f
commit 181401a2ca
6 changed files with 235 additions and 2 deletions

View File

@ -439,6 +439,7 @@ pub struct ColorSchemeMetaData {
pub origin_url: Option<String>,
pub wezterm_version: Option<String>,
}
impl_lua_conversion_dynamic!(ColorSchemeMetaData);
#[derive(Debug, Clone, PartialEq, FromDynamic, ToDynamic)]
pub struct ColorSchemeFile {
@ -448,6 +449,7 @@ pub struct ColorSchemeFile {
#[dynamic(default)]
pub metadata: ColorSchemeMetaData,
}
impl_lua_conversion_dynamic!(ColorSchemeFile);
impl ColorSchemeFile {
pub fn from_toml_value(value: &toml::Value) -> anyhow::Result<Self> {

View File

@ -18,7 +18,7 @@ As features stabilize some brief notes about them will accumulate here.
* [window:set_position](config/lua/window/set_position.md) method for controlling window position.
* [window:maximize](config/lua/window/maximize.md) and [window:restore](config/lua/window/restore.md) methods for controlling window maximization state.
* [window:get_selection_escapes_for_pane](config/lua/window/get_selection_escapes_for_pane.md) method for getting the current selection including escape sequences. [#2223](https://github.com/wez/wezterm/issues/2223)
* New [wezterm.color](config/lua/wezterm.color/index.md) module for working with colors.
* New [wezterm.color](config/lua/wezterm.color/index.md) module for working with colors and importing color schemes.
* New [wezterm.gui](config/lua/wezterm.gui/index.md) module and [mux_window:gui_window](config/lua/mux-window/gui_window.md) method.
* New [wezterm.gui.screens()](config/lua/wezterm.gui/screens.md) function for getting information about the available screens/monitors/displays
* New [wezterm.gui.get_appearance()](config/lua/wezterm.gui/get_appearance.md) function for a simpler way to get system dark mode state

View File

@ -0,0 +1,79 @@
# `wezterm.color.load_base16_scheme(file_name)`
*Since: nightly builds only*
Loads a yaml file in [base16](https://github.com/chriskempson/base16) format
and returns it as a wezterm color scheme.
Note that wezterm ships with the base16 color schemes that were referenced via
[base16-schemes-source](https://github.com/chriskempson/base16-schemes-source)
when the release was prepared so this function is primarily useful if you want
to import a base16 color scheme that either isn't listed from the main list, or
that was created after your version of wezterm was built.
This function returns a tuple of the the color definitions and the metadata.
For example, given a yaml file with these contents:
```yaml
scheme: "Cupcake"
author: "Chris Kempson (http://chriskempson.com)"
base00: "fbf1f2"
base01: "f2f1f4"
base02: "d8d5dd"
base03: "bfb9c6"
base04: "a59daf"
base05: "8b8198"
base06: "72677E"
base07: "585062"
base08: "D57E85"
base09: "EBB790"
base0A: "DCB16C"
base0B: "A3B367"
base0C: "69A9A7"
base0D: "7297B9"
base0E: "BB99B4"
base0F: "BAA58C"
```
Then:
```lua
> colors, metadata = wezterm.color.load_base16_scheme("/tmp/cupcake.yaml")
> print(colors)
22:59:26.998 INFO logging > lua: {
"ansi": [
"#fbf1f2",
"#d57e85",
"#a3b367",
"#dcb16c",
"#7297b9",
"#bb99b4",
"#69a9a7",
"#8b8198",
],
"background": "#fbf1f2",
"brights": [
"#bfb9c6",
"#d57e85",
"#a3b367",
"#dcb16c",
"#7297b9",
"#bb99b4",
"#69a9a7",
"#585062",
],
"cursor_bg": "#8b8198",
"cursor_border": "#8b8198",
"cursor_fg": "#8b8198",
"foreground": "#8b8198",
"indexed": {},
"selection_bg": "#8b8198",
"selection_fg": "#fbf1f2",
}
> print(metadata)
22:59:29.671 INFO logging > lua: {
"author": "Chris Kempson (http://chriskempson.com)",
"name": "Cupcake",
}
```

View File

@ -0,0 +1,46 @@
# `wezterm.color.load_scheme(file_name)`
*Since: nightly builds only*
Loads a wezterm color scheme from a TOML file. This function
returns a tuple of the the color definitions and the metadata:
```lua
> colors, metadata = wezterm.color.load_scheme("wezterm/assets/colors/Abernathy.toml")
> print(metadata)
22:37:06.041 INFO logging > lua: {
"name": "Abernathy",
"origin_url": "https://github.com/mbadolato/iTerm2-Color-Schemes",
}
> print(colors)
22:37:10.416 INFO logging > lua: {
"ansi": [
"#000000",
"#cd0000",
"#00cd00",
"#cdcd00",
"#1093f5",
"#cd00cd",
"#00cdcd",
"#faebd7",
],
"background": "#111416",
"brights": [
"#404040",
"#ff0000",
"#00ff00",
"#ffff00",
"#11b5f6",
"#ff00ff",
"#00ffff",
"#ffffff",
],
"cursor_bg": "#bbbbbb",
"cursor_border": "#bbbbbb",
"cursor_fg": "#ffffff",
"foreground": "#eeeeec",
"indexed": {},
"selection_bg": "#eeeeec",
"selection_fg": "#333333",
}
```

View File

@ -0,0 +1,77 @@
# `wezterm.color.load_terminal_sexy_scheme(file_name)`
*Since: nightly builds only*
Loads a json file exported from [terminal.sexy](https://terminal.sexy/)
and returns it as a wezterm color scheme.
Note that wezterm ships with all of the pre-defined terminal.sexy color
schemes, so this function is primarily useful if you want to design a color
scheme using terminal.sexy and then import it to wezterm.
This function returns a tuple of the the color definitions and the metadata.
For example, given a json file with these contents:
```json
{
"name": "",
"author": "",
"color": [
"#282a2e",
"#a54242",
"#8c9440",
"#de935f",
"#5f819d",
"#85678f",
"#5e8d87",
"#707880",
"#373b41",
"#cc6666",
"#b5bd68",
"#f0c674",
"#81a2be",
"#b294bb",
"#8abeb7",
"#c5c8c6"
],
"foreground": "#c5c8c6",
"background": "#1d1f21"
}
```
Then:
```
> colors, metadata = wezterm.color.load_terminal_sexy_scheme("/path/to/file.json")
> print(colors)
22:37:10.416 INFO logging > lua: {
"ansi": [
"#282a2e",
"#a54242",
"#8c9440",
"#de935f",
"#5f819d",
"#85678f",
"#5e8d87",
"#707880",
],
"background": "#1d1f21",
"brights": [
"#373b41",
"#cc6666",
"#b5bd68",
"#f0c674",
"#81a2be",
"#b294bb",
"#8abeb7",
"#c5c8c6"
],
"foreground": "#c5c8c6",
}
> print(metadata)
22:37:06.041 INFO logging > lua: {
"name": "",
"author": ""
}
```

View File

@ -1,6 +1,8 @@
use crate::schemes::base16::Base16Scheme;
use crate::schemes::sexy::Sexy;
use config::lua::mlua::{self, Lua, MetaMethod, UserData, UserDataMethods};
use config::lua::{get_or_create_module, get_or_create_sub_module};
use config::{Gradient, Palette, RgbaColor, SrgbaTuple};
use config::{ColorSchemeFile, Gradient, Palette, RgbaColor, SrgbaTuple};
mod image_colors;
pub mod schemes;
@ -123,6 +125,33 @@ pub fn register(lua: &Lua) -> anyhow::Result<()> {
})?,
)?;
color.set(
"load_scheme",
lua.create_function(|_, file_name: String| {
let data = std::fs::read_to_string(file_name)
.map_err(|err| mlua::Error::external(format!("{err:#}")))?;
let scheme = ColorSchemeFile::from_toml_str(&data)
.map_err(|err| mlua::Error::external(format!("{err:#}")))?;
Ok((scheme.colors, scheme.metadata))
})?,
)?;
color.set(
"load_terminal_sexy_scheme",
lua.create_function(|_, file_name: String| {
let scheme = Sexy::load_file(file_name)
.map_err(|err| mlua::Error::external(format!("{err:#}")))?;
Ok((scheme.colors, scheme.metadata))
})?,
)?;
color.set(
"load_base16_scheme",
lua.create_function(|_, file_name: String| {
let scheme = Base16Scheme::load_file(file_name)
.map_err(|err| mlua::Error::external(format!("{err:#}")))?;
Ok((scheme.colors, scheme.metadata))
})?,
)?;
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)?)?;