use crate::escape_format;
use std::ffi::OsString;
use super::SESSION_FORMAT;
use super::{Effects, Op, Operation, Safety, Scope, SessionSlot, Slot};
use crate::Command;
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct NewSession {
#[cfg_attr(
feature = "serde",
serde(
serialize_with = "crate::plan::wire::argument",
deserialize_with = "crate::plan::wire::parse_argument"
)
)]
#[cfg_attr(feature = "schema", schemars(with = "crate::plan::wire::Argument"))]
name: OsString,
#[cfg_attr(
feature = "serde",
serde(
serialize_with = "crate::plan::wire::optional_argument",
deserialize_with = "crate::plan::wire::parse_optional_argument"
)
)]
#[cfg_attr(
feature = "schema",
schemars(with = "Option<crate::plan::wire::Argument>")
)]
start_directory: Option<OsString>,
#[cfg_attr(
feature = "serde",
serde(
serialize_with = "crate::plan::wire::optional_argument",
deserialize_with = "crate::plan::wire::parse_optional_argument"
)
)]
#[cfg_attr(
feature = "schema",
schemars(with = "Option<crate::plan::wire::Argument>")
)]
window_name: Option<OsString>,
}
impl NewSession {
#[must_use]
pub fn new(name: impl Into<OsString>) -> Self {
Self {
name: name.into(),
start_directory: None,
window_name: None,
}
}
#[must_use]
pub fn start_directory(mut self, directory: impl Into<OsString>) -> Self {
self.start_directory = Some(directory.into());
self
}
#[must_use]
pub fn window_name(mut self, name: impl Into<OsString>) -> Self {
self.window_name = Some(name.into());
self
}
pub(crate) fn render(&self) -> Command {
let mut command = Command::new("new-session")
.arg("-d")
.arg("-P")
.arg("-F")
.arg(SESSION_FORMAT)
.arg("-s")
.arg(escape_format(&self.name));
if let Some(directory) = &self.start_directory {
command = command.arg("-c").arg(escape_format(directory));
}
if let Some(name) = &self.window_name {
command = command.arg("-n").arg(escape_format(name));
}
command
}
}
operation!(
NewSession,
creates = Slot<SessionSlot>,
effects = Effects {
creates: Some(Scope::Session),
..Effects::MUTATING
},
safety = Safety::Mutating
);