diff options
-rw-r--r-- | src/commands.rs | 7 | ||||
-rw-r--r-- | src/main.rs | 3 | ||||
-rw-r--r-- | src/sessions.rs | 13 | ||||
-rw-r--r-- | zellij-utils/src/cli.rs | 4 |
4 files changed, 21 insertions, 6 deletions
diff --git a/src/commands.rs b/src/commands.rs index f27f0c550..d37a422a0 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -207,14 +207,14 @@ pub(crate) fn send_action_to_session( "Session '{}' not found. The following sessions are active:", session_name ); - list_sessions(false, false); + list_sessions(false, false, true); std::process::exit(1); } } else if let Ok(session_name) = envs::get_session_name() { attach_with_cli_client(cli_action, &session_name, config); } else { eprintln!("Please specify the session name to send actions to. The following sessions are active:"); - list_sessions(false, false); + list_sessions(false, false, true); std::process::exit(1); } }, @@ -357,6 +357,7 @@ fn attach_with_session_name( .collect(), false, false, + true, ); process::exit(1); }, @@ -374,7 +375,7 @@ fn attach_with_session_name( ActiveSession::One(session_name) => ClientInfo::Attach(session_name, config_options), ActiveSession::Many => { println!("Please specify the session to attach to, either by using the full name or a unique prefix.\nThe following sessions are active:"); - list_sessions(false, false); + list_sessions(false, false, true); process::exit(1); }, }, diff --git a/src/main.rs b/src/main.rs index 0f0d980b6..eacefc8a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -167,9 +167,10 @@ fn main() { if let Some(Command::Sessions(Sessions::ListSessions { no_formatting, short, + reverse, })) = opts.command { - commands::list_sessions(no_formatting, short); + commands::list_sessions(no_formatting, short, reverse); } else if let Some(Command::Sessions(Sessions::ListAliases)) = opts.command { commands::list_aliases(opts); } else if let Some(Command::Sessions(Sessions::KillAllSessions { yes })) = opts.command { diff --git a/src/sessions.rs b/src/sessions.rs index 587220715..75e8ddf66 100644 --- a/src/sessions.rs +++ b/src/sessions.rs @@ -143,10 +143,18 @@ pub(crate) fn print_sessions( mut sessions: Vec<(String, Duration, bool)>, no_formatting: bool, short: bool, + reverse: bool, ) { // (session_name, timestamp, is_dead) let curr_session = envs::get_session_name().unwrap_or_else(|_| "".into()); - sessions.sort_by(|a, b| a.1.cmp(&b.1)); + sessions.sort_by(|a, b| { + if reverse { + // sort by `Duration` ascending (newest would be first) + a.1.cmp(&b.1) + } else { + b.1.cmp(&a.1) + } + }); sessions .iter() .for_each(|(session_name, timestamp, is_dead)| { @@ -246,7 +254,7 @@ pub(crate) fn delete_session(name: &str, force: bool) { } } -pub(crate) fn list_sessions(no_formatting: bool, short: bool) { +pub(crate) fn list_sessions(no_formatting: bool, short: bool, reverse: bool) { let exit_code = match get_sessions() { Ok(running_sessions) => { let resurrectable_sessions = get_resurrectable_sessions(); @@ -270,6 +278,7 @@ pub(crate) fn list_sessions(no_formatting: bool, short: bool) { .collect(), no_formatting, short, + reverse, ); 0 } diff --git a/zellij-utils/src/cli.rs b/zellij-utils/src/cli.rs index 3d30bb391..f224b5ef1 100644 --- a/zellij-utils/src/cli.rs +++ b/zellij-utils/src/cli.rs @@ -106,6 +106,10 @@ pub enum Sessions { /// Print just the session name #[clap(short, long, value_parser, takes_value(false), default_value("false"))] short: bool, + + /// List the sessions in reverse order (default is ascending order) + #[clap(short, long, value_parser, takes_value(false), default_value("false"))] + reverse: bool, }, /// List existing plugin aliases #[clap(visible_alias = "la")] |