Implemented the pause in tmux in a different way

This commit is contained in:
Roey Darwish Dror
2019-06-13 09:21:39 +03:00
parent 9430ab30f6
commit e5d0f4cbfa
3 changed files with 49 additions and 23 deletions

View File

@@ -8,7 +8,7 @@ use std::env;
use std::io;
use std::os::unix::process::CommandExt;
use std::path::Path;
use std::process::Command;
use std::process::{exit, Command};
pub fn run_tpm(base_dirs: &BaseDirs, run_type: RunType) -> Result<(), Error> {
let tpm = base_dirs
@@ -24,6 +24,16 @@ pub fn run_tpm(base_dirs: &BaseDirs, run_type: RunType) -> Result<(), Error> {
fn has_session(tmux: &Path, session_name: &str) -> Result<bool, io::Error> {
Ok(Command::new(tmux)
.args(&["has-session", "-t", session_name])
.env_remove("TMUX")
.output()?
.status
.success())
}
fn new_session(tmux: &Path, session_name: &str) -> Result<bool, io::Error> {
Ok(Command::new(tmux)
.args(&["new-session", "-d", "-s", session_name, "-n", "dummy"])
.env_remove("TMUX")
.spawn()?
.wait()?
.success())
@@ -31,7 +41,8 @@ fn has_session(tmux: &Path, session_name: &str) -> Result<bool, io::Error> {
fn run_in_session(tmux: &Path, command: &str) -> Result<(), Error> {
Command::new(tmux)
.args(&["new-window", "-a", "-t", "topgrade:1", command])
.args(&["new-window", "-a", "-t", "topgrade:1", "-n", "local", command])
.env_remove("TMUX")
.spawn()
.context(ErrorKind::ProcessExecution)?
.wait()
@@ -43,30 +54,31 @@ fn run_in_session(tmux: &Path, command: &str) -> Result<(), Error> {
pub fn run_in_tmux() -> ! {
let tmux = which("tmux").expect("Could not find tmux");
let command = env::args().collect::<Vec<String>>().join(" ");
let command = {
let mut command = vec![
String::from("env"),
String::from("TOPGRADE_KEEP_END=1"),
String::from("TOPGRADE_INSIDE_TMUX=1"),
];
command.extend(env::args());
command.join(" ")
};
if has_session(&tmux, "topgrade").expect("Error launching tmux") {
run_in_session(&tmux, &command).expect("Error launching tmux");
if !has_session(&tmux, "topgrade").expect("Error launching tmux") {
new_session(&tmux, "topgrade").expect("Error launching tmux");
}
run_in_session(&tmux, &command).expect("Error launching tmux");
Command::new(&tmux)
.args(&["kill-window", "-t", "topgrade:dummy"])
.output()
.unwrap();
if env::var("TMUX").is_err() {
let err = Command::new(tmux).args(&["attach", "-t", "topgrade"]).exec();
panic!("{:?}", err);
} else {
let err = Command::new(tmux)
.args(&[
"new-session",
"-s",
"topgrade",
"-n",
"topgrade",
&command,
";",
"set",
"remain-on-exit",
"on",
])
.exec();
panic!("{:?}", err);
println!("Topgrade launched in a new tmux session");
exit(0);
}
}