Skip to content

Commit

Permalink
Added hooks to correctly set layout Resolves #6
Browse files Browse the repository at this point in the history
  • Loading branch information
MeirKriheli committed Aug 15, 2023
1 parent ee0a4da commit 22f40e7
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/tmux/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ pub(crate) enum Commands<'a> {
AttachSession { session_name: &'a str },
/// `kill-session` command
StopSession { session_name: &'a str },
/// Set a hook for tmux events
SetHook {
session_name: &'a str,
hook_name: &'a str,
hook_command: String,
},
}

impl<'a> Commands<'a> {
Expand Down Expand Up @@ -223,6 +229,18 @@ impl<'a> Commands<'a> {
write!(f, "{TMUX_BIN} kill-session -t {session_name}")
}

fn fmt_set_hook(
f: &mut fmt::Formatter,
session_name: &str,
hook_name: &str,
hook_command: &str,
) -> Result<(), fmt::Error> {
write!(
f,
"{TMUX_BIN} set-hook -t {session_name} {hook_name} \"{hook_command}\""
)
}

/// Runs the command, based on the enum values.
pub fn run(&self) -> Result<(), TmuxError> {
match self {
Expand Down Expand Up @@ -272,6 +290,11 @@ impl<'a> Commands<'a> {
} => Commands::run_select_pane(session_name, *window_index, *pane_index),
Commands::AttachSession { session_name } => Commands::run_attach_session(session_name),
Commands::StopSession { session_name } => Commands::run_stop_session(session_name),
Commands::SetHook {
session_name,
hook_name,
hook_command,
} => Commands::run_set_hook(session_name, hook_name, hook_command),
}
}

Expand Down Expand Up @@ -470,6 +493,22 @@ impl<'a> Commands<'a> {
)))
}
}

fn run_set_hook(
session_name: &str,
hook_name: &str,
hook_command: &str,
) -> Result<(), TmuxError> {
let args = ["set-hook", "-t", session_name, hook_name, hook_command];
let res = Command::new(TMUX_BIN).args(args).status()?;
if res.success() {
Ok(())
} else {
Err(TmuxError::Message(format!(
"Cannot set {hook_name} hook for session {session_name}"
)))
}
}
}

impl<'a> fmt::Display for Commands<'a> {
Expand Down Expand Up @@ -532,6 +571,11 @@ impl<'a> fmt::Display for Commands<'a> {
Commands::fmt_attach_session(f, session_name)
}
Commands::StopSession { session_name } => Commands::fmt_stop_session(f, session_name),
Commands::SetHook {
session_name,
hook_name,
hook_command,
} => Commands::fmt_set_hook(f, session_name, hook_name, hook_command),
}
}
}
48 changes: 48 additions & 0 deletions src/tmux/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ impl<'a> TmuxProject<'a> {
})
}

if self.tmux.version >= TmuxVersion::Version(2, 6) {
let hook_cmd = self.get_layout_hooks_command();

if let Some(cmd) = hook_cmd {
commands.push(cmd);
}
}

commands.push(self.get_attach_session_command());

commands.push(Commands::ProjectEvent {
Expand Down Expand Up @@ -287,6 +295,46 @@ impl<'a> TmuxProject<'a> {

Ok(res.status.success())
}

/// Get to hook commands to correctly apply layouts
fn get_layout_hooks_command(&self) -> Option<Commands> {
// No windows? No need to set layouts
if self.project.windows.as_ref().is_none() {
return None;
}

let windows = self.project.windows.as_ref().unwrap();
if windows.len() == 0 {
return None;
}

let mut hook_commands: Vec<String> = vec![
"set main-pane-height 66%".into(),
"set main-pane-width 66%".into(),
];
windows.iter().enumerate().for_each(|(idx, w)| {
// Need to select window before applying layout
hook_commands.push(format!("selectw -t {}", self.tmux.base_index + idx));
hook_commands.push(format!("selectl {}", w.layout));
if idx > 0 {
hook_commands.push("selectw -l".into());
}
});

// Once done, unset the hook
hook_commands.push(format!(
"set-hook -u -t {} client-session-changed",
self.project.project_name
));

let hook_command = hook_commands.join(";");

Some(Commands::SetHook {
session_name: &self.project.project_name,
hook_name: "client-session-changed",
hook_command,
})
}
}

impl<'a> fmt::Display for TmuxProject<'a> {
Expand Down

0 comments on commit 22f40e7

Please sign in to comment.