diff options
author | Christian Duerr <contact@christianduerr.com> | 2022-08-31 22:48:38 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-01 01:48:38 +0300 |
commit | 4ddb608563d985060d69594d1004550a680ae3bd (patch) | |
tree | 0b02a330b3e59300cff80a147f3c1bdab7f9ea57 /alacritty_config | |
parent | 18f9c2793924aec91c80a69ccb45f529adaffae5 (diff) | |
download | alacritty-4ddb608563d985060d69594d1004550a680ae3bd.tar.gz alacritty-4ddb608563d985060d69594d1004550a680ae3bd.zip |
Add IPC config subcommand
This patch adds a new mechanism for changing configuration options
without editing the configuration file, by sending options to running
instances through `alacritty msg`.
Each window will load Alacritty's configuration file by default and then
accept IPC messages for config updates using the `alacritty msg config`
subcommand. By default all windows will be updated, individual windows
can be addressed using `alacritty msg config --window-id
"$ALACRITTY_WINDOW_ID"`.
Each option will replace the config's current value and cannot be reset
until Alacritty is restarted or the option is overwritten with a new
value.
Configuration options are passed in the format `field.subfield=value`,
where `value` is interpreted as yaml.
Closes #472.
Diffstat (limited to 'alacritty_config')
-rw-r--r-- | alacritty_config/Cargo.toml | 14 | ||||
-rw-r--r-- | alacritty_config/src/lib.rs | 64 |
2 files changed, 78 insertions, 0 deletions
diff --git a/alacritty_config/Cargo.toml b/alacritty_config/Cargo.toml new file mode 100644 index 00000000..3b287f43 --- /dev/null +++ b/alacritty_config/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "alacritty_config" +version = "0.1.0" +authors = ["Christian Duerr <contact@christianduerr.com>"] +license = "MIT/Apache-2.0" +description = "Alacritty configuration abstractions" +homepage = "https://github.com/alacritty/alacritty" +edition = "2021" +rust-version = "1.57.0" + +[dependencies] +log = { version = "0.4.17", features = ["serde"] } +serde_yaml = "0.8.24" +serde = "1.0.137" diff --git a/alacritty_config/src/lib.rs b/alacritty_config/src/lib.rs new file mode 100644 index 00000000..7a467650 --- /dev/null +++ b/alacritty_config/src/lib.rs @@ -0,0 +1,64 @@ +use std::collections::HashMap; +use std::error::Error; + +use log::LevelFilter; +use serde::Deserialize; +use serde_yaml::Value; + +pub trait SerdeReplace { + fn replace(&mut self, key: &str, value: Value) -> Result<(), Box<dyn Error>>; +} + +macro_rules! impl_replace { + ($($ty:ty),*$(,)*) => { + $( + impl SerdeReplace for $ty { + fn replace(&mut self, key: &str, value: Value) -> Result<(), Box<dyn Error>> { + replace_simple(self, key, value) + } + } + )* + }; +} + +#[rustfmt::skip] +impl_replace!( + usize, u8, u16, u32, u64, u128, + isize, i8, i16, i32, i64, i128, + f32, f64, + bool, + char, + String, + LevelFilter, +); + +fn replace_simple<'de, D>(data: &mut D, key: &str, value: Value) -> Result<(), Box<dyn Error>> +where + D: Deserialize<'de>, +{ + if !key.is_empty() { + let error = format!("Fields \"{}\" do not exist", key); + return Err(error.into()); + } + *data = D::deserialize(value)?; + + Ok(()) +} + +impl<'de, T: Deserialize<'de>> SerdeReplace for Vec<T> { + fn replace(&mut self, key: &str, value: Value) -> Result<(), Box<dyn Error>> { + replace_simple(self, key, value) + } +} + +impl<'de, T: Deserialize<'de>> SerdeReplace for Option<T> { + fn replace(&mut self, key: &str, value: Value) -> Result<(), Box<dyn Error>> { + replace_simple(self, key, value) + } +} + +impl<'de, T: Deserialize<'de>> SerdeReplace for HashMap<String, T> { + fn replace(&mut self, key: &str, value: Value) -> Result<(), Box<dyn Error>> { + replace_simple(self, key, value) + } +} |