aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJae-Heon Ji <32578710+jaeheonji@users.noreply.github.com>2021-10-11 23:57:56 +0900
committerGitHub <noreply@github.com>2021-10-11 16:57:56 +0200
commit0ca5c187295a157d9e6d24676146681da4fa0188 (patch)
tree0b4cba0de0444319c66c4342828863613f52d588 /src
parentdocs(changelog): update screen/tab client refactor (diff)
downloadsl0ppij-0ca5c187295a157d9e6d24676146681da4fa0188.tar.gz
sl0ppij-0ca5c187295a157d9e6d24676146681da4fa0188.tar.xz
sl0ppij-0ca5c187295a157d9e6d24676146681da4fa0188.zip
feat(cli): add `kill-session` command (#745)
* feat: add kill-session command * style: apply formatting rules * feat: add new instruction for kill-session * feat: update feedback of kill-session * separation of command `kill-session` and `kill-all-sessions` function. * Add information to various situations * Add a question (yes or no) when executing the `kill-all-sessions` command. * chore: adjust clippy warning * fix warning wording * make rustfmt happy Co-authored-by: Aram Drevekenin <aram@poor.dev>
Diffstat (limited to 'src')
-rw-r--r--src/main.rs61
-rw-r--r--src/sessions.rs13
2 files changed, 72 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs
index 90eeb96b..70734ae4 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -5,8 +5,8 @@ mod tests;
use crate::install::populate_data_dir;
use sessions::{
- assert_session, assert_session_ne, get_active_session, get_sessions, list_sessions,
- print_sessions, session_exists, ActiveSession,
+ assert_session, assert_session_ne, get_active_session, get_sessions, kill_session,
+ list_sessions, print_sessions, session_exists, ActiveSession,
};
use std::process;
use zellij_client::{os_input_output::get_client_os_input, start_client, ClientInfo};
@@ -27,6 +27,63 @@ pub fn main() {
list_sessions();
}
+ if let Some(Command::Sessions(Sessions::KillAllSessions { yes })) = opts.command {
+ match get_sessions() {
+ Ok(sessions) => {
+ if sessions.is_empty() {
+ println!("No active zellij sessions found.");
+ process::exit(1);
+ } else {
+ let kill_all_sessions = |sessions: Vec<std::string::String>| {
+ for session in sessions.iter() {
+ kill_session(session);
+ }
+ process::exit(0)
+ };
+
+ if yes {
+ kill_all_sessions(sessions);
+ } else {
+ use std::io::{stdin, stdout, Write};
+
+ let mut answer = String::new();
+ println!("WARNING: this action will kill all sessions.");
+ print!("Do you want to continue? [y/N] ");
+ let _ = stdout().flush();
+ stdin().read_line(&mut answer).unwrap();
+
+ match answer.as_str().trim() {
+ "y" | "Y" | "yes" | "Yes" => kill_all_sessions(sessions),
+ _ => {
+ println!("Abort.");
+ process::exit(1);
+ }
+ }
+ }
+ }
+ }
+ Err(e) => {
+ eprintln!("Error occured: {:?}", e);
+ process::exit(1);
+ }
+ }
+ }
+
+ if let Some(Command::Sessions(Sessions::KillSession { target_session })) = opts.command.clone()
+ {
+ match target_session.as_ref() {
+ Some(target_session) => {
+ assert_session(target_session);
+ kill_session(target_session);
+ process::exit(0);
+ }
+ None => {
+ println!("Please specify the session name to kill.");
+ process::exit(1);
+ }
+ }
+ }
+
atomic_create_dir(&*ZELLIJ_TMP_DIR).unwrap();
atomic_create_dir(&*ZELLIJ_TMP_LOG_DIR).unwrap();
if let Some(path) = opts.server {
diff --git a/src/sessions.rs b/src/sessions.rs
index 9f1e20f0..c2d8afea 100644
--- a/src/sessions.rs
+++ b/src/sessions.rs
@@ -84,6 +84,19 @@ pub(crate) fn get_active_session() -> ActiveSession {
}
}
+pub(crate) fn kill_session(name: &str) {
+ let path = &*ZELLIJ_SOCK_DIR.join(name);
+ match LocalSocketStream::connect(path) {
+ Ok(stream) => {
+ IpcSenderWithContext::new(stream).send(ClientToServerMsg::KillSession);
+ }
+ Err(e) => {
+ eprintln!("Error occured: {:?}", e);
+ process::exit(1);
+ }
+ };
+}
+
pub(crate) fn list_sessions() {
let exit_code = match get_sessions() {
Ok(sessions) => {