summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKunal Mohan <kunalmohan99@gmail.com>2021-05-22 22:12:43 +0530
committerKunal Mohan <kunalmohan99@gmail.com>2021-05-22 22:21:26 +0530
commit0621ba8f349a28d0f746f4c8349658025e5f64f5 (patch)
treebfe825e2987fdc3df35a12a380e1e0730757297e
parent1162d40ea00aec38907c7079e53c62a02c2c5eda (diff)
Allow user to specify session name
-rw-r--r--Cargo.lock2
-rw-r--r--Cargo.toml1
-rw-r--r--src/list_sessions.rs17
-rw-r--r--src/main.rs18
-rw-r--r--src/tests/mod.rs4
-rw-r--r--zellij-client/Cargo.toml1
-rw-r--r--zellij-client/src/lib.rs35
-rw-r--r--zellij-utils/src/cli.rs4
8 files changed, 62 insertions, 20 deletions
diff --git a/Cargo.lock b/Cargo.lock
index a637785b5..e32a829b3 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2290,6 +2290,7 @@ name = "zellij"
version = "0.12.0"
dependencies = [
"insta",
+ "names",
"zellij-client",
"zellij-server",
"zellij-utils",
@@ -2299,7 +2300,6 @@ dependencies = [
name = "zellij-client"
version = "0.12.0"
dependencies = [
- "names",
"termbg",
"zellij-utils",
]
diff --git a/Cargo.toml b/Cargo.toml
index 9e7e31ab4..5f7afaaa6 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,6 +13,7 @@ resolver = "2"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+names = "0.11.0"
zellij-client = { path = "zellij-client/", version = "0.12.0" }
zellij-server = { path = "zellij-server/", version = "0.12.0" }
zellij-utils = { path = "zellij-utils/", version = "0.12.0" }
diff --git a/src/list_sessions.rs b/src/list_sessions.rs
index 41437e1a7..ee72a34d0 100644
--- a/src/list_sessions.rs
+++ b/src/list_sessions.rs
@@ -67,3 +67,20 @@ pub(crate) fn assert_session(name: &str) {
};
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);
+}
diff --git a/src/main.rs b/src/main.rs
index 5a6d5ed18..a91ce508c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,10 +2,10 @@ mod list_sessions;
#[cfg(test)]
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::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_utils::{
cli::{CliArgs, Command, Sessions},
@@ -61,10 +61,20 @@ pub fn main() {
Box::new(os_input),
opts,
config,
- Some((session_name, force)),
+ ClientInfo::Attach(session_name, force),
);
} 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),
+ );
}
}
}
diff --git a/src/tests/mod.rs b/src/tests/mod.rs
index f8a48a39f..e075ed49a 100644
--- a/src/tests/mod.rs
+++ b/src/tests/mod.rs
@@ -5,7 +5,7 @@ pub mod tty_inputs;
pub mod utils;
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_utils::{cli::CliArgs, input::config::Config};
@@ -21,6 +21,6 @@ pub fn start(
start_server(server_os_input, PathBuf::from(""));
})
.unwrap();
- start_client(client_os_input, opts, config, None);
+ start_client(client_os_input, opts, config, ClientInfo::New("".into()));
let _ = server_thread.join();
}
diff --git a/zellij-client/Cargo.toml b/zellij-client/Cargo.toml
index 76a25c1c4..f9d0db086 100644
--- a/zellij-client/Cargo.toml
+++ b/zellij-client/Cargo.toml
@@ -9,7 +9,6 @@ license = "MIT"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
-names = "0.11.0"
termbg = "0.2.3"
zellij-utils = { path = "../zellij-utils/", version = "0.12.0" }
diff --git a/zellij-client/src/lib.rs b/zellij-client/src/lib.rs
index 45b60d44a..f3b0f11d3 100644
--- a/zellij-client/src/lib.rs
+++ b/zellij-client/src/lib.rs
@@ -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(
mut os_input: Box<dyn ClientOsApi>,
opts: CliArgs,
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 take_snapshot = "\u{1b}[?1049h";
@@ -106,20 +112,25 @@ pub fn start_client(
};
#[cfg(not(any(feature = "test", test)))]
- let first_msg = if let Some((name, force)) = attach_to {
- SESSION_NAME.set(name).unwrap();
- std::env::set_var(&"ZELLIJ_SESSION_NAME", SESSION_NAME.get().unwrap());
+ let first_msg = match info {
+ ClientInfo::Attach(name, force) => {
+ SESSION_NAME.set(name).unwrap();
+ std::env::set_var(&"ZELLIJ_SESSION_NAME", SESSION_NAME.get().unwrap());
- ClientToServerMsg::AttachClient(client_attributes, force)
- } else {
- SESSION_NAME
- .set(names::Generator::default().next().unwrap())
- .unwrap();
- std::env::set_var(&"ZELLIJ_SESSION_NAME", SESSION_NAME.get().unwrap());
+ ClientToServerMsg::AttachClient(client_attributes, force)
+ }
+ ClientInfo::New(name) => {
+ SESSION_NAME.set(name).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))]
let first_msg = {
diff --git a/zellij-utils/src/cli.rs b/zellij-utils/src/cli.rs
index 87f929c7e..bf7851c9f 100644
--- a/zellij-utils/src/cli.rs
+++ b/zellij-utils/src/cli.rs
@@ -20,6 +20,10 @@ pub struct CliArgs {
#[structopt(long, parse(from_os_str), hidden = true)]
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
#[structopt(short, long, parse(from_os_str))]
pub layout: Option<PathBuf>,