Skip to content

Commit

Permalink
Fix bug where window resizing breaks if a session is launched via twm (
Browse files Browse the repository at this point in the history
…#17)

Addresses the issue with gh-16 where resizing the window breaks
everything if your tmux session was attached to via execvp. I still have
*no* idea why this is happening, but this seems to fix it at least;
instead of calling execvp with tmux as the target program, we call it to
whatever the user's shell is with `-c tmux attach -t <session_name>` as
the arguments. Tested this across multiple shells and it seems to work
fine.
  • Loading branch information
vinnymeller authored Jan 31, 2024
1 parent a3cf447 commit 8608431
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions src/tmux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use crate::config::{TwmGlobal, TwmLocal};
use crate::matches::SafePath;
use crate::picker::get_skim_selection_from_slice;
use anyhow::{bail, Context, Result};
use libc::{execvp,c_char};
use std::ffi::CString;
use std::os::unix::process::CommandExt;
use std::path::Path;
use std::process::{Command, Output};

Expand Down Expand Up @@ -35,7 +34,6 @@ impl From<&str> for SessionName {
SessionName { name }
}
}

fn run_tmux_command(args: &[&str]) -> Result<Output> {
let output = Command::new("tmux")
.args(args)
Expand Down Expand Up @@ -92,25 +90,16 @@ fn attach_to_tmux_session(session_name: &str) -> Result<()> {
}
}

fn attach_to_tmux_session_outside_tmux(repo_name: &str) -> Result<()> {
let tmux_attach = CString::new("tmux").unwrap();
let tmux_attach_args = vec![
CString::new("tmux").unwrap(),
CString::new("attach").unwrap(),
CString::new("-t").unwrap(),
CString::new(repo_name).with_context(|| "Unable to turn repo name to a cstring.")?,
];

let tmux_attach_args_ptrs: Vec<*const c_char> = tmux_attach_args
.iter()
.map(|arg| arg.as_ptr() as *const c_char)
.chain(std::iter::once(std::ptr::null()))
.collect();

unsafe {
execvp(tmux_attach.as_ptr(), tmux_attach_args_ptrs.as_ptr());
}
Err(anyhow::anyhow!("Unable to attach to tmux session!"))
fn attach_to_tmux_session_outside_tmux(session_name: &str) -> Result<()> {
let shell = std::env::var("SHELL").unwrap_or("sh".to_string());
let exec_error = Command::new(shell)
.args(["-c", format!("tmux attach -t {}", session_name).as_str()])
.exec();
anyhow::bail!(
"Failed to attach to tmux session with name {repo_name} outside tmux: {exec_error}",
repo_name = session_name,
exec_error = exec_error
);
}

fn tmux_has_session(session_name: &SessionName) -> bool {
Expand Down

0 comments on commit 8608431

Please sign in to comment.