The -t flag now works when the tmux session already exists (fix #54)
This commit is contained in:
59
src/tmux.rs
59
src/tmux.rs
@@ -5,7 +5,9 @@ use super::utils::{Check, PathExt};
|
|||||||
use directories::BaseDirs;
|
use directories::BaseDirs;
|
||||||
use failure::Error;
|
use failure::Error;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
use std::io;
|
||||||
use std::os::unix::process::CommandExt;
|
use std::os::unix::process::CommandExt;
|
||||||
|
use std::path::Path;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
pub fn run_tpm(base_dirs: &BaseDirs, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
|
pub fn run_tpm(base_dirs: &BaseDirs, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
|
||||||
@@ -27,23 +29,50 @@ pub fn run_tpm(base_dirs: &BaseDirs, terminal: &mut Terminal, dry_run: bool) ->
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn has_session(tmux: &Path, session_name: &str) -> Result<bool, io::Error> {
|
||||||
|
Ok(Command::new(tmux)
|
||||||
|
.args(&["has-session", "-t", session_name])
|
||||||
|
.spawn()?
|
||||||
|
.wait()?
|
||||||
|
.success())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_in_session(tmux: &Path, command: &str) -> Result<(), Error> {
|
||||||
|
Command::new(tmux)
|
||||||
|
.args(&["new-window", "-a", "-t", "topgrade:1", command])
|
||||||
|
.spawn()?
|
||||||
|
.wait()?
|
||||||
|
.check()?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn run_in_tmux() -> ! {
|
pub fn run_in_tmux() -> ! {
|
||||||
let tmux = which("tmux").expect("Could not find tmux");
|
let tmux = which("tmux").expect("Could not find tmux");
|
||||||
|
let command = env::args().collect::<Vec<String>>().join(" ");
|
||||||
|
|
||||||
let err = Command::new(tmux)
|
if has_session(&tmux, "topgrade").expect("Error launching tmux") {
|
||||||
.args(&[
|
run_in_session(&tmux, &command).expect("Error launching tmux");
|
||||||
"new-session",
|
|
||||||
"-s",
|
|
||||||
"topgrade",
|
|
||||||
"-n",
|
|
||||||
"topgrade",
|
|
||||||
&env::args().collect::<Vec<String>>().join(" "),
|
|
||||||
";",
|
|
||||||
"set",
|
|
||||||
"remain-on-exit",
|
|
||||||
"on",
|
|
||||||
])
|
|
||||||
.exec();
|
|
||||||
|
|
||||||
panic!("{:?}", 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user