Skip to content

Commit

Permalink
fix(cli): session names only for attach in fish completion (#2857)
Browse files Browse the repository at this point in the history
* feat(client): add flag for short output list-sessions

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

* chore(client): run cargo fmt
  • Loading branch information
dj95 authored Oct 20, 2023
1 parent bf41b17 commit 35d9318
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,14 @@ pub(crate) fn send_action_to_session(
"Session '{}' not found. The following sessions are active:",
session_name
);
list_sessions(false);
list_sessions(false, false);
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);
list_sessions(false, false);
std::process::exit(1);
}
},
Expand Down Expand Up @@ -355,6 +355,7 @@ fn attach_with_session_name(
.map(|s| (s.clone(), Duration::default(), false))
.collect(),
false,
false,
);
process::exit(1);
},
Expand All @@ -372,7 +373,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);
list_sessions(false, false);
process::exit(1);
},
},
Expand Down
8 changes: 6 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,12 @@ fn main() {
}
}

if let Some(Command::Sessions(Sessions::ListSessions { no_formatting })) = opts.command {
commands::list_sessions(no_formatting);
if let Some(Command::Sessions(Sessions::ListSessions {
no_formatting,
short,
})) = opts.command
{
commands::list_sessions(no_formatting, short);
} else if let Some(Command::Sessions(Sessions::KillAllSessions { yes })) = opts.command {
commands::kill_all_sessions(yes);
} else if let Some(Command::Sessions(Sessions::KillSession { ref target_session })) =
Expand Down
13 changes: 11 additions & 2 deletions src/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
let curr_session = envs::get_session_name().unwrap_or_else(|_| "".into());
sessions.sort_by(|a, b| a.1.cmp(&b.1));
sessions
.iter()
.for_each(|(session_name, timestamp, is_dead)| {
if short {
println!("{}", session_name);
return;
}
if no_formatting {
let suffix = if curr_session == *session_name {
format!("(current)")
Expand Down Expand Up @@ -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() {
Ok(running_sessions) => {
let resurrectable_sessions = get_resurrectable_sessions();
Expand All @@ -268,6 +276,7 @@ pub(crate) fn list_sessions(no_formatting: bool) {
})
.collect(),
no_formatting,
short,
);
0
}
Expand Down
2 changes: 1 addition & 1 deletion zellij-utils/assets/completions/comp.fish
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function __fish_complete_sessions
zellij list-sessions 2>/dev/null
zellij list-sessions --short --no-formatting 2>/dev/null
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 a" -f -a "(__fish_complete_sessions)" -d "Session"
Expand Down
4 changes: 4 additions & 0 deletions zellij-utils/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ pub enum Sessions {
/// Do not add colors and formatting to the list (useful for parsing)
#[clap(short, long, value_parser, takes_value(false), default_value("false"))]
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
Expand Down

0 comments on commit 35d9318

Please sign in to comment.