diff --git a/src/actions/restore.rs b/src/actions/restore.rs index 802a22a..f9dec62 100644 --- a/src/actions/restore.rs +++ b/src/actions/restore.rs @@ -12,10 +12,13 @@ use crate::{ tmux::{self, pane::Pane, session::Session, window::Window}, }; +/// Name of the placeholder session. +const PLACEHOLDER_SESSION_NAME: &str = "[placeholder]"; + /// Restore all sessions, windows & panes from the backup file. pub async fn restore>(backup_filepath: P) -> Result { let start = Instant::now(); - tmux::server::start().await?; + tmux::server::start(PLACEHOLDER_SESSION_NAME).await?; let elapsed = start.elapsed(); println!("start server: {:?}", elapsed); @@ -53,15 +56,18 @@ pub async fn restore>(backup_filepath: P) -> Result handles.push(handle); } - if let Err(e) = join_all(handles) + let pairs: Vec = match join_all(handles) .await .into_iter() .collect::, ParseError>>() { - return Err(anyhow::anyhow!("error: {e}")); - } + Ok(vec_pairs) => vec_pairs.into_iter().flatten().collect(), + Err(e) => return Err(anyhow::anyhow!("error: {e}")), + }; - tmux::server::kill_placeholder_session().await?; // created above by server::start() + eprintln!("num pairs: {}", pairs.len()); + + tmux::server::kill_session(PLACEHOLDER_SESSION_NAME).await?; // created above by server::start() let elapsed = start.elapsed(); println!("create sessions: {:?}", elapsed); @@ -82,6 +88,10 @@ struct Pair { /// The session is created with the first window in order to give it the right name. The remainder /// of windows are created in sequence, to preserve the order from the backup. /// +/// # Note +/// +/// This strategy is faster than creating a placeholder window and removing it at the end (checked +/// multiple times). async fn restore_session( session: Session, related_windows: Vec, diff --git a/src/tmux/server.rs b/src/tmux/server.rs index fb50227..98bb04a 100644 --- a/src/tmux/server.rs +++ b/src/tmux/server.rs @@ -4,15 +4,12 @@ use async_std::process::Command; use crate::error::ParseError; -/// Name of the placeholder session. -const PLACEHOLDER_SESSION_NAME: &str = "[placeholder]"; - /// Start the Tmux server if needed, creating a session named `"[placeholder]"` in order to keep the server /// running. /// /// It is ok-ish to already have an existing session named `"[placeholder]"`. -pub async fn start() -> Result<(), ParseError> { - let args = vec!["new-session", "-d", "-s", PLACEHOLDER_SESSION_NAME]; +pub async fn start(initial_session_name: &str) -> Result<(), ParseError> { + let args = vec!["new-session", "-d", "-s", initial_session_name]; let output = Command::new("tmux").args(&args).output().await?; let buffer = String::from_utf8(output.stdout)?; @@ -24,8 +21,8 @@ pub async fn start() -> Result<(), ParseError> { } /// Remove the session named `"[placeholder]"` used to keep the server alive. -pub async fn kill_placeholder_session() -> Result<(), ParseError> { - let args = vec!["kill-session", "-t", PLACEHOLDER_SESSION_NAME]; +pub async fn kill_session(name: &str) -> Result<(), ParseError> { + let args = vec!["kill-session", "-t", name]; let output = Command::new("tmux").args(&args).output().await?; let buffer = String::from_utf8(output.stdout)?;