Repository: zellij-org/zellij
File: src/main.rs (Rust)

Code before:
use zellij_server::{os_input_output::get_server_os_input, start_server};
use zellij_utils::{
    cli::{CliArgs, Command, SessionCommand, Sessions},
    consts::{ZELLIJ_TMP_DIR, ZELLIJ_TMP_LOG_DIR},
    logging::*,
    setup::{get_default_data_dir, Setup},
    structopt::StructOpt,
};

pub fn main() {
    configure_logger();
    let opts = CliArgs::from_args();

    if let Some(Command::Sessions(Sessions::ListSessions)) = opts.command {
        list_sessions();
    }

    if let Some(Command::Sessions(Sessions::KillSession {
        all,
        target_session,
    })) = opts.command.clone()
    {
        if all {
            match get_sessions() {
                Ok(sessions) => match target_session {
                    Some(target) => {
                        for session in sessions.iter() {
                            if session == &target {
                                continue;
                            }
                            kill_session(session);
                        }
                    }
                    None => {
                        for session in sessions.iter() {
                            kill_session(session);
                        }
                    }
                },
                Err(e) => {
                    eprintln!("Error occured: {:?}", e);
                    process::exit(1);
                }
            }
        } else {
            kill_session(target_session.as_ref().unwrap());
        }

        process::exit(0);
    }


Review comment (suggestion):
I think this kills every session *except* the target... is this how it's done in tmux? I find it a little confusing, but maybe that's just me 🤷

Also - really not a must, but since you asked for rust comments - you can use `find` here to do this: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.find

Code after:
use zellij_server::{os_input_output::get_server_os_input, start_server};
use zellij_utils::{
    cli::{CliArgs, Command, SessionCommand, Sessions},
    consts::{ZELLIJ_TMP_DIR, ZELLIJ_TMP_LOG_DIR},
    logging::*,
    setup::{get_default_data_dir, Setup},
    structopt::StructOpt,
};

pub fn main() {
    configure_logger();
    let opts = CliArgs::from_args();

    if let Some(Command::Sessions(Sessions::ListSessions)) = opts.command {
        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);
                            }
                        }
                    }
                }