diff --git a/Cargo.lock b/Cargo.lock index 08410cd92..efec62f21 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3298,6 +3298,7 @@ dependencies = [ "tabout", "term", "termwiz", + "textwrap", "thiserror", "toml", "uds_windows", diff --git a/Cargo.toml b/Cargo.toml index 3b30733a2..3e5805896 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,6 +54,7 @@ structopt = "0.2" tabout = { path = "tabout" } term = { path = "term" } termwiz = { path = "termwiz"} +textwrap = "0.11" toml = "0.4" unicode-normalization = "0.1" unicode-segmentation = "1.5" diff --git a/src/config/mod.rs b/src/config/mod.rs index b0d51067f..bfa3e39ca 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -155,8 +155,7 @@ impl ConfigInner { } Err(err) => { let err = format!("{:#}", err); - log::error!("While (re)loading configuration: {}", err); - crate::toast_notification("Wezterm Configuration", &err); + crate::termwiztermtab::show_configuration_error_message(&err); self.error.replace(err); } } diff --git a/src/frontend/mod.rs b/src/frontend/mod.rs index 7e306d2e0..6433229dd 100644 --- a/src/frontend/mod.rs +++ b/src/frontend/mod.rs @@ -6,6 +6,7 @@ use downcast_rs::{impl_downcast, Downcast}; use serde::Deserialize; use std::cell::RefCell; use std::rc::Rc; +use std::sync::atomic::{AtomicBool, Ordering}; pub mod activity; pub mod gui; @@ -29,6 +30,15 @@ thread_local! { static FRONT_END: RefCell>> = RefCell::new(None); } +static HAS_GUI_FRONT_END: AtomicBool = AtomicBool::new(false); + +/// Returns true if a GUI frontend has been initialized, which implies that +/// it makes sense (and is safe) to use the window crate and associated +/// functionality +pub fn has_gui_front_end() -> bool { + HAS_GUI_FRONT_END.load(Ordering::Acquire) +} + pub fn front_end() -> Option> { let mut res = None; FRONT_END.with(|f| { @@ -41,14 +51,17 @@ pub fn front_end() -> Option> { impl FrontEndSelection { pub fn try_new(self) -> Result, Error> { - let front_end = match self { - FrontEndSelection::MuxServer => muxserver::MuxServerFrontEnd::try_new(), - FrontEndSelection::Null => muxserver::MuxServerFrontEnd::new_null(), - FrontEndSelection::Software => gui::GuiFrontEnd::try_new_no_opengl(), - FrontEndSelection::OpenGL => gui::GuiFrontEnd::try_new(), - }?; + let (front_end, is_gui) = match self { + FrontEndSelection::MuxServer => (muxserver::MuxServerFrontEnd::try_new(), false), + FrontEndSelection::Null => (muxserver::MuxServerFrontEnd::new_null(), false), + FrontEndSelection::Software => (gui::GuiFrontEnd::try_new_no_opengl(), true), + FrontEndSelection::OpenGL => (gui::GuiFrontEnd::try_new(), true), + }; + + let front_end = front_end?; FRONT_END.with(|f| *f.borrow_mut() = Some(Rc::clone(&front_end))); + HAS_GUI_FRONT_END.store(is_gui, Ordering::Release); Ok(front_end) } diff --git a/src/main.rs b/src/main.rs index 1252cb513..7b67ff2bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -411,6 +411,7 @@ fn run_ssh(config: config::ConfigHandle, opts: SshCommand) -> anyhow::Result<()> drop(activity); }); + maybe_show_configuration_error_window(); gui.run_forever() } @@ -435,6 +436,7 @@ fn run_serial(config: config::ConfigHandle, opts: &SerialCommand) -> anyhow::Res let tab = block_on(domain.spawn(PtySize::default(), None, None, window_id))?; // FIXME: blocking gui.spawn_new_window(&fontconfig, &tab, window_id)?; + maybe_show_configuration_error_window(); gui.run_forever() } @@ -614,6 +616,7 @@ fn run_terminal_gui(config: config::ConfigHandle, opts: StartCommand) -> anyhow: drop(activity); }); + maybe_show_configuration_error_window(); gui.run_forever() } @@ -687,6 +690,13 @@ fn main() { } } +fn maybe_show_configuration_error_window() { + if let Err(err) = config::configuration_result() { + let err = format!("{:#}", err); + termwiztermtab::show_configuration_error_message(&err); + } +} + fn run() -> anyhow::Result<()> { // This is a bit gross. // In order to not to automatically open a standard windows console when @@ -727,14 +737,7 @@ fn run() -> anyhow::Result<()> { if !opts.skip_config { config::reload(); } - let config = match config::configuration_result() { - Err(err) => { - let err = format!("{:#}", err); - toast_notification("Wezterm configuration", &err); - config::configuration() - } - Ok(config) => config, - }; + let config = crate::config::configuration(); #[cfg(target_os = "macos")] { diff --git a/src/termwiztermtab.rs b/src/termwiztermtab.rs index 6d17d6805..38c289bee 100644 --- a/src/termwiztermtab.rs +++ b/src/termwiztermtab.rs @@ -463,3 +463,34 @@ pub fn message_box_ok(message: &str) { })) .ok(); } + +/// If the GUI has been started, pops up a window with the supplied error +/// message framed as a configuration error. +/// If there is no GUI front end, generates a toast notification instead. +/// This is a little bit lame in that it will open a new window for each error +/// event rather than updating a prior window. +pub fn show_configuration_error_message(err: &str) { + log::error!("While (re)loading configuration: {}", err); + if crate::frontend::has_gui_front_end() { + let wrapped = textwrap::fill(&err, 78); + let mut message = wrapped.replace("\n", "\r\n"); + message.push_str("\r\n\r\n"); + + promise::spawn::spawn_into_main_thread(run(80, 24, move |mut term| { + term.render(&[ + Change::Title("Wezterm Configuration Error".to_string()), + Change::Text(message.to_string()), + ]) + .map_err(Error::msg)?; + + let mut editor = LineEditor::new(term); + editor.set_prompt("(press enter to close this window)"); + + let mut host = NopLineEditorHost::default(); + editor.read_line(&mut host).ok(); + Ok(()) + })); + } else { + crate::toast_notification("Wezterm Configuration", &err); + } +}