Skip to content

Commit

Permalink
feat(restore): WIP assemble all pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
graelo committed Aug 18, 2022
1 parent 0bf3738 commit 5f0df8a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
20 changes: 15 additions & 5 deletions src/actions/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P: AsRef<Path>>(backup_filepath: P) -> Result<v1::Overview> {
let start = Instant::now();
tmux::server::start().await?;
tmux::server::start(PLACEHOLDER_SESSION_NAME).await?;
let elapsed = start.elapsed();
println!("start server: {:?}", elapsed);

Expand Down Expand Up @@ -53,15 +56,18 @@ pub async fn restore<P: AsRef<Path>>(backup_filepath: P) -> Result<v1::Overview>
handles.push(handle);
}

if let Err(e) = join_all(handles)
let pairs: Vec<Pair> = match join_all(handles)
.await
.into_iter()
.collect::<Result<Vec<_>, 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);

Expand All @@ -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<Window>,
Expand Down
11 changes: 4 additions & 7 deletions src/tmux/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -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)?;
Expand Down

0 comments on commit 5f0df8a

Please sign in to comment.