Allow user to specify session name

This commit is contained in:
Kunal Mohan 2021-05-22 22:12:43 +05:30
parent 1162d40ea0
commit 0621ba8f34
8 changed files with 62 additions and 20 deletions

2
Cargo.lock generated
View file

@ -2290,6 +2290,7 @@ name = "zellij"
version = "0.12.0" version = "0.12.0"
dependencies = [ dependencies = [
"insta", "insta",
"names",
"zellij-client", "zellij-client",
"zellij-server", "zellij-server",
"zellij-utils", "zellij-utils",
@ -2299,7 +2300,6 @@ dependencies = [
name = "zellij-client" name = "zellij-client"
version = "0.12.0" version = "0.12.0"
dependencies = [ dependencies = [
"names",
"termbg", "termbg",
"zellij-utils", "zellij-utils",
] ]

View file

@ -13,6 +13,7 @@ resolver = "2"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
names = "0.11.0"
zellij-client = { path = "zellij-client/", version = "0.12.0" } zellij-client = { path = "zellij-client/", version = "0.12.0" }
zellij-server = { path = "zellij-server/", version = "0.12.0" } zellij-server = { path = "zellij-server/", version = "0.12.0" }
zellij-utils = { path = "zellij-utils/", version = "0.12.0" } zellij-utils = { path = "zellij-utils/", version = "0.12.0" }

View file

@ -67,3 +67,20 @@ pub(crate) fn assert_session(name: &str) {
}; };
process::exit(exit_code); process::exit(exit_code);
} }
pub(crate) fn assert_session_ne(name: &str) {
let exit_code = match get_sessions() {
Ok(sessions) => {
if sessions.iter().all(|s| s != name) {
return;
}
println!("Session with name {:?} aleady exists. Use attach command to connect to it or specify a different name.", name);
0
}
Err(e) => {
eprintln!("Error occured: {:?}", e);
1
}
};
process::exit(exit_code);
}

View file

@ -2,10 +2,10 @@ mod list_sessions;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
use list_sessions::{assert_session, list_sessions}; use list_sessions::{assert_session, assert_session_ne, list_sessions};
use std::convert::TryFrom; use std::convert::TryFrom;
use std::process; use std::process;
use zellij_client::{os_input_output::get_client_os_input, start_client}; 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}; use zellij_server::{os_input_output::get_server_os_input, start_server};
use zellij_utils::{ use zellij_utils::{
cli::{CliArgs, Command, Sessions}, cli::{CliArgs, Command, Sessions},
@ -61,10 +61,20 @@ pub fn main() {
Box::new(os_input), Box::new(os_input),
opts, opts,
config, config,
Some((session_name, force)), ClientInfo::Attach(session_name, force),
); );
} else { } else {
start_client(Box::new(os_input), opts, config, None); let session_name = opts
.session
.clone()
.unwrap_or_else(|| names::Generator::default().next().unwrap());
assert_session_ne(&session_name);
start_client(
Box::new(os_input),
opts,
config,
ClientInfo::New(session_name),
);
} }
} }
} }

View file

@ -5,7 +5,7 @@ pub mod tty_inputs;
pub mod utils; pub mod utils;
use std::path::PathBuf; use std::path::PathBuf;
use zellij_client::{os_input_output::ClientOsApi, start_client}; use zellij_client::{os_input_output::ClientOsApi, start_client, ClientInfo};
use zellij_server::{os_input_output::ServerOsApi, start_server}; use zellij_server::{os_input_output::ServerOsApi, start_server};
use zellij_utils::{cli::CliArgs, input::config::Config}; use zellij_utils::{cli::CliArgs, input::config::Config};
@ -21,6 +21,6 @@ pub fn start(
start_server(server_os_input, PathBuf::from("")); start_server(server_os_input, PathBuf::from(""));
}) })
.unwrap(); .unwrap();
start_client(client_os_input, opts, config, None); start_client(client_os_input, opts, config, ClientInfo::New("".into()));
let _ = server_thread.join(); let _ = server_thread.join();
} }

View file

@ -9,7 +9,6 @@ license = "MIT"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
names = "0.11.0"
termbg = "0.2.3" termbg = "0.2.3"
zellij-utils = { path = "../zellij-utils/", version = "0.12.0" } zellij-utils = { path = "../zellij-utils/", version = "0.12.0" }

View file

@ -76,11 +76,17 @@ fn spawn_server(socket_path: &Path) -> io::Result<()> {
} }
} }
#[derive(Debug, Clone)]
pub enum ClientInfo {
Attach(String, bool),
New(String),
}
pub fn start_client( pub fn start_client(
mut os_input: Box<dyn ClientOsApi>, mut os_input: Box<dyn ClientOsApi>,
opts: CliArgs, opts: CliArgs,
config: Config, config: Config,
attach_to: Option<(String, bool)>, info: ClientInfo,
) { ) {
let clear_client_terminal_attributes = "\u{1b}[?1l\u{1b}=\u{1b}[r\u{1b}12l\u{1b}[?1000l\u{1b}[?1002l\u{1b}[?1003l\u{1b}[?1005l\u{1b}[?1006l\u{1b}[?12l"; let clear_client_terminal_attributes = "\u{1b}[?1l\u{1b}=\u{1b}[r\u{1b}12l\u{1b}[?1000l\u{1b}[?1002l\u{1b}[?1003l\u{1b}[?1005l\u{1b}[?1006l\u{1b}[?12l";
let take_snapshot = "\u{1b}[?1049h"; let take_snapshot = "\u{1b}[?1049h";
@ -106,20 +112,25 @@ pub fn start_client(
}; };
#[cfg(not(any(feature = "test", test)))] #[cfg(not(any(feature = "test", test)))]
let first_msg = if let Some((name, force)) = attach_to { let first_msg = match info {
SESSION_NAME.set(name).unwrap(); ClientInfo::Attach(name, force) => {
std::env::set_var(&"ZELLIJ_SESSION_NAME", SESSION_NAME.get().unwrap()); SESSION_NAME.set(name).unwrap();
std::env::set_var(&"ZELLIJ_SESSION_NAME", SESSION_NAME.get().unwrap());
ClientToServerMsg::AttachClient(client_attributes, force) ClientToServerMsg::AttachClient(client_attributes, force)
} else { }
SESSION_NAME ClientInfo::New(name) => {
.set(names::Generator::default().next().unwrap()) SESSION_NAME.set(name).unwrap();
.unwrap(); std::env::set_var(&"ZELLIJ_SESSION_NAME", SESSION_NAME.get().unwrap());
std::env::set_var(&"ZELLIJ_SESSION_NAME", SESSION_NAME.get().unwrap());
spawn_server(&*ZELLIJ_IPC_PIPE).unwrap(); spawn_server(&*ZELLIJ_IPC_PIPE).unwrap();
ClientToServerMsg::NewClient(client_attributes, Box::new(opts), Box::new(config_options)) ClientToServerMsg::NewClient(
client_attributes,
Box::new(opts),
Box::new(config_options),
)
}
}; };
#[cfg(any(feature = "test", test))] #[cfg(any(feature = "test", test))]
let first_msg = { let first_msg = {

View file

@ -20,6 +20,10 @@ pub struct CliArgs {
#[structopt(long, parse(from_os_str), hidden = true)] #[structopt(long, parse(from_os_str), hidden = true)]
pub server: Option<PathBuf>, pub server: Option<PathBuf>,
/// Specify name of a new session
#[structopt(long, short)]
pub session: Option<String>,
/// Name of a layout file in the layout directory /// Name of a layout file in the layout directory
#[structopt(short, long, parse(from_os_str))] #[structopt(short, long, parse(from_os_str))]
pub layout: Option<PathBuf>, pub layout: Option<PathBuf>,