fix(cli): session names only for attach in fish completion (#2857)

* feat(client): add flag for short output list-sessions

* fix(cli): list session names on fish completion

* chore(client): run cargo fmt
This commit is contained in:
Daniel Jankowski 2023-10-20 17:51:01 +02:00 committed by GitHub
parent bf41b17cc6
commit 35d93189e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 8 deletions

View file

@ -206,14 +206,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); list_sessions(false, false);
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); list_sessions(false, false);
std::process::exit(1); std::process::exit(1);
} }
}, },
@ -355,6 +355,7 @@ fn attach_with_session_name(
.map(|s| (s.clone(), Duration::default(), false)) .map(|s| (s.clone(), Duration::default(), false))
.collect(), .collect(),
false, false,
false,
); );
process::exit(1); process::exit(1);
}, },
@ -372,7 +373,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); list_sessions(false, false);
process::exit(1); process::exit(1);
}, },
}, },

View file

@ -87,8 +87,12 @@ fn main() {
} }
} }
if let Some(Command::Sessions(Sessions::ListSessions { no_formatting })) = opts.command { if let Some(Command::Sessions(Sessions::ListSessions {
commands::list_sessions(no_formatting); no_formatting,
short,
})) = opts.command
{
commands::list_sessions(no_formatting, short);
} else if let Some(Command::Sessions(Sessions::KillAllSessions { yes })) = opts.command { } else if let Some(Command::Sessions(Sessions::KillAllSessions { yes })) = opts.command {
commands::kill_all_sessions(yes); commands::kill_all_sessions(yes);
} else if let Some(Command::Sessions(Sessions::KillSession { ref target_session })) = } else if let Some(Command::Sessions(Sessions::KillSession { ref target_session })) =

View file

@ -146,13 +146,21 @@ fn assert_socket(name: &str) -> bool {
} }
} }
pub(crate) fn print_sessions(mut sessions: Vec<(String, Duration, bool)>, no_formatting: bool) { pub(crate) fn print_sessions(
mut sessions: Vec<(String, Duration, bool)>,
no_formatting: bool,
short: 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| a.1.cmp(&b.1));
sessions sessions
.iter() .iter()
.for_each(|(session_name, timestamp, is_dead)| { .for_each(|(session_name, timestamp, is_dead)| {
if short {
println!("{}", session_name);
return;
}
if no_formatting { if no_formatting {
let suffix = if curr_session == *session_name { let suffix = if curr_session == *session_name {
format!("(current)") format!("(current)")
@ -245,7 +253,7 @@ pub(crate) fn delete_session(name: &str, force: bool) {
} }
} }
pub(crate) fn list_sessions(no_formatting: bool) { pub(crate) fn list_sessions(no_formatting: bool, short: 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();
@ -268,6 +276,7 @@ pub(crate) fn list_sessions(no_formatting: bool) {
}) })
.collect(), .collect(),
no_formatting, no_formatting,
short,
); );
0 0
} }

View file

@ -1,5 +1,5 @@
function __fish_complete_sessions function __fish_complete_sessions
zellij list-sessions 2>/dev/null zellij list-sessions --short --no-formatting 2>/dev/null
end end
complete -c zellij -n "__fish_seen_subcommand_from attach" -f -a "(__fish_complete_sessions)" -d "Session" complete -c zellij -n "__fish_seen_subcommand_from attach" -f -a "(__fish_complete_sessions)" -d "Session"
complete -c zellij -n "__fish_seen_subcommand_from a" -f -a "(__fish_complete_sessions)" -d "Session" complete -c zellij -n "__fish_seen_subcommand_from a" -f -a "(__fish_complete_sessions)" -d "Session"

View file

@ -102,6 +102,10 @@ pub enum Sessions {
/// Do not add colors and formatting to the list (useful for parsing) /// Do not add colors and formatting to the list (useful for parsing)
#[clap(short, long, value_parser, takes_value(false), default_value("false"))] #[clap(short, long, value_parser, takes_value(false), default_value("false"))]
no_formatting: bool, no_formatting: bool,
/// Print just the session name
#[clap(short, long, value_parser, takes_value(false), default_value("false"))]
short: bool,
}, },
/// Attach to a session /// Attach to a session