Skip to content

Commit

Permalink
fix(startup): do not parse resurrectable sessions on startup (zellij-…
Browse files Browse the repository at this point in the history
…org#3505)

* fix(startup): do not parse resurrectable sessions on startup

* style(fmt): rustfmt
  • Loading branch information
imsnif authored and Tomcat-42 committed Nov 9, 2024
1 parent 9511769 commit 93062ae
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
12 changes: 5 additions & 7 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use std::{fs::File, io::prelude::*, path::PathBuf, process, time::Duration};

use crate::sessions::{
assert_dead_session, assert_session, assert_session_ne, delete_session as delete_session_impl,
get_active_session, get_name_generator, get_resurrectable_sessions, get_sessions,
get_sessions_sorted_by_mtime, kill_session as kill_session_impl, match_session_name,
print_sessions, print_sessions_with_index, resurrection_layout, session_exists, ActiveSession,
get_active_session, get_name_generator, get_resurrectable_session_names,
get_resurrectable_sessions, get_sessions, get_sessions_sorted_by_mtime,
kill_session as kill_session_impl, match_session_name, print_sessions,
print_sessions_with_index, resurrection_layout, session_exists, ActiveSession,
SessionNameMatch,
};
use zellij_client::{
Expand Down Expand Up @@ -673,10 +674,7 @@ fn generate_unique_session_name() -> String {
.map(|s| s.0.clone())
.collect::<Vec<String>>()
});
let dead_sessions: Vec<String> = get_resurrectable_sessions()
.iter()
.map(|(s, _, _)| s.clone())
.collect();
let dead_sessions = get_resurrectable_session_names();
let Ok(sessions) = sessions else {
eprintln!("Failed to list existing sessions: {:?}", sessions);
process::exit(1);
Expand Down
35 changes: 33 additions & 2 deletions src/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,37 @@ pub(crate) fn get_resurrectable_sessions() -> Vec<(String, Duration, Layout)> {
}
}

pub(crate) fn get_resurrectable_session_names() -> Vec<String> {
match fs::read_dir(&*ZELLIJ_SESSION_INFO_CACHE_DIR) {
Ok(files_in_session_info_folder) => {
let files_that_are_folders = files_in_session_info_folder
.filter_map(|f| f.ok().map(|f| f.path()))
.filter(|f| f.is_dir());
files_that_are_folders
.filter_map(|folder_name| {
let folder = folder_name.display().to_string();
let resurrection_layout_file = session_layout_cache_file_name(&folder);
if std::path::Path::new(&resurrection_layout_file).exists() {
folder_name
.file_name()
.map(|f| format!("{}", f.to_string_lossy()))
} else {
None
}
})
.collect()
},
Err(e) => {
log::error!(
"Failed to read session_info cache folder: \"{:?}\": {:?}",
&*ZELLIJ_SESSION_INFO_CACHE_DIR,
e
);
vec![]
},
}
}

pub(crate) fn get_sessions_sorted_by_mtime() -> anyhow::Result<Vec<String>> {
match fs::read_dir(&*ZELLIJ_SOCK_DIR) {
Ok(files) => {
Expand Down Expand Up @@ -407,8 +438,8 @@ pub(crate) fn assert_session_ne(name: &str) {

match session_exists(name) {
Ok(result) if !result => {
let resurrectable_sessions = get_resurrectable_sessions();
if resurrectable_sessions.iter().find(|(s, _, _)| s == name).is_some() {
let resurrectable_sessions = get_resurrectable_session_names();
if resurrectable_sessions.iter().find(|s| s == &name).is_some() {
println!("Session with name {:?} already exists, but is dead. Use the attach command to resurrect it or, the delete-session command to kill it or specify a different name.", name);
} else {
return
Expand Down

0 comments on commit 93062ae

Please sign in to comment.