feat: list-sessions show newest sessions last, for better user experience (#3194)

* feat: sort `list-sessions` from oldest to newest

by putting the most recent sessions last, the user won't need to scroll back up
to see active sessions when there are a lot of resurrectable sessions.

* feat: add an `--reverse` option to the `list-sessions` subcommand

the `--reverse` flag reverts sorting order back to the old "newest sessions
first".  also updated call sites of `list_sessions` and `print_sessions` with
`reverse: true`, to keep the original behavior everywhere else except the output
of `list-sessions` subcommand.

* chore: update the help message

---------

Co-authored-by: Jae-Heon Ji <atx6419@gmail.com>
This commit is contained in:
toymil 2024-03-24 22:30:47 +08:00 committed by GitHub
parent 5fb75ab6d1
commit 9deb033340
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 21 additions and 6 deletions

View file

@ -207,14 +207,14 @@ pub(crate) fn send_action_to_session(
"Session '{}' not found. The following sessions are active:", "Session '{}' not found. The following sessions are active:",
session_name session_name
); );
list_sessions(false, false); list_sessions(false, false, true);
std::process::exit(1); std::process::exit(1);
} }
} else if let Ok(session_name) = envs::get_session_name() { } else if let Ok(session_name) = envs::get_session_name() {
attach_with_cli_client(cli_action, &session_name, config); attach_with_cli_client(cli_action, &session_name, config);
} else { } else {
eprintln!("Please specify the session name to send actions to. The following sessions are active:"); 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); std::process::exit(1);
} }
}, },
@ -357,6 +357,7 @@ fn attach_with_session_name(
.collect(), .collect(),
false, false,
false, false,
true,
); );
process::exit(1); process::exit(1);
}, },
@ -374,7 +375,7 @@ fn attach_with_session_name(
ActiveSession::One(session_name) => ClientInfo::Attach(session_name, config_options), ActiveSession::One(session_name) => ClientInfo::Attach(session_name, config_options),
ActiveSession::Many => { 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:"); 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); process::exit(1);
}, },
}, },

View file

@ -167,9 +167,10 @@ fn main() {
if let Some(Command::Sessions(Sessions::ListSessions { if let Some(Command::Sessions(Sessions::ListSessions {
no_formatting, no_formatting,
short, short,
reverse,
})) = opts.command })) = 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 { } else if let Some(Command::Sessions(Sessions::ListAliases)) = opts.command {
commands::list_aliases(opts); commands::list_aliases(opts);
} else if let Some(Command::Sessions(Sessions::KillAllSessions { yes })) = opts.command { } else if let Some(Command::Sessions(Sessions::KillAllSessions { yes })) = opts.command {

View file

@ -143,10 +143,18 @@ pub(crate) fn print_sessions(
mut sessions: Vec<(String, Duration, bool)>, mut sessions: Vec<(String, Duration, bool)>,
no_formatting: bool, no_formatting: bool,
short: bool, short: bool,
reverse: bool,
) { ) {
// (session_name, timestamp, is_dead) // (session_name, timestamp, is_dead)
let curr_session = envs::get_session_name().unwrap_or_else(|_| "".into()); 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 sessions
.iter() .iter()
.for_each(|(session_name, timestamp, is_dead)| { .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() { let exit_code = match get_sessions() {
Ok(running_sessions) => { Ok(running_sessions) => {
let resurrectable_sessions = get_resurrectable_sessions(); let resurrectable_sessions = get_resurrectable_sessions();
@ -270,6 +278,7 @@ pub(crate) fn list_sessions(no_formatting: bool, short: bool) {
.collect(), .collect(),
no_formatting, no_formatting,
short, short,
reverse,
); );
0 0
} }

View file

@ -106,6 +106,10 @@ pub enum Sessions {
/// Print just the session name /// Print just the session name
#[clap(short, long, value_parser, takes_value(false), default_value("false"))] #[clap(short, long, value_parser, takes_value(false), default_value("false"))]
short: bool, 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 /// List existing plugin aliases
#[clap(visible_alias = "la")] #[clap(visible_alias = "la")]