1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-27 02:25:28 +03:00

config: expose configuration generation number

This commit is contained in:
Wez Furlong 2019-11-24 09:19:14 -08:00
parent 8996f897b9
commit d06c08e0f3
4 changed files with 48 additions and 17 deletions

View File

@ -39,7 +39,7 @@ pub use unix::*;
lazy_static! { lazy_static! {
static ref HOME_DIR: PathBuf = dirs::home_dir().expect("can't find HOME dir"); static ref HOME_DIR: PathBuf = dirs::home_dir().expect("can't find HOME dir");
static ref RUNTIME_DIR: PathBuf = compute_runtime_dir().unwrap(); static ref RUNTIME_DIR: PathBuf = compute_runtime_dir().unwrap();
static ref CONFIG: ConfigHandle = ConfigHandle::new(); static ref CONFIG: Configuration = Configuration::new();
} }
/// Discard the current configuration and replace it with /// Discard the current configuration and replace it with
@ -49,13 +49,13 @@ pub fn use_default_configuration() {
} }
/// Returns a handle to the current configuration /// Returns a handle to the current configuration
pub fn configuration() -> Arc<Config> { pub fn configuration() -> ConfigHandle {
CONFIG.get() CONFIG.get()
} }
/// If there was an error loading the preferred configuration, /// If there was an error loading the preferred configuration,
/// return it, otherwise return the current configuration /// return it, otherwise return the current configuration
pub fn configuration_result() -> Result<Arc<Config>, Error> { pub fn configuration_result() -> Result<ConfigHandle, Error> {
if let Some(error) = CONFIG.get_error() { if let Some(error) = CONFIG.get_error() {
failure::bail!("{}", error); failure::bail!("{}", error);
} }
@ -65,6 +65,7 @@ pub fn configuration_result() -> Result<Arc<Config>, Error> {
struct ConfigInner { struct ConfigInner {
config: Arc<Config>, config: Arc<Config>,
error: Option<String>, error: Option<String>,
generation: usize,
} }
impl ConfigInner { impl ConfigInner {
@ -76,10 +77,12 @@ impl ConfigInner {
Ok(config) => Self { Ok(config) => Self {
config: Arc::new(config), config: Arc::new(config),
error: None, error: None,
generation: 0,
}, },
Err(err) => Self { Err(err) => Self {
config: Arc::new(Config::default_config()), config: Arc::new(Config::default_config()),
error: Some(err.to_string()), error: Some(err.to_string()),
generation: 0,
}, },
} }
} }
@ -94,6 +97,7 @@ impl ConfigInner {
Ok(config) => { Ok(config) => {
self.config = Arc::new(config); self.config = Arc::new(config);
self.error.take(); self.error.take();
self.generation += 1;
} }
Err(err) => { Err(err) => {
self.error.replace(err.to_string()); self.error.replace(err.to_string());
@ -107,14 +111,15 @@ impl ConfigInner {
fn use_defaults(&mut self) { fn use_defaults(&mut self) {
self.config = Arc::new(Config::default_config()); self.config = Arc::new(Config::default_config());
self.error.take(); self.error.take();
self.generation += 1;
} }
} }
pub struct ConfigHandle { pub struct Configuration {
inner: Mutex<ConfigInner>, inner: Mutex<ConfigInner>,
} }
impl ConfigHandle { impl Configuration {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
inner: Mutex::new(ConfigInner::load()), inner: Mutex::new(ConfigInner::load()),
@ -122,8 +127,12 @@ impl ConfigHandle {
} }
/// Returns the effective configuration. /// Returns the effective configuration.
pub fn get(&self) -> Arc<Config> { pub fn get(&self) -> ConfigHandle {
Arc::clone(&self.inner.lock().unwrap().config) let inner = self.inner.lock().unwrap();
ConfigHandle {
config: Arc::clone(&inner.config),
generation: inner.generation,
}
} }
/// Reset the configuration to defaults /// Reset the configuration to defaults
@ -153,6 +162,29 @@ impl ConfigHandle {
} }
} }
#[derive(Clone, Debug)]
pub struct ConfigHandle {
config: Arc<Config>,
generation: usize,
}
impl ConfigHandle {
/// Returns the generation number for the configuration,
/// allowing consuming code to know whether the config
/// has been reloading since they last derived some
/// information from the configuration
pub fn generation(&self) -> usize {
self.generation
}
}
impl std::ops::Deref for ConfigHandle {
type Target = Config;
fn deref(&self) -> &Config {
&*self.config
}
}
#[derive(Debug, Deserialize, Clone)] #[derive(Debug, Deserialize, Clone)]
pub struct Config { pub struct Config {
/// The font size, measured in points /// The font size, measured in points

View File

@ -8,7 +8,6 @@ use self::hbwrap as harfbuzz;
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
use std::rc::Rc; use std::rc::Rc;
use std::sync::Arc;
pub mod system; pub mod system;
pub use self::system::*; pub use self::system::*;
@ -28,7 +27,7 @@ pub mod fontloader;
#[cfg(any(target_os = "macos", windows))] #[cfg(any(target_os = "macos", windows))]
pub mod fontloader_and_freetype; pub mod fontloader_and_freetype;
use super::config::{configuration, Config, TextStyle}; use super::config::{configuration, ConfigHandle, TextStyle};
use term::CellAttributes; use term::CellAttributes;
type FontPtr = Rc<RefCell<Box<dyn NamedFont>>>; type FontPtr = Rc<RefCell<Box<dyn NamedFont>>>;
@ -194,7 +193,7 @@ impl FontConfiguration {
/// cell attributes. /// cell attributes.
pub fn match_style<'a>( pub fn match_style<'a>(
&self, &self,
config: &'a Arc<Config>, config: &'a ConfigHandle,
attrs: &CellAttributes, attrs: &CellAttributes,
) -> &'a TextStyle { ) -> &'a TextStyle {
// a little macro to avoid boilerplate for matching the rules. // a little macro to avoid boilerplate for matching the rules.

View File

@ -2,7 +2,7 @@ use super::quad::*;
use super::renderstate::*; use super::renderstate::*;
use super::utilsprites::RenderMetrics; use super::utilsprites::RenderMetrics;
use crate::clipboard::SystemClipboard; use crate::clipboard::SystemClipboard;
use crate::config::{configuration, Config}; use crate::config::{configuration, ConfigHandle};
use crate::font::{FontConfiguration, FontSystemSelection}; use crate::font::{FontConfiguration, FontSystemSelection};
use crate::frontend::gui::tabbar::{TabBarItem, TabBarState}; use crate::frontend::gui::tabbar::{TabBarItem, TabBarState};
use crate::frontend::{executor, front_end}; use crate::frontend::{executor, front_end};
@ -356,7 +356,7 @@ impl WindowCallbacks for TermWindow {
impl TermWindow { impl TermWindow {
pub fn new_window( pub fn new_window(
config: &Arc<Config>, config: &ConfigHandle,
fontconfig: &Rc<FontConfiguration>, fontconfig: &Rc<FontConfiguration>,
tab: &Rc<dyn Tab>, tab: &Rc<dyn Tab>,
mux_window_id: MuxWindowId, mux_window_id: MuxWindowId,

View File

@ -342,7 +342,7 @@ impl SshParameters {
} }
} }
fn run_ssh(config: Arc<config::Config>, opts: &SshCommand) -> Fallible<()> { fn run_ssh(config: config::ConfigHandle, opts: &SshCommand) -> Fallible<()> {
let front_end_selection = opts.front_end.unwrap_or(config.front_end); let front_end_selection = opts.front_end.unwrap_or(config.front_end);
let gui = front_end_selection.try_new()?; let gui = front_end_selection.try_new()?;
@ -410,7 +410,7 @@ fn run_ssh(config: Arc<config::Config>, opts: &SshCommand) -> Fallible<()> {
gui.run_forever() gui.run_forever()
} }
fn run_serial(config: Arc<config::Config>, opts: &SerialCommand) -> Fallible<()> { fn run_serial(config: config::ConfigHandle, opts: &SerialCommand) -> Fallible<()> {
let font_system = opts.font_system.unwrap_or(config.font_system); let font_system = opts.font_system.unwrap_or(config.font_system);
font_system.set_default(); font_system.set_default();
@ -437,7 +437,7 @@ fn run_serial(config: Arc<config::Config>, opts: &SerialCommand) -> Fallible<()>
gui.run_forever() gui.run_forever()
} }
fn client_domains(config: &Arc<config::Config>) -> Vec<ClientDomainConfig> { fn client_domains(config: &config::ConfigHandle) -> Vec<ClientDomainConfig> {
let mut domains = vec![]; let mut domains = vec![];
for unix_dom in &config.unix_domains { for unix_dom in &config.unix_domains {
domains.push(ClientDomainConfig::Unix(unix_dom.clone())); domains.push(ClientDomainConfig::Unix(unix_dom.clone()));
@ -453,7 +453,7 @@ fn client_domains(config: &Arc<config::Config>) -> Vec<ClientDomainConfig> {
domains domains
} }
fn run_mux_client(config: Arc<config::Config>, opts: &ConnectCommand) -> Fallible<()> { fn run_mux_client(config: config::ConfigHandle, opts: &ConnectCommand) -> Fallible<()> {
let client_config = client_domains(&config) let client_config = client_domains(&config)
.into_iter() .into_iter()
.find(|c| c.name() == opts.domain_name) .find(|c| c.name() == opts.domain_name)
@ -500,7 +500,7 @@ fn run_mux_client(config: Arc<config::Config>, opts: &ConnectCommand) -> Fallibl
gui.run_forever() gui.run_forever()
} }
fn run_terminal_gui(config: Arc<config::Config>, opts: &StartCommand) -> Fallible<()> { fn run_terminal_gui(config: config::ConfigHandle, opts: &StartCommand) -> Fallible<()> {
#[cfg(unix)] #[cfg(unix)]
{ {
if opts.daemonize { if opts.daemonize {