Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kill session: switch to last attached if enabled in config #72

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,21 +385,33 @@ fn kill_subcommand(config: Config) -> Result<(), TmsError> {
.expect("The tmux command static string should always be valid utf-9");
current_session.retain(|x| x != '\'' && x != '\n');

let sessions = String::from_utf8(execute_tmux_command("tmux list-sessions -F #S").stdout)
let sessions = String::from_utf8(execute_tmux_command("tmux list-sessions -F '#{?session_attached,,#{session_name}#,#{session_last_attached}}").stdout)
.expect("The tmux command static string should always be valid utf-9");
let sessions: Vec<&str> = sessions.lines().collect();
let cleaned = sessions.replace('\'', "").replace("\n\n", "\n");
let mut sessions: Vec<(&str, &str)> = cleaned
.trim()
.split('\n')
.filter_map(|s| s.split_once(','))
.collect();

if let Some(SessionSortOrderConfig::LastAttached) = config.session_sort_order {
sessions.sort_by(|a, b| b.1.cmp(a.1));
}

let to_session = if config.default_session.is_some()
&& sessions.contains(&config.default_session.as_deref().unwrap())
&& sessions
.iter()
.find(|session| session.0 == config.default_session.as_deref().unwrap())
.is_some()
&& current_session != config.default_session.as_deref().unwrap()
{
config.default_session.as_deref().unwrap()
} else if current_session != sessions[0] {
sessions[0]
config.default_session.as_deref()
} else {
sessions.get(1).unwrap_or_else(|| &sessions[0])
sessions.first().map(|s| s.0)
};
execute_tmux_command(&format!("tmux switch-client -t {to_session}"));
if let Some(to_session) = to_session {
execute_tmux_command(&format!("tmux switch-client -t {to_session}"));
}
execute_tmux_command(&format!("tmux kill-session -t {current_session}"));

Ok(())
Expand Down
Loading