Skip to content

Commit

Permalink
Implement attach --create
Browse files Browse the repository at this point in the history
  • Loading branch information
FortStatement authored and NextSilicon-Guy committed Sep 16, 2021
1 parent 0f3590a commit cb2e6b0
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 23 deletions.
69 changes: 59 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ mod sessions;
mod tests;

use crate::install::populate_data_dir;
use sessions::{assert_session, assert_session_ne, get_active_session, list_sessions};
use sessions::{
assert_session, assert_session_ne, get_active_session, get_sessions, list_sessions,
print_sessions, session_exists, ActiveSession,
};
use std::process;
use zellij_client::{os_input_output::get_client_os_input, start_client, ClientInfo};
use zellij_server::{os_input_output::get_server_os_input, start_server};
Expand Down Expand Up @@ -52,29 +55,75 @@ pub fn main() {
}
};
if let Some(Command::Sessions(Sessions::Attach {
mut session_name,
session_name,
force,
create,
options,
})) = opts.command.clone()
{
if let Some(session) = session_name.as_ref() {
assert_session(session);
} else {
session_name = Some(get_active_session());
}

let config_options = match options {
Some(SessionCommand::Options(o)) => config_options.merge(o),
None => config_options,
};

let (client, attach_layout) = match session_name.as_ref() {
Some(session) => {
if create {
if !session_exists(session).unwrap() {
(ClientInfo::New(session_name.unwrap()), layout)
} else {
(
ClientInfo::Attach(
session_name.unwrap(),
force,
config_options.clone(),
),
None,
)
}
} else {
assert_session(session);
(
ClientInfo::Attach(
session_name.unwrap(),
force,
config_options.clone(),
),
None,
)
}
}
None => match get_active_session() {
ActiveSession::None => {
if create {
(
ClientInfo::New(names::Generator::default().next().unwrap()),
layout,
)
} else {
println!("No active zellij sessions found.");
process::exit(1);
}
}
ActiveSession::One(session_name) => (
ClientInfo::Attach(session_name, force, config_options.clone()),
None,
),
ActiveSession::Many => {
println!("Please specify the session name to attach to. The following sessions are active:");
print_sessions(get_sessions().unwrap());
process::exit(1);
}
},
};

start_client(
Box::new(os_input),
opts,
config,
config_options.clone(),
ClientInfo::Attach(session_name.unwrap(), force, config_options),
None,
client,
attach_layout,
);
} else {
let session_name = opts
Expand Down
49 changes: 36 additions & 13 deletions src/sessions.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::os::unix::fs::FileTypeExt;
use std::{fs, io, process};
use zellij_utils::{
async_std::io::ErrorKind,
consts::ZELLIJ_SOCK_DIR,
interprocess::local_socket::LocalSocketStream,
ipc::{ClientToServerMsg, IpcSenderWithContext},
};

fn get_sessions() -> Result<Vec<String>, io::ErrorKind> {
pub(crate) fn get_sessions() -> Result<Vec<String>, io::ErrorKind> {
match fs::read_dir(&*ZELLIJ_SOCK_DIR) {
Ok(files) => {
let mut sessions = Vec::new();
Expand Down Expand Up @@ -47,7 +48,7 @@ fn assert_socket(name: &str) -> bool {
}
}

fn print_sessions(sessions: Vec<String>) {
pub(crate) fn print_sessions(sessions: Vec<String>) {
let curr_session = std::env::var("ZELLIJ_SESSION_NAME").unwrap_or_else(|_| "".into());
sessions.iter().for_each(|session| {
let suffix = if curr_session == *session {
Expand All @@ -59,22 +60,29 @@ fn print_sessions(sessions: Vec<String>) {
})
}

pub(crate) fn get_active_session() -> String {
pub(crate) enum ActiveSession {
None,
One(String),
Many,
}

pub(crate) fn get_active_session() -> ActiveSession {
match get_sessions() {
Ok(mut sessions) => {
if sessions.len() == 1 {
return sessions.pop().unwrap();
return ActiveSession::One(sessions.pop().unwrap());
}
if sessions.is_empty() {
println!("No active zellij sessions found.");
ActiveSession::None
} else {
println!("Please specify the session name to attach to. The following sessions are active:");
print_sessions(sessions);
ActiveSession::Many
}
}
Err(e) => eprintln!("Error occured: {:?}", e),
Err(e) => {
eprintln!("Error occured: {:?}", e);
process::exit(1);
}
}
process::exit(1);
}

pub(crate) fn list_sessions() {
Expand All @@ -95,15 +103,30 @@ pub(crate) fn list_sessions() {
process::exit(exit_code);
}

pub(crate) fn assert_session(name: &str) {
match get_sessions() {
pub(crate) fn session_exists(name: &str) -> Result<bool, ErrorKind> {
return match get_sessions() {
Ok(sessions) => {
if sessions.iter().any(|s| s == name) {
return Ok(true);
}
Ok(false)
}
Err(e) => Err(e),
};
}

pub(crate) fn assert_session(name: &str) {
match session_exists(name) {
Ok(result) => {
if result {
return;
} else {
println!("No session named {:?} found.", name);
}
println!("No session named {:?} found.", name);
}
Err(e) => eprintln!("Error occured: {:?}", e),
Err(e) => {
eprintln!("Error occured: {:?}", e);
}
};
process::exit(1);
}
Expand Down
5 changes: 5 additions & 0 deletions zellij-utils/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ pub enum Sessions {
/// zellij client (if any) and attach to this.
#[structopt(long, short)]
force: bool,

/// Create a session if one does not exist.
#[structopt(long)]
create: bool,

/// Change the behaviour of zellij
#[structopt(subcommand, name = "options")]
options: Option<SessionCommand>,
Expand Down

0 comments on commit cb2e6b0

Please sign in to comment.