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

fix: change the way sessions are sorted #1347

Merged
merged 3 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::install::populate_data_dir;
use crate::sessions::kill_session as kill_session_impl;
use crate::sessions::{
assert_session, assert_session_ne, get_active_session, get_sessions,
get_sessions_sorted_by_creation_date, print_sessions, print_sessions_with_index,
session_exists, ActiveSession,
get_sessions_sorted_by_mtime, print_sessions, print_sessions_with_index, session_exists,
ActiveSession,
};
use dialoguer::Confirm;
use std::path::PathBuf;
Expand Down Expand Up @@ -114,7 +114,7 @@ fn find_indexed_session(

fn attach_with_session_index(config_options: Options, index: usize, create: bool) -> ClientInfo {
// Ignore the session_name when `--index` is provided
match get_sessions_sorted_by_creation_date() {
match get_sessions_sorted_by_mtime() {
Ok(sessions) if sessions.is_empty() => {
if create {
create_new_client()
Expand Down
15 changes: 6 additions & 9 deletions src/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,21 @@ pub(crate) fn get_sessions() -> Result<Vec<String>, io::ErrorKind> {
}
}

pub(crate) fn get_sessions_sorted_by_creation_date() -> anyhow::Result<Vec<String>> {
pub(crate) fn get_sessions_sorted_by_mtime() -> anyhow::Result<Vec<String>> {
match fs::read_dir(&*ZELLIJ_SOCK_DIR) {
Ok(files) => {
let mut sessions_with_creation_date: Vec<(String, SystemTime)> = Vec::new();
let mut sessions_with_mtime: Vec<(String, SystemTime)> = Vec::new();
for file in files {
let file = file?;
let file_name = file.file_name().into_string().unwrap();
let file_created_at = file.metadata()?.created()?;
let file_modified_at = file.metadata()?.modified()?;
if file.file_type()?.is_socket() && assert_socket(&file_name) {
sessions_with_creation_date.push((file_name, file_created_at));
sessions_with_mtime.push((file_name, file_modified_at));
}
}
sessions_with_creation_date.sort_by_key(|x| x.1); // the oldest one will be the first
sessions_with_mtime.sort_by_key(|x| x.1); // the oldest one will be the first

let sessions = sessions_with_creation_date
.iter()
.map(|x| x.0.clone())
.collect();
let sessions = sessions_with_mtime.iter().map(|x| x.0.clone()).collect();
Ok(sessions)
}
Err(err) if io::ErrorKind::NotFound != err.kind() => Err(err.into()),
Expand Down